将数据导入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()
'''
最新文章
- Objective-C 数据集合
- php中session锁--如何防止阻塞请求(译)
- OSG-OSGEarth
- 咏南IOCP中间件
- 联系旭日150安装CentOS5.X版本手记
- Debug BLE application with nRF Sniffer+wireshark
- Android开发—— Native 与 Web 之架构抉择
- Oracle SYS_CONTEXT Function
- 我的Android进阶之旅------>;Android安全退出应用程序的几种方式
- ASP.NET Excel导入到SQL Server数据库
- [原创] linux deepin 2014.1下编译putty
- Python学习笔记——基础篇【第六周】——logging模块
- KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情
- SOD开源框架MSF(消息服务框架)介绍
- iOS UITabView简写瀑布流
- C语言程序设计基础-第1周作业-初步
- grunt压缩js代码
- Android 取消标题栏
- 最详尽的 JS 原型与原型链终极详解,没有「可能是」。(一)
- 1.3 Java中的标识符和关键字
热门文章
- vue教程2-05 v-for循环 重复数据无法添加问题 加track-by=&#39;索引&#39;
- 匿名类、包、权限修饰符_DAY10
- ES代码总结2
- Java实现Socket通信
- 【从0到1学Web前端】CSS定位问题三(相对定位,绝对定位) 分类: HTML+CSS 2015-05-29 23:01 842人阅读 评论(0) 收藏
- 装饰者模式——Java设计模式
- redis-小用
- 使用whiptail开发linux环境交互式对话框
- 团队作业6——展示博客(alpha阶段)
- C++语言------顺序表实现,用动态数组的方法