几个有用的python脚本

1、通过pythony连接Hive执行Hql的脚本
[spark@Master Py_logproc]$ cat py2hive.py 
#!/usr/bin/env python
import sys
sys.path.append('/home/spark/opt/hive-1.2.1/lib/py')
from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
def hiveExe(sql):
    try:
        transport = TSocket.TSocket('127.0.0.1', 10000) 
        transport = TTransport.TBufferedTransport(transport)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        client = ThriftHive.Client(protocol)
        transport.open()
        client.execute(sql)
        print "The return value is : " 
        print client.fetchAll()
        print "............"
        transport.close()
    except Thrift.TException, tx:
        print '%s' % (tx.message)
if __name__ == '__main__':
    select_sql="select url_current from yemao1_log limit 100";
    hiveExe(select_sql)
   
2、通过python解析json数据并导出成普通文本的脚本
[spark@Master Py_logproc]$ cat json2file.py
# -*- encoding:utf-8 -*-
import json
import warnings
warnings.filterwarnings("ignore")




if __name__=="__main__": 
 import sys
 reload(sys)
 sys.setdefaultencoding( "utf-8" ) 
 
 if len(sys.argv)==1:  
     print "need argv"  
 else:  
     print sys.argv  




for json_array in open('/home/spark/opt/Log_Data/Py_logproc/log_tmpdir/yemaopythonlog'):
  yemao_array = json.loads(json_array)
  for yemao in yemao_array: 
   if not yemao.has_key('_reason'):
    
    time            =   yemao['time']
    url_current     =   yemao['url_current']
    phone           =   yemao['phone']
    token           =   yemao['token']
    
    f=open('/home/spark/opt/Log_Data/Py_logproc/log_tmpdir/xxxx.txt','a+')
    f.write('%d'%time+'|'+url_current+'|'+phone+'|'+token+'\n')
    f.close()




print 'yemao_array_insert_file done'

3、Mysql连接供调用的python脚本
[spark@Master Py_logproc]$ cat db.py
import MySQLdb




db_config = {
    'host': '120.55.189.188',
    'user': 'datahouse',
    'passwd': 'DTHS2016',
    'port': 3306,
    'db': 'logdata',
    'charset': 'utf8'
}




def getDB():
  try:
    conn = MySQLdb.connect(host=db_config['host'],user=db_config['user'],passwd=db_config['passwd'],port=db_config['port'],charset=db_config['charset'])
    conn.autocommit(True)
    curr = conn.cursor()
    curr.execute("SET NAMES utf8");
    curr.execute("USE %s" % db_config['db']);




    return conn, curr
  except MySQLdb.Error,e:
    print "Mysql Error %d: %s" % (e.args[0], e.args[1])
    return None, None

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值