Dataphin计算任务如何通过Python操作ODPS_SQL

更新时间:

问题描述

Dataphin计算任务如何通过Python操作ODPS_SQL。

解决方案

建表和插入语句如下,ODPS必须有读写权限:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

################################################

import pandas as pd

from odps import ODPS

o=ODPS('access_id_XXX', 'access_key_XXX', 'project_name_XXX',
endpoint='http://service.odps.aliyun.com/api')

o.execute_sql('''

drop TABLE if exists luyao_online_proj.s_store_zr11;

CREATE TABLE IF NOT EXISTS luyao_online_proj.s_store_zr11

(

s_store_sk STRING ,

s_store_id STRING ,

s_rec_start_date STRING ,

s_rec_end_date STRING

)

PARTITIONED BY (

`ds` STRING

);

''',hints={"odps.sql.submit.mode":"script"})
#!/usr/bin/env python

# -*- coding: utf-8 -*-

################################################

import pandas as pd

from odps import ODPS

o=ODPS('access_id_XXX', 'access_key_XXX', 'project_name_XXX',
endpoint='http://service.odps.aliyun.com/api')

o.execute_sql('''

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10001,'c0001','20200618','20200619');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10002,'c0002','20200519','20200520');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10003,'c0003','20200520','20200521');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10004,'c0004','20200519','20200520');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10005,'c0005','20200517','20200518');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10005,'c0005','20200515','20200520');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10007,'c0007','20200515','20200519');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10008,'c0008','20200514','20200518');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(10009,'c0009','20200515','20200517');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(100010,'c00010','20200513','20200516');

insert into table luyao_online_proj.s_store_zr11 partition (ds ='20221024') values(100011,'c00011','20200519','20200516');

''',hints={"odps.sql.submit.mode":"script"})

适用于

  • Dataphin