简单先知与脚本
epltoolset的Python项目详细描述
EPL工具集
EPL代表抽取熊猫负载。一个使oracle python etl脚本开发更容易的包。当只需要简单的功能时,使用cx_oracle和pandas可能会过于繁琐。这个包试图用更少的样板代码实现这种交互。打算使用python 3.6
安装
epltoolset在pypi上可用,使用pip可以更容易地安装。
pip install epltoolset
创建凭证文件
该包提供了一个简单易用的脚本来生成凭据文件。然后这些凭证以json格式存储。
manage-oracle-creds
如果要直接编辑凭据文件,则其格式如下。
{"<cred_set_name>":{"HOST":"<host>","PORT":<port>,"SID":"<sid>","USERNAME":"<username>","PASSWORD":"<password>",},"<cred_set_name>":{...}...}
导入以供使用
主类是pdconnection类。pdconnection类还使用了一个credentials类,但除非需要更精细的控制,否则不需要导入该类。
fromepltoolsetimportCredentials,PdConnection
使用pdconnection类
实例化对象
可以创建连接对象,然后通过方法检查对象的可行性
# Instantiate a connection objectcn=PdConnection(cred_set="TEST_SPOT",cred_file='.connectcreds.creds')# Check everything is in orderifcn.cred_file_exists():print("Specified Credential File Exists")ifcn.cred_set_exists():print("Specified Credential File Exists")cn.load_cred_set()ifcn.can_connect():print("Tested that connection Possible")
Specified Credential File Exists Specified Credential File Exists Tested that connection Possible
方法链接还可以用于实例化和加载凭据,或将许多其他方法链接在一起。
cn=PdConnection(cred_set="TEST_SPOT").load_cred_set()
在数据帧中查询Oracle表
可以将sql查询直接带到pandas数据帧,而不必担心实现这一点所需的cx或lce连接。
df=cn.sql_to_dataframe(sql="SELECT * FROM example_table_rr")print(df.head())
FAVORITE_PASTRY MEMBERDATE PERSONID SCORE 0 muffin 23-NOV-15 13.2 1 scone 13-SEP-12 22.3 2 bagel 03-FEB-16 31.2 3 donut 01-DEC-05 40.4 4 cookies 05-NOV-17 54.0
将数据帧放入Oracle表中
在执行了所需的任何操作之后,可以使用相同的连接对象将数据帧放回数据库中。如果表已经存在,则表创建将失败,除非指定要丢弃它。
cn.df_to_table(df,"example_table_post_trans_rr",drop_existing=True)
其他一些东西
也可以执行泛型sql
cn.execute_sql(some_sql_string)
其他函数可以用于对连接进行更精细的控制,尽管不是必需的,因为如果个别方法需要,连接将被关闭和打开。
cn.make_connection()cn.close_connection()cn.is_connected()
如果需要更改函数,可以直接访问连接对象的属性。其中一些值也可以在实例化时设置,或者保留为默认值。
cn.cred_set# To maybe change the database or schema with same filecn.fetch_size# integer that can affect querying performanceself.creds# epltoolset.Credentials, loaded by methodself.conn# cx_Oracle.Connection, can be kept open between uses