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ù)實例