import sys

import json

import pymongo

import datetime

from pymongo import MongoClient

client = MongoClient('mongodb://192.168.1.31:20000,192.168.1.34:20000')

db = client.RHY

collection = db.ST_RIVER_R

f = open("D:/bigdata/st_river_r.CSV")

line = f.readline()

print(line)

fieldNames = line.split(',')

# STCD,TM,Z,Q,XSA,XSAVV,XSMXV,FLWCHRCD,WPTN,MSQMT,MSAMT,MSVMT

line = f.readline()

count = 0

records = []

insertCount = 0

while line:
     #
     count = count + 1
     fieldValues = line.split(',')
     if len(fieldValues) == 12 or fieldValues[0].strip() != '':
         insertObj = {}
         STCD = fieldValues[0]
         insertObj['STCD'] = STCD
         TM = fieldValues[1]
         if TM.strip() != '':
             TM = datetime.datetime.strptime(TM, '%Y-%m-%d %H:%M:%S')
             insertObj['TM'] = TM
         Z = fieldValues[2]
         if Z.strip() != '':
             Z = float(Z)
             insertObj['Z'] = Z
         Q = fieldValues[3]
         if Q.strip() != '':
             Q = float(Q)
             insertObj['Q'] = Q
         # XSA
         XSA = fieldValues[4]
         if XSA.strip() != '':
             XSA = float(XSA)
             insertObj['XSA'] = XSA
         # XSAVV
         XSAVV = fieldValues[5]
         if XSAVV.strip() != '':
             XSAVV = float(XSAVV)
             insertObj['XSAVV'] = XSAVV
         #
         XSMXV = fieldValues[6]
         if XSMXV.strip() != '':
             XSMXV = float(XSMXV)
             insertObj['XSMXV'] = XSMXV
         #
         FLWCHRCD = fieldValues[7]
         if FLWCHRCD.strip() != '':
             insertObj['FLWCHRCD'] = FLWCHRCD
         #
         WPTN = fieldValues[8]
         if WPTN.strip() != '':
             insertObj['WPTN'] = WPTN
         #
         MSQMT = fieldValues[9]
         if MSQMT.strip() != '':
             insertObj['MSQMT'] = MSQMT
         #
         MSAMT = fieldValues[10]
         if MSAMT.strip() != '':
             insertObj['MSAMT'] = MSAMT
         #
         MSVMT = fieldValues[11]
         if MSVMT.strip() != '':
             insertObj['MSVMT'] = MSVMT
         #
         # collection.insert_one(insertObj)
         # collection.insert_many(new_posts)
         records.append(insertObj)
         if len(records) == 1000:
             insertCount = insertCount + 1
             if count > 1451000:
                 collection.insert_many(records)
                 print(str(count) + '  ' + str(insertCount))
             print(count)
             records = []
     else:
         print(line)
     #
     line = f.readline()

f.close()

client.close()

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import sys

import json

import math

import copy

import pymongo

import datetime

from pymongo import MongoClient

import shapefile

import pymysql

sf = shapefile.Reader(r'E:/Ambari/ubuntu/mapdata/aircraftPositionLine50.shp')

fields = sf.fields

shapes = sf.shapes()

count = len(shapes)

print('count: ' + str(count))

fieldName = []

for index in range(len(fields)):
     if index > 0:
         field = fields[index]
         # print(field)
         fieldName.append(field[0])

#print(fieldName)

#

db = pymysql.connect("127.0.0.1","root","gis","acms" )

cursor = db.cursor()

sql = "INSERT INTO airline_r(id, code, name, time_index, x, y, z, angle) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
  

for index in range(count):
     preX = None
     preY = None
     preZ = None
     angle = None

features = []
     record = sf.record(index)
     attribute = record[0:len(fields)]
     attribute[0] = index
     print(attribute)
     shap = shapes[index]
     points = shap.points
     pointCount = len(points)

