Python3 讀取odps數(shù)據(jù)庫數(shù)據(jù)

Python3 讀取odps數(shù)據(jù)庫數(shù)據(jù)

pyodps安裝

如果未安裝pyodps包,則需要先安裝:

pip install pyodps

ODPS常用包導(dǎo)入

from odps import ODPS

from odps import DataFrame

import pandas as pd

from collections import defaultdict

連接odps數(shù)據(jù)庫

o = ODPS(access_id='access id', #登陸賬號

????????secret_access_key='password', #登陸密碼

????????project='projectname', #odps上的項目名稱

????????endpoint='http://service.odps.aliyun.com/api') #官方提供的接口

獲取數(shù)據(jù):

方法一:通過odps內(nèi)置DataFrame讀取,該方法讀取的數(shù)據(jù)結(jié)構(gòu)類型為odps.df.expr.core.DataFrame

def get_odps_table(tb_name):

???data = DataFrame(o.get_table(tb_name))

???data['ds'] = data['ds'].astype('int')

return data

rdata = get_odps_table('tb_name') #獲取表數(shù)據(jù)實例

方法二:通過sql獲取數(shù)據(jù),該方法得到的數(shù)據(jù)類型為pandas.core.frame.

DataFrame。

def exe_sql(sql):

???data = []

???with o.execute_sql(sql).open_reader() as reader:

???????d = defaultdict(list)? #collection默認(rèn)一個dict

???????for record in reader:

???????????for res in record:

??????????????? d[res[0]].append(res[1])?

??????? data = pd.DataFrame.from_dict(d,orient='index').T??

???return data

rdata =?exe_sql('select aname,bname from tablename where aid = "1111"') #獲取數(shù)據(jù)實例

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容