1. 程式人生 > >日誌資料檔案Json欄位資料入庫處理

日誌資料檔案Json欄位資料入庫處理

# -*- encoding:utf-8 -*-
import json
import warnings
import os
import os.path
import shutil


warnings.filterwarnings("ignore")


if __name__=="__main__": 
 import sys
 reload(sys)
 sys.setdefaultencoding( "utf-8" ) 


 #os.remove('/home/hs/opt/User_Behavior_Log/log_data/access_json.dat') 
 f=open('/home/hs/opt/User_Behavior_Log/log_data/access_json.dat','w+')
 f.close()


 if len(sys.argv)==1:  
     print "need argv"  
 else:  
     print sys.argv


for json_data in open('/home/hs/opt/User_Behavior_Log/log_data/access.json'):
    uri = json_data.split('\t')[0] 
    json_col = json.loads(json_data.split('\t')[1])


#    print json_col['shop_id'][0]
    
    
    if json_col.has_key('app_version'):
       app_version  =   json_col['app_version'][0]
    else:
       app_version  = '0'


    if json_col.has_key('code'):
       code  =   json_col['code'][0]
    else:
       code  = '0'


    if json_col.has_key('delivery_type'):
       delivery_type  =   json_col['delivery_type'][0]
    else:
       delivery_type  = '0'


    if json_col.has_key('protocol_version'):
       protocol_version  =   json_col['protocol_version'][0]
    else:
       protocol_version  = '0'


    if json_col.has_key('device_id'):
       device_id  =   json_col['device_id'][0]
    else:
       device_id  = '0'


    if json_col.has_key('device_type'):
       device_type  =   json_col['device_type'][0]
    else:
       device_type  = '0'


    if json_col.has_key('dormentry_id'):
       dormentry_id  =   json_col['dormentry_id'][0]
    else:
       dormentry_id  = '0'


    if json_col.has_key('dormitory'):
       dormitory  =   json_col['dormitory'][0].replace("\n","").replace("\t","")
    else:
       dormitory  = '0'


    if json_col.has_key('expect_end_time'):
       expect_end_time  =   json_col['expect_end_time'][0]
    else:
       expect_end_time  = '0'


    if json_col.has_key('expect_start_time'):
       expect_start_time  =   json_col['expect_start_time'][0]
    else:
       expect_start_time  = '0'


    if json_col.has_key('item_id'):
       item_id  =   json_col['item_id'][0]
    else:
       item_id  = '0'


    if json_col.has_key('num_per_page'):
       num_per_page  =   json_col['num_per_page'][0]
    else:
       num_per_page  = '0'


    if json_col.has_key('order_sn'):
       order_sn  =   json_col['order_sn'][0]
    else:
       order_sn  = '0'


    if json_col.has_key('page'):
       page  =   json_col['page'][0]
    else:
       page  = '0'


    if json_col.has_key('pay_type'):
       pay_type  =   json_col['pay_type'][0]
    else:
       pay_type  = '0'


    if json_col.has_key('phone'):
       phone  =   json_col['phone'][0]
    else:
       phone  = '0'


    if json_col.has_key('protocol_version'):
       protocol_version  =   json_col['protocol_version'][0]
    else:
       protocol_version  = '0'


    if json_col.has_key('quantity'):
       quantity  =   json_col['quantity'][0]
    else:
       quantity  = '0'


    if json_col.has_key('rid'):
       rid  =   json_col['rid'][0]
    else:
       rid  = '0'


    if json_col.has_key('shop_id'):
       shop_id  =   json_col['shop_id'][0]
    else:
       shop_id  = '0'


    if json_col.has_key('sign'):
       sign  =   json_col['sign'][0]
    else:
       sign  = '0'


    if json_col.has_key('site_id'):
       site_id  =   json_col['site_id'][0]
    else:
       site_id  = '0'


    if json_col.has_key('time'):
       time  =   json_col['time'][0]
    elif json_col.has_key('_'):
       time  =   json_col['_'][0]
    else:
       time  = '0'


    if json_col.has_key('token'):
       token  =   json_col['token'][0]
    else:
       token  = '0'


    if json_col.has_key('type'):
       type  =   json_col['type'][0]
    else:
       type  = 0


    log_date          =   int(sys.argv[1]) 
    
    f=open('/home/hs/opt/User_Behavior_Log/log_data/access_json.dat','a+')
    f.write(uri+'\t'+app_version+'\t'+code+'\t'+delivery_type+'\t'+device_id+'\t'+device_type+'\t'+dormentry_id+'\t'+dormitory+'\t'+expect_end_time+'\t'+expect_start_time+'\t'+item_id+'\t'+num_per_page+'\t'+order_sn+'\t'+page+'\t'+pay_type+'\t'+phone+'\t'+protocol_version+'\t'+quantity+'\t'+rid+'\t'+shop_id+'\t'+sign+'\t'+site_id+'\t'+time+'\t'+token+'\t'+`type`+'\t'+'%d'%log_date+'\n')
    f.close()


print 'json_only_one_col_insert_file done'