for i in range(pointCount):
         coordinate = shap.points[i]
         x = coordinate[0]
         y = coordinate[1]
         z = (0 if (len(coordinate) < 3) else coordinate[2])
         if preX != None:
             angle = math.atan2(y-preY, x - preX)
             feature = copy.deepcopy(attribute)
             feature.append(i-1)
             feature.append(preX)
             feature.append(preY)
             feature.append(preZ)
             feature.append(angle)
             print(feature)
             features.append(tuple(feature))
             #cursor.execute(sql % tuple(feature))
             #cursor.execute(sql, feature)
         if i == pointCount -1:
             feature = copy.deepcopy(attribute)
             feature.append(i)
             feature.append(x)
             feature.append(y)
             feature.append(z)
             feature.append(angle)
             print(feature)
             features.append(tuple(feature))
             #cursor.execute(sql % tuple(feature))
             #cursor.execute(sql, feature)
         preX = x
         preY = y
         preZ = z
     #print(features)
     cursor.executemany(sql, features)
     db.commit()
     '''  
     try:
         # 执行sql语句
         cursor.executemany(sql, features)
         # 提交到数据库执行
         db.commit()
     except:
         # 如果发生错误则回滚
         print()
         db.rollback()
     '''

# 关闭数据库连接

db.close()

'''

client = MongoClient('mongodb://192.168.1.31:20000,192.168.1.34:20000')

db = client.RHY

collection = db.ST_RIVER_R

f = open("D:/bigdata/st_river_r.CSV")

line = f.readline()

print(line)

fieldNames = line.split(',')

# STCD,TM,Z,Q,XSA,XSAVV,XSMXV,FLWCHRCD,WPTN,MSQMT,MSAMT,MSVMT

line = f.readline()

count = 0

records = []

insertCount = 0

while line:
     #
     count = count + 1
     fieldValues = line.split(',')
     if len(fieldValues) == 12 or fieldValues[0].strip() != '':
         insertObj = {}
         STCD = fieldValues[0]
         insertObj['STCD'] = STCD
         TM = fieldValues[1]
         if TM.strip() != '':
             TM = datetime.datetime.strptime(TM, '%Y-%m-%d %H:%M:%S')
             insertObj['TM'] = TM
         Z = fieldValues[2]
         if Z.strip() != '':
             Z = float(Z)
             insertObj['Z'] = Z
         Q = fieldValues[3]
         if Q.strip() != '':
             Q = float(Q)
             insertObj['Q'] = Q
         # XSA
         XSA = fieldValues[4]
         if XSA.strip() != '':
             XSA = float(XSA)
             insertObj['XSA'] = XSA
         # XSAVV
         XSAVV = fieldValues[5]
         if XSAVV.strip() != '':
             XSAVV = float(XSAVV)
             insertObj['XSAVV'] = XSAVV
         #
         XSMXV = fieldValues[6]
         if XSMXV.strip() != '':
             XSMXV = float(XSMXV)
             insertObj['XSMXV'] = XSMXV
         #
         FLWCHRCD = fieldValues[7]
         if FLWCHRCD.strip() != '':
             insertObj['FLWCHRCD'] = FLWCHRCD
         #
         WPTN = fieldValues[8]
         if WPTN.strip() != '':
             insertObj['WPTN'] = WPTN
         #
         MSQMT = fieldValues[9]
         if MSQMT.strip() != '':
             insertObj['MSQMT'] = MSQMT
         #
         MSAMT = fieldValues[10]
         if MSAMT.strip() != '':
             insertObj['MSAMT'] = MSAMT
         #
         MSVMT = fieldValues[11]
         if MSVMT.strip() != '':
             insertObj['MSVMT'] = MSVMT
         #
         # collection.insert_one(insertObj)
         # collection.insert_many(new_posts)
         records.append(insertObj)
         if len(records) == 1000:
             insertCount = insertCount + 1
             if count > 1451000:
                 collection.insert_many(records)
                 print(str(count) + '  ' + str(insertCount))
             print(count)
             records = []
     else:
         print(line)
     #
     line = f.readline()

f.close()

client.close()

'''

