利用Python遍历odps元数据信息

bm:表名

fq:分区

cjsj:分区创建时间

smzq:生命周期

dx:分区大小

from odps import ODPS
odps = ODPS('xxxxxxxxx', 'xxxxxxxxxxxx', 'dyx',
            endpoint='http://xxxxxxxxx.odps.ops.yun.ga/api')
odps1 = ODPS('xxxxxxxxxxxx', 'xxxxxxxxxxxxxx', 'dwd__dev',
            endpoint='http://xxxxxxxxx1.odps.ops.yun.ga/api')            
import json
project = odps.get_project()
print(project)
#获取需要执行的表
sql='''select replace(trim(mbbm),'dwd__dev.','')mbbm from t_julong_bzc_sjgxzq where xh >=60 and xh<70 and facjsj<>''  '''
#读出表结果数据
with odps1.execute_sql(sql).open_reader() as reader:
    for record in reader:
        try:            
            name=record.mbbm            
#遍历出元数据信息        
            for table in odps.list_tables():                
#判断自定义表数据存在于元数据里面
                if name == table.name:            
                #print(name)
                #print (table.name)
#判断表是否分区
                    if table.schema.partitions:                        
                        a=list=[]                                                           
#遍历元数据的 partitions信息
                        for partition in table.partitions:                                                        
                            fq=str(partition.name)
                            name=str(project.name+'.'+table.name)
                            sj=str(partition.creation_time)
                            dx=str(partition.size)
                            smzq=str(partition.lifecycle)
                            list.append('('+name)
                            list.append(fq) 
                            list.append(sj) 
                            list.append(smzq)
                            list.append(dx+')')                                                                                                                        
                      #  return list
                            continue
                        sql='''insert into t_jl_sjtz_gxqk PARTITION(xmkj='dwd__dev')(bm,fq,cjsj,smzq,dx) values %s '''%str(str(str(str(a).replace('[\'(','(\'') ).replace(')\']','\')')).replace('\'(','(\'')).replace(')\'','\')')
                        print (sql)
                        odps1.execute_sql(sql)
        except Exception as e:
		print(e)                
                    #print str(str(str(str(a).replace('[\'(','(\'') ).replace(')\']','\')')).replace('\'(','(\'')).replace(')\'','\')')
                    #sql1='''select array(%s) '''%(str(a).replace("]",""))
                    #print(sql1)
                    #s=a.list.repalce('[','')
                    #print  (a)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值