Load Data from Local Files
On this page
To load data from local files, enable LOCAL INFILE by starting the SingleStore client with the --local-infile option.LOAD DATA LOCAL INFILE.
The basic format for loading data into a table using local file is:
LOAD DATA LOCAL INFILE '/example-directory/<file_name>'
INTO TABLE <table_name>
COLUMNS TERMINATED BY ',';Example Using a Local CSV File
Using this table format:
CREATE TABLE employees(emp_id INT,emp_lastname VARCHAR(25),emp_firstname VARCHAR(25),emp_title VARCHAR(25),dept VARCHAR(25),emp_city VARCHAR(25),emp_state VARCHAR(5),emp_ccode VARCHAR(4));
Load data from a local file location:
LOAD DATA LOCAL INFILE '/tmp/emp_data.csv'INTO TABLE employeesFIELDS TERMINATED BY ','ENCLOSED BY '"';
Verify the file loaded correctly:
SELECT * FROM employees;
+--------+--------------+---------------+---------------+-----------+--------------+-----------+-----------+
| emp_id | emp_lastname | emp_firstname | emp_title | dept | emp_city | emp_state | emp_ccode |
+--------+--------------+---------------+---------------+-----------+--------------+-----------+-----------+
| 102 | Karras | Damien | Doctor | R&D | NYC | NY | US |
| 110 | Strode | Laurie | VP Sales | Sales | Haddonfield | IL | US |
| 89 | Whateley | Wilbur | CEO | Sen_Mgmt | Dunwich | MA | US |
| 817 | Eriksson | Oskar | Support | IT | Stockholm | NULL | SE |
| 298 | Denbrough | Bill | Salesperson | Sales | Bangor | ME | US |
| 399 | Torrance | Jack | PR Dir | PR | Estes Park | CO | US |
| 410 | Wilkes | Annie | HR Mgr | HR | Silver Creek | CO | US |
| 312 | Cady | Max | IT Dir | IT | New Essex | FL | US |
| 691 | Grevers | Nick | Rep | PR | Grimnetz | NULL | CH |
| 14 | Bateman | Patrick | Prod_Mgr | prod_dev | NYC | NY | US |
| 75 | White | Carrie | Receptionist | HR | Chamberlain | ME | US |
| 263 | MacNeil | Regan | R&D Mgr | R&D | Washington | DC | US |
+--------+--------------+---------------+---------------+-----------+--------------+-----------+-----------+Stream Data with LOAD DATA LOCAL INFILE
Instead of loading data from a file on the local filesystem, applications can stream data directly from memory into LOAD DATA LOCAL INFILE.
-
Data is generated programmatically (for example, by an ETL script) and would otherwise need to be written to a temporary file first.
-
Data is produced incrementally from another source (for example, another database, a queue, or a stream processor) and must be loaded without buffering the full dataset on disk.
-
The application already has the rows in memory and does not need file-based persistence.
Compared with multi-row INSERT, streaming can reduce client and network overhead by avoiding the construction and transmission of a large SQL statement.LOAD DATA can load the data with a single statement, whereas INSERT statements may need to be split into multiple statements because the SQL query size is limited by max_.
Streaming uses the same MySQL protocol mechanism as file-based LOAD DATA LOCAL INFILE, SingleStore requests the file contents from the client, and the client responds with in-memory bytes.LOAD DATA clauses (FIELDS TERMINATED BY, ENCLOSED BY, SET, SKIP DUPLICATE KEY ERRORS, FORMAT AVRO, FORMAT JSON, FORMAT PARQUET, etc.
The application must use a SingleStore client library that supports stream-based LOAD DATA LOCAL INFILE.
-
SingleStore Python client (
singlestoredb)
Each supported client provides its own mechanism for attaching an in-memory stream to LOAD DATA LOCAL INFILE.
Supported clients and connectors expose stream-based LOAD DATA LOCAL INFILE through their own APIs:
-
SingleStore Python client (
singlestoredb): Use:stream:as theINFILEvalue and pass the in-memory iterator or stream through theinfile_argument.stream -
SingleStore JDBC driver: When an input stream is registered with
Statement.), the filename is ignored; no specific filename format is required.setNextLocalInfileInputStream(InputStream -
SingleStore .
NET connector: Use SingleStoreBulkLoaderto load the stream.A filename is not required. -
SingleStore Go driver: Use
Reader::<name>, where<name>is the exact string passed toRegisterReaderHandler.
Refer to the documentation for the specific client or connector for the API used to attach the in-memory stream.
For example, the following example uses the SingleStore Python client to stream CSV data generated dynamically into a table:
import singlestoredb as s2conn = s2.connect('user:password@host:3306/my_db?local_infile=true')cur = conn.cursor()cur.execute('CREATE TABLE generator (a INT, b INT, c INT)')def upload_csv():yield '1,2,3\n'yield '4,5,6\n'yield '7,8,9\n'# Chunks do not need to align to row boundaries.yield '10,11,12\n13,14,15\n'cur.execute("""LOAD DATA LOCAL INFILE ':stream:' INTO TABLE generatorFIELDS TERMINATED BY ',' ENCLOSED BY '"'""",infile_stream=upload_csv(),)
The generator streams chunks to the server.\n in this example), not by chunk boundaries.
For patterns where the data producer runs in a different thread from the SQL client, use a queue.
import singlestoredb as s2from queue import Queuefrom threading import Threadconn = s2.connect('user:password@host:3306/my_db?local_infile=true')cur = conn.cursor()cur.execute('CREATE TABLE queue_load (a INT, b INT, c INT)')data_feeder = Queue()t = Thread(target=cur.execute,args=("""LOAD DATA LOCAL INFILE ':stream:' INTO TABLE queue_loadFIELDS TERMINATED BY ',' ENCLOSED BY '"'""",),kwargs=dict(infile_stream=data_feeder),)t.start()data_feeder.put('1,2,3\n')data_feeder.put('4,5,6\n')data_feeder.put('7,8,9\n')data_feeder.put('10,11,12\n13,14,15\n')# An empty string ends the stream.data_feeder.put('')t.join()
Streamed data is subject to the same parsing, error handling, and load data error logging as file-based loads.ERRORS HANDLE '<handle>' and inspected in information_.
Client-side connection settings that disable LOAD DATA LOCAL INFILE (for example, local_) reject stream-based loads with the same client-side error as file-based loads.
CSV FILE DATA
Below is the contents of the csv file used in the examples above.
014, Bateman, Patrick,Prod_Mgr, prod_dev, NYC, NY, US102, Karras, Damien, Doctor,R&D, NYC, NY, US298, Denbrough, Bill, Salesperson,Sales, Bangor, ME, US399, Torrance, Jack, PR Dir, PR,Estes Park,CO, US410, Wilkes, Annie, HR Mgr,HR,Silver Creek, CO, US110, Strode, Laurie, VP Sales,Sales, Haddonfield, IL, US312, Cady, Max, IT Dir, IT, New Essex,FL, US089, Whateley, Wilbur, CEO, Sen_Mgmt, Dunwich, MA, US075, White, Carrie, Receptionist, HR,Chamberlain, ME, US263, MacNeil, Regan, R&D Mgr,R&D, Washington, DC, US817, Eriksson, Oskar, Support, IT, Stockholm, NULL, SE691, Grevers, Nick, Rep, PR, Grimnetz, NULL, CH
Last modified: