python实例pyspark以及pyt
%pyspark
#查询认证用户
import sys
#import MySQLdb
import mysql.connector
import pandas as pd
import datetime
import time
optmap = {
'dbuser' : 'haoren',
'dbpass' : 'G4d',
'dbhost' : '172.12.112.5',
'dbport' : 3306,
'dbname' : 'GMDB'
}
def sql_select(reqsql):
ret = ''
try:
db_conn = mysql.connector.connect(user=optmap['dbuser'], password=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], database=optmap['dbname'])
db_cursor=db_conn.cursor()
count = db_cursor.execute(reqsql)
ret = db_cursor.fetchall()
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
db_cursor.close()
db_conn.close
return ret
userlist = []
def renzhengsingger(startday,endday):
t1 = int(time.mktime(time.strptime(startday,'%Y-%m-%d %H:%M:%S')) )
t2 = int(time.mktime(time.strptime(endday,'%Y-%m-%d %H:%M:%S')))
for n in range(0,10):
reqsql = "select PERFORMERID,sum(DURATION)/3600 from PERFORMERSHOWTIMERECORD%d where STARTTIME >=%s and STARTTIME <%s group by PERFORMERID ;" %(n,t1,t2)
ret = sql_select(reqsql)
userlist.append(ret)
#print userlist
for i in range(0,10):
for p in userlist[i]:
print p[0],p[1]
renzhengsingger('2017-08-01 00:00:00','2017-09-01 00:00:00')
======================================================================================================================
%pyspark
#查询认证用户
import sys
#import MySQLdb
import mysql.connector
import pandas as pd
import datetime
import time
optmap = {
'dbuser' : 'haoren',
'dbpass' : 'G4d',
'dbhost' : '172.12.112.8',
'dbport' : 3306,
'dbname' : 'IMDB'
}
optmap1 = {
'dbuser' : 'haoren',
'dbpass' : 'G4d',
'dbhost' : '172.12.112.5',
'dbport' : 3306,
'dbname' : 'GMDB'
}
def sql_select(reqsql):
ret = ''
try:
db_conn = mysql.connector.connect(user=optmap['dbuser'], password=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], database=optmap['dbname'])
db_cursor=db_conn.cursor()
count = db_cursor.execute(reqsql)
ret = db_cursor.fetchall()
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
db_cursor.close()
db_conn.close
return ret
def sql_select1(reqsql1):
ret = ''
try:
db_conn1 = mysql.connector.connect(user=optmap1['dbuser'], password=optmap1['dbpass'], host=optmap1['dbhost'], port=optmap1['dbport'], database=optmap1['dbname'])
db_cursor1=db_conn1.cursor()
count = db_cursor1.execute(reqsql1)
ret1 = db_cursor1.fetchall()
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
db_cursor1.close()
db_conn1.close
return ret1
#定义查询认证用户函数
def renzhengsingger(startday,endday):
t1 = int(time.mktime(time.strptime(startday,'%Y-%m-%d %H:%M:%S')) )
t2 = int(time.mktime(time.strptime(endday,'%Y-%m-%d %H:%M:%S')))
reqsql = "select PERFORMERID,from_unixtime(ADDTIME) from PERFORMERINFO where ADDTIME >=%s and ADDTIME < %s" %(t1,t2)
ret = sql_select(reqsql)
for i in ret:
#print i[0]
id = int(i[0])%10
reqsql1 = "select sum(DURATION)/3600 from PERFORMERSHOWTIMERECORD%d where STARTTIME >=%s and STARTTIME <%s and PERFORMERID=%d" %(id,t1,t2,i[0])
#print reqsql1
ret1 = sql_select1(reqsql1)
print i[0],",",i[1],",",ret1[0][0]
renzhengsingger('2017-08-01 00:00:00','2017-09-01 00:00:00')
================================================================================================
%pyspark
#encoding=gbk
#-*-coding:gbk-*-
import sys
#import MySQLdb
import mysql.connector
import pandas as pd
import datetime
import time
optmap = {
'dbuser' : 'haoren',
'dbpass' : 'G4d',
'dbhost' : '172.17.12.8',
'dbport' : 3306,
'dbname' : 'IMDB'
}
optmap1 = {
'dbuser' : 'haoren',
'dbpass' : 'G4d',
'dbhost' : '172.17.12.5',
'dbport' : 3306,
'dbname' : 'GMDB'
}
def sql_select(reqsql): #定义数据库连接执行函数
ret = ''
try:
db_conn = mysql.connector.connect(user=optmap['dbuser'], password=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], database=optmap['dbname'])
db_cursor=db_conn.cursor()
count = db_cursor.execute(reqsql)
ret = db_cursor.fetchall()
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
db_cursor.close()
db_conn.close
return ret
def sql_select1(reqsql1): #定义数据库连接执行函数
ret = ''
try:
db_conn1 = mysql.connector.connect(user=optmap1['dbuser'], password=optmap1['dbpass'], host=optmap1['dbhost'], port=optmap1['dbport'], database=optmap1['dbname'])
db_cursor1=db_conn1.cursor()
count = db_cursor1.execute(reqsql1)
ret1 = db_cursor1.fetchall()
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
db_cursor1.close()
db_conn1.close
return ret1
#批量查询消费者的昵称
def getnickname(uid): #定义函数
id = int(uid)%10
reqsql = "select CHANNELNICKNAME from CHARBASE%d where ID=%d" %(id,uid) #数据库字段可能含有中文,需要处理后方可正常显示
#reqsql = "select NICKNAME from CHARBASE%d where ID=%d" %(id,uid)
ret = sql_select(reqsql)
return ret
userlist = [49072058,54332450,23154709] #定义一个字典
for i in userlist:
#查询消费者昵称
ret1 = getnickname(i)
ss = ret1[0][0]
ss = ss.encode('unicode-escape').decode('string_escape') #开始处理带有中文的字符
nick_name = ss.decode('gbk') #解码后方可正常显示
print i ,nick_name
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341