#!/usr/bin/python
# -*- encoding:UTF-8 -*-
# author:pangPython
import json
#將sql語(yǔ)句寫(xiě)入文件
def main():
movie_json_file = open("movies.json","r",encoding='utf-8')
sql_file = open("movies.sql","a",encoding='utf-8')
for line in movie_json_file:
json2sql(sql_file,line)
#解析聚合數(shù)據(jù)返回的json影訊api查詢串為sql語(yǔ)句
def json2sql(sql_file,json_str):
jstr = json.loads(json_str)
if jstr['resultcode'] == "200":
result_jstr = jstr['result']
sql = 'insert into movies(';
key_jstr = ''
value_jstr = '"'
for j in result_jstr.keys():
key_jstr = key_jstr+j+','
for i in result_jstr.values():
value_jstr = value_jstr+str(i)+'","'
#print(sql+key_jstr[:-1]+') values('+value_jstr[:-2]+');')
sql_file.write(sql+key_jstr[:-1]+') values('+value_jstr[:-2]+');\n')
if __name__ == '__main__':
main()
pyHive 連接 hive數(shù)據(jù)庫(kù)