2017-08-10 15 views
0

私は、Python 3.6を使用してフォルダ構造を反復し、これらのCSVのファイル・パスを、すでに作成された2つの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 

しばらく前に私は、SQL ServerのBULKのINSERTのSQLコマンドを使用する機能を生成するためにPHPを使用することができました:

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×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機能を見ています。 –

関連する問題