Daten per Bulk Load laden (CSV / aus Datei)
Oracle
-- SQL*Loader (from the shell)
sqlldr app_user/password control=customer.ctl log=load.log
-- Contents of customer.ctl:
-- LOAD DATA INFILE 'customer.csv'
-- INTO TABLE customer FIELDS TERMINATED BY ','
-- (id, name, email)
-- Alternative: with an external table
CREATE TABLE ext_customer (id NUMBER, name VARCHAR2(100))
ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY dir_csv
ACCESS PARAMETERS (FIELDS TERMINATED BY ',') LOCATION ('customer.csv'));Das gängigste Tool ist SQL*Loader (mit Control File). Um die Datei wie eine Abfrage zu lesen, eine External Table verwenden. Für große Mengen ist Direct Path Load (DIRECT=TRUE) schnell.
PostgreSQL
-- Server side (file on the server; requires superuser/pg_read_server_files)
COPY customer (id, name, email) FROM '/data/customer.csv' WITH (FORMAT csv, HEADER true);
-- Client side (psql, file on the client)
\copy customer (id, name, email) FROM 'customer.csv' WITH (FORMAT csv, HEADER true)COPY liest eine Datei auf dem Server und erfordert Berechtigungen; psql \copy liest eine Client-Datei und braucht keine zusätzlichen Berechtigungen. Es ist der schnellste Weg, CSV zu laden.
MySQL
-- Server side
LOAD DATA INFILE '/data/customer.csv' INTO TABLE customer
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
-- From a client file
LOAD DATA LOCAL INFILE 'customer.csv' INTO TABLE customer
FIELDS TERMINATED BY ',' IGNORE 1 LINES;LOAD DATA INFILE ist der schnellste Weg für Bulk Loads. LOCAL liest eine Client-Datei und muss sowohl auf dem Server (local_infile=ON) als auch auf dem Client aktiviert sein. secure_file_priv beschränkt den Serverpfad.
SQL Server
-- T-SQL BULK INSERT
BULK INSERT customer FROM 'C:\data\customer.csv'
WITH (FORMAT='CSV', FIRSTROW=2, FIELDTERMINATOR=',', ROWTERMINATOR='0x0a');
-- bcp from the shell
bcp mydb.dbo.customer in customer.csv -c -t, -S server -TBULK INSERT liest die Datei aus einem Pfad, auf den das Dienstkonto von SQL Server zugreifen kann. bcp ist ein Kommandozeilentool; -c ist der Zeichenmodus, -t das Feldtrennzeichen. Für maximale Geschwindigkeit mit TABLOCK Minimal Logging anstreben.
MongoDB
# Import CSV / JSON (from the shell)
mongoimport --db=shop --collection=customer --type=csv --headerline --file=customer.csv
# For a JSON array
mongoimport --db=shop --collection=customer --file=customer.json --jsonArraymongoimport lädt CSV/TSV/JSON; --headerline behandelt die erste Zeile als Feldnamen. Um vorhandene Dokumente zu aktualisieren, --mode=upsert --upsertFields verwenden. Bei großen Daten beschleunigt --numInsertionWorkers den Vorgang.
Redis
# Fastest: feed commands in protocol format and pipe them
cat commands.txt | redis-cli --pipe
# Example lines in commands.txt:
# SET customer:1 "Alice"
# SET customer:2 "Bob"Für Bulk Loads redis-cli --pipe (Mass Insert) verwenden; sehr schnell, da Befehle im RESP-Protokollformat über eine einzige Verbindung gestreamt werden. Alternativ werden Befehle per Pipelining gruppiert.
Elasticsearch
# _bulk API (NDJSON: an action line + a source line per document)
curl -s -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/customer/_bulk' --data-binary @data.ndjson
# Contents of data.ndjson:
# { "index": {} }
# { "id": 1, "name": "Alice" }
# { "index": {} }
# { "id": 2, "name": "Bob" }Die _bulk-API nimmt NDJSON entgegen (pro Dokument eine Aktionszeile + eine Datenzeile; die Datei muss mit \n enden). Für CSV Logstash, Filebeat oder elasticdump verwenden. Das Deaktivieren von refresh_interval während des Ladens beschleunigt den Vorgang.