将数据导入MongoDB集群与MySQL
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的更多相关文章
- sqoop将oracle数据导入hdfs集群
使用sqoop将oracle数据导入hdfs集群 集群环境: hadoop1.0.0 hbase0.92.1 zookeeper3.4.3 hive0.8.1 sqoop-1.4.1-incubati ...
- 使用pandas把mysql的数据导入MongoDB。
使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...
- rancher导入k8s集群后添加监控无数据
1.日志报错 rancher导入k8s集群后添加监控无数据,rancher日志报错: k8s.io/kube-state-metrics/pkg/collectors/builder.go:: Fai ...
- mongodb集群安装及到现在遇到的一些问题
集群搭建 只有3台服务器,开始搭建mongodb集群里主要参照的是http://www.lanceyan.com/tech/arch/mongodb_shard1.html,端口的设置也是mongos ...
- 搭建高可用mongodb集群(四)—— 分片(经典)
转自:http://www.lanceyan.com/tech/arch/mongodb_shard1.html 按照上一节中<搭建高可用mongodb集群(三)-- 深入副本集>搭建后还 ...
- [转]搭建高可用mongodb集群(四)—— 分片
按照上一节中<搭建高可用mongodb集群(三)—— 深入副本集>搭建后还有两个问题没有解决: 从节点每个上面的数据都是对数据库全量拷贝,从节点压力会不会过大? 数据压力大到机器支撑不了的 ...
- 搭建高可用mongodb集群(四)—— 分片
按照上一节中<搭建高可用mongodb集群(三)—— 深入副本集>搭建后还有两个问题没有解决: 从节点每个上面的数据都是对数据库全量拷贝,从节点压力会不会过大? 数据压力大到机器支撑不了的 ...
- 搭建高可用mongodb集群(三)—— 深入副本集内部机制
在上一篇文章<搭建高可用mongodb集群(二)—— 副本集> 介绍了副本集的配置,这篇文章深入研究一下副本集的内部机制.还是带着副本集的问题来看吧! 副本集故障转移,主节点是如何选举的? ...
- 搭建高可用mongodb集群(一)——配置mongodb
在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. NOSQL有这些优势: 大数据量,可以通过 ...
随机推荐
- vue教程1-08 交互 get、post、jsonp
vue教程1-08 交互 get.post.jsonp 一.如果vue想做交互,引入: vue-resouce 二.get方式 1.get获取一个普通文本数据: <!DOCTYPE html&g ...
- 在MongoDB中实现聚合函数
在MongoDB中实现聚合函数 随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加. ...
- Java8-函数复合用法
JDK8自带的函数式接口Function有两个默认方法andThen和compose,它们都返回Function的一个实例,可以用这两个方法把Function接口所代表的的Lambda表达式复合起来. ...
- 公共技术点( Java 反射 Reflection)
转载路径:http://p.codekk.com/blogs/detail/5596953ed6459ae7934997c5 本文为 Android 开源项目源码解析 公共技术点中的 Java 反射 ...
- 为什么研发团队不适合量化KPI的绩效考核?
研发团队(如果不是外包,不是机械性的活动)如果进行的是creative的有创造性的智力活动,那么应该不适合用量化KPI的绩效考核和激励,不应该用工时.bug数(难度大的bug可能多,测试人员可能没有经 ...
- CompoundScope说明
A class scope adds capabilities to keep track of changes in relatedclass scopes - this allows client ...
- 【JAVA】内部类,内部接口
内部类: 内部类可以很好的实现隐藏,一般的非内部类,是不允许有 private 与protected权限的,但内部类可以 内部类拥有外围类的所有元素的访问权限 可是实现多重继承 可以避免修改接口而实现 ...
- Chapter 3 Phenomenon——22
He paused, and for a brief moment his stunning face was unexpectedly vulnerable. 他愣住了,然后一段时间他令人昏迷的脸变 ...
- 微信小程序——<radio></radio>大小改变
css样式改变大小: transform:scale(.7);
- php使用 utf8_encode 来将特殊字符转成 utf8
如果在接受 $_POST 或 $_GET 时发生类似的错误报告:SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xF6te ...