2017-08-10 356 views
0

我正在使用Python 3.6遍歷文件夾結構,並返回所有這些CSV的文件路徑,我想將其導入到兩個已創建的Oracle表中。將CSV導入Oracle表(Python)

con = cx_Oracle.connect('BLAH/[email protected]:666/BLAH') 

#Targets the exact filepaths of the CSVs we want to import into the Oracle database 
if os.access(base_cust_path, os.W_OK): 
    for path, dirs, files in os.walk(base_cust_path): 
     if "Daily" not in path and "Daily" not in dirs and "Jul" not in path and "2017-07" not in path: 
      for f in files: 
       if "OUTPUT" in f and "MERGE" not in f and "DD" not in f: 
        print("Import to OUTPUT table: "+ path + "/" + f) 
        #Run function to import to SQL Table 1 
       if "MERGE" in f and "OUTPUT" not in f and "DD" not in f: 
        print("Import to MERGE table: "+ path + "/" + f) 
        #Run function to import to SQL Table 2 

前一段時間我可以使用PHP來生產所使用的BULK INSERT SQL命令爲SQL Server的功能:

function bulkInserttoDB($csvPath){ 
    $tablename = "[DATABASE].[dbo].[TABLE]"; 
    $insert = "BULK 
       INSERT ".$tablename." 
       FROM '".$csvPath."' 
       WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\\n')"; 

    print_r($insert); 
    print_r("<br>"); 

    $result = odbc_prepare($GLOBALS['connection'], $insert); 
    odbc_execute($result)or die(odbc_error($connection)); 
} 

我一直在尋找複製此爲Python,但也有少數Google搜索讓我相信Oracle沒有「BULK INSERT」命令。這個BULK INSERT命令有很棒的性能。

由於我加載的這些CSV數據量很大(2GB x 365),因此性能至關重要。做這件事最有效的方法是什麼?

+0

你可以考慮使用[sql * loader](https://stackoverflow.com/a/6198961/322909)+ python的Popen。 – John

+0

我同意,使用Oracle Data Pump加載數據。 –

回答

0

批量插入使用cx_oracle庫和命令

con = cx_Oracle.connect(CONNECTION_STRING) 
cur= con.cursor() 
cur.prepare("INSERT INTO MyTable values (
        to_date(:1,'YYYY/MM/DD HH24:MI:SS'), 
        :2, 
        :3, 
        to_date(:4,'YYYY/MM/DD HH24:MI:SS'), 
        :5, 
        :6, 
        to_date(:7,'YYYY/MM/DD HH24:MI:SS'), 
        :8, 
        to_date(:9,'YYYY/MM/DD HH24:MI:SS'))" 
      ) ##prepare your statment 
list.append((sline[0],sline[1],sline[2],sline[3],sline[4],sline[5],sline[6],sline[7],sline[8])) ##prepare your data 
cur.executemany(None, list) ##insert 

你準備一個INSERT語句做。然後你存儲你的文件和你的列表。最後你執行很多。它會使一切癱瘓。

+0

這更多的是我希望找到的東西。現在就試試這個。感謝'它會癱瘓所有'的頭 - 在嘗試之前會先嚐試幾個CSV。 – fila

+0

但是說實話,我認爲使用像sqlLoader這樣的Oracle工具可以提高性能...... – Steven

+0

使用executemany(),查看cx_Oracle batcherrors功能來幫助診斷無效數據問題。 –