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)
posted @ 2017-11-11 21:19  人微言轻1  阅读(2797)  评论(0编辑  收藏  举报