将数据导入MongoDB集群与MySQL的更多相关文章

  1. sqoop将oracle数据导入hdfs集群

    使用sqoop将oracle数据导入hdfs集群 集群环境: hadoop1.0.0 hbase0.92.1 zookeeper3.4.3 hive0.8.1 sqoop-1.4.1-incubati ...

  2. 使用pandas把mysql的数据导入MongoDB。

    使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...

  3. rancher导入k8s集群后添加监控无数据

    1.日志报错 rancher导入k8s集群后添加监控无数据,rancher日志报错: k8s.io/kube-state-metrics/pkg/collectors/builder.go:: Fai ...

  4. mongodb集群安装及到现在遇到的一些问题

    集群搭建 只有3台服务器,开始搭建mongodb集群里主要参照的是http://www.lanceyan.com/tech/arch/mongodb_shard1.html,端口的设置也是mongos ...

  5. 搭建高可用mongodb集群(四)—— 分片(经典)

    转自:http://www.lanceyan.com/tech/arch/mongodb_shard1.html 按照上一节中<搭建高可用mongodb集群(三)-- 深入副本集>搭建后还 ...

  6. [转]搭建高可用mongodb集群(四)—— 分片

    按照上一节中<搭建高可用mongodb集群(三)—— 深入副本集>搭建后还有两个问题没有解决: 从节点每个上面的数据都是对数据库全量拷贝,从节点压力会不会过大? 数据压力大到机器支撑不了的 ...

  7. 搭建高可用mongodb集群(四)—— 分片

    按照上一节中<搭建高可用mongodb集群(三)—— 深入副本集>搭建后还有两个问题没有解决: 从节点每个上面的数据都是对数据库全量拷贝,从节点压力会不会过大? 数据压力大到机器支撑不了的 ...

  8. 搭建高可用mongodb集群(三)—— 深入副本集内部机制

    在上一篇文章<搭建高可用mongodb集群(二)—— 副本集> 介绍了副本集的配置,这篇文章深入研究一下副本集的内部机制.还是带着副本集的问题来看吧! 副本集故障转移,主节点是如何选举的? ...

  9. 搭建高可用mongodb集群(一)——配置mongodb

    在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. NOSQL有这些优势: 大数据量,可以通过 ...

随机推荐

  1. Spring Boot 中使用 Jedis 及 Lettuce的对比

    首先,同样的程序,采用不同方式的Redis连接方式. defautl : 默认,0配置 ,也就是走的是 lettuce 单通道方式.   端口:8081 jedis : 使用Jedis 连接池.    ...

  2. oralce11g RAC 启动后 CRS-0184: Cannot communicate with the CRS daemon.

    很奇怪的一个问题! ORACLE数据库服务器,系统启动之后,查看集群状态,发现CRS实例不可用,然后网上查找资料: 隔了几分钟之后,再次查询相关集群服务状态,发现正常了!!! 暂时得出的结论:操作系统 ...

  3. syslog之二:syslog协议及rsyslog服务全解析

    目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...

  4. 广度优先遍历-BFS、深度优先遍历-DFS

    广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...

  5. haproxy+keepalived原理特点

    所有的系统,都是先经历一个单台机器搞所有业务的时代,一个程序+一个mysql数据库,就可以满足开发及第一个版本上线的要求.随着,数据的增加以及业务的增长,这些应用就面临一个访问量的扩大以及扩展的问题. ...

  6. Entity Framework 6.x 学习之Database First

    一.单表操作 1. 建表 CREATE TABLE [Chapter1].[Customer] ( , ), ) COLLATE Chinese_PRC_CI_AS NOT NULL, ) COLLA ...

  7. Iptables之recent模块小结

    Iptables的recent模块用于限制一段时间内的连接数, 是谨防大量请求攻击的必杀绝技! 善加利用该模块可充分保证服务器安全. recent常用参数--name      设定列表名称,即设置跟 ...

  8. SpringMvc @JsonView 使用方式

    准确来说,@JsonView注解不是Spring的,它位于jackson-annotation包中: 作用:SpringMvc使用@ResponseBody将结果以json返回客户端,  有些实体类的 ...

  9. Python面向对象基础一

    公司可能过一两个月就要从深圳搬到东莞松山湖,项目组的现在有的在转Java或其他语言的,问我们要不要转java+hoodap+spark方向,我还是先不转,毕竟之前是从ios转回C#,这现在在转其他的那 ...

  10. Spring基础(8) : properties配置文件

    <context:property-placeholder location="p.properties"/> <bean id="p" cl ...