#!/usr/bin/python
#-*-coding:utf-8-*-
"""
@author: Yugengde
@contact: [Email protected]
@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 ()
# database Insert
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 (' Data store success ', 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)
Data from the Redis database is imported into the SQL Server database