我正在使用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加载数据。 –