I want to explicit list the columns in importTable for LOAD DATA with mysqlsh:
https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-utilities-parallel-table.html
import.js:
util.importTable("sample.csv", {schema: "myschema", table: "mytable",
showProgress: true, columns: ["id","firstname",@dummy,@dummy,"lastname"]});
My csv contains 5 columns. But if my final table only contains id, firstname, lastname, I have to find a way to skip two columns of the source csv,
Import with:
mysqlsh --user=root --password='rootpw' --socket=/var/run/mysqld/mysqld.sock < import.js
Problem: SyntaxError: Invalid or unexpected token at STDIN:2:347. Neither @dummy nor '@dummy' nor "@dummy" works.
Question: how can I skip dummy columns? When using LOAD DATA it would work with using @dummy in the column options.
tl;dr
Since MySQL Shell 8.0.22 you can skip columns by replacing column name in
columnsoption with integer value.which is equivalent to:
MySQL Shell >= 8.0.22
MySQL Shell’s parallel table import utility util.importTable() since version 8.0.22 has a new option
decodeColumns, and an enhancement to thecolumnsoption, to enable you to capture columns from the import file for input preprocessing (or to discard them) in the same way as with aLOAD DATAstatement. ThedecodeColumnsoption specifies preprocessing transformations for the captured data in the same way as theSETclause of aLOAD DATAstatement, and assigns them to columns in the target table.util.importTablecolumnoption was extended and now is accepting integers as array value. Integer value passed ask-th argument, capturesk-th column in input file as user variable named@i, whereiis an passed integer.Captured column value binded to user variable can be used in
decodeColumnsoption to preprocess and transform data before assigning the result to the column in table.Example 1 - Preprocess column2:
is equivalent to:
Example 2 - Skip columns:
is equivalent to:
Example 3 - Generate values for columns
is equivalent to:
8.0.17 <= MySQL Shell < 8.0.22
Unfortunately, currently
util.importTablein MySQL Shell 8.0.18 do not support user variables passed to columns options.You can workaround this limitation by ALTER TABLE with missing columns that exists in import data file with proper column type:
import your data e.g.
/tmp/names.csv[1]and drop
dummycolumns:or simply invoke LOAD DATA LOCAL INFILE SQL command:
[1] Sample data
/tmp/names.csv