redis数据库的数据导入到SQLServer数据库中
Posted 人贱言轻
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis数据库的数据导入到SQLServer数据库中相关的知识,希望对你有一定的参考价值。
1./.
#!/usr/bin/python
# -*-coding:utf-8-*-
"""
@author: yugengde
@contact: yugengde@163.com
@file : redis_sqlserver.py
@time: 2017/11/11 16:50
"""
import redis
import pymssql
import json
from multiprocessing import Pool
MSSQL_URL = \'**.**.**.**\'
MSSQL_USER = \'sa\'
MSSQL_PASSWD = \'****\'
MSSQL_DB = \'amazon\'
MSSQL_CHARSET = \'utf8\'
r = redis.Redis(host=\'**.**.**.**\',password=\'******\')
def insert_sqlserver(item):
# print(\'start insert...\')
item = json.loads(item)
conn = pymssql.connect(host=MSSQL_URL,user=MSSQL_USER,password=MSSQL_PASSWD,database=MSSQL_DB,charset=MSSQL_CHARSET)
cursor = conn.cursor()
# 数据库插入
sql = "insert into amazon(\\
title,brand,classify,sku,price,\\
comment_num,comment_av,product_attr,promotion,product_desc,\\
product_info,url,save_time,hash_value\\
) VALUES (%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s)"
try:
cursor.execute(sql, (item[\'title\'],item[\'brand\'],item[\'classify\'],item[\'sku\'],item[\'price\'], \\
item[\'comment_num\'],item[\'comment_av\'],item[\'product_attr\'],item[\'promotion\'],item[\'product_desc\'], \\
item[\'product_info\'],item[\'url\'],item[\'save_time\'],item[\'hash_value\'],))
conn.commit()
# print(\'==================================================================\')
# print(\'数据存储成功\', item)
# print(\'==================================================================\')
except:
print(\'==================================================================\')
print(\'error\',item)
print(\'==================================================================\')
cursor.close()
conn.close()
if __name__ == "__main__":
for i in range(100):
items = r.lrange(\'amaz:items\',start=1000*i,end=1000*(i+1))
pool = Pool()
pool.map(insert_sqlserver,items)
以上是关于redis数据库的数据导入到SQLServer数据库中的主要内容,如果未能解决你的问题,请参考以下文章
怎么把sqlserver数据库表里面的数据导入到mysql数据库里?