我正在使用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),因此性能至關重要。做這件事最有效的方法是什麼?
你可以考慮使用[sql * loader](https://stackoverflow.com/a/6198961/322909)+ python的Popen。 – John
我同意,使用Oracle Data Pump加載數據。 –