简体   繁体   中英

How to make a query for two tables in two different servers (python)?

I'm stuck a bit with a following problem:

I have two different servers within separated tables: Oracle with table "A" and PostgreSQL with table "B".

I`m trying to make a program via python, which joins these two tables and writes the result into csv file.

What is the best way to do it? (Importing a table from one db to another? Making a JOIN outside the db?)

Would be happy for your help!

import cx_Oracle
from sqlalchemy import create_engine
import pandas as pd

engineORACLE = create_engine('oracle://user:password@ip:1521/ORACLE_SERVIVE_NAME')
enginePOSTGRE = create_engine('postgresql://user@lip:5432/mydb')

df1 = pd.read_sql_query('select * from tableA', con=engineORACLE)    
df2 = pd.read_sql_query('select * from TableB',con=enginePOSTGRE)

dfcombined = df1.merge(df2, on='blabla', how='left') # for left outer join, you can also do, 'right', 'outer' or 'inner' (change 'blabla' with the key!)

Something like this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM