LOAD DATA LOCAL, How do I skip the first line? - sql

I'm trying to Load a CSV file into my MySQL database,
But I would like to skip the first line.
I fact It contains the name of my columns and no interesting data.
Here is the query I'm using:
LOAD DATA LOCAL INFILE '/myfile.csv'
INTO TABLE tableName
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
(column,column,column);

LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test IGNORE 1 LINES;
(reference)

For those curious, IGNORE N LINES should be after the separator qualifiers:
LOAD DATA LOCAL INFILE '/myfile.csv'
INTO TABLE tableName
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(column,column,column);

Try this:
IGNORE N LINES
LOAD DATA INFILE "/path/to/file.csv"
INTO TABLE MYTABLE
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

Related

How do I export data that contains tabs and other arbitrary characters from Hive to disk?

For example:
hive -e "select '\t' as a, ',' as b, 1 as c" > output.txt
The tabs are not escaped in the output file, even though they are used as a delimiter.
How can I ensure I can use a full ASCII map of characters when outputting data from Hive?
Try this answer if that helps:
select "\\t" as a, "\," as b

unload data to s3 with new line characters redshift

i am trying to export table from redshift to s3 using unload command
command :
unload ('SELECT * FROM table where id = 4') TO 's3://path/temp/table1'
credentials 'aws_access_key_id="ahsvdgsfadhsagdffjh;aws_secret_access_key=ahgsdasdhgsahdgsahdgsahdgsahgsa' delimiter '|' NULL
AS
'\\N' escape;
one of the field in my table contains new line so the csv generated breaks into two lines
is there any way to replace new line to \n or to add a end of line character
You should be removing escape, and just try ADDQUOTE Option, it will produce correct CSV. As "" will instruct your CSV reader to treat \n as verbiage rather than newline.
UNLOAD ('SELECT * FROM table where id = 4')
TO 's3://path/temp/table1'
credentials aws_access_key_id="ahsvdgsfadhsagdffjh;
aws_secret_access_key=ahgsdasdhgsahdgsahdgsahdgsahgsa'
delimiter '|'
NULL AS '\\N' ADDQUOTE;

Space in Hive output

I want my Query output whitespace separate. I just want output fields to be separated only by one space. I used the below settings but I get ABC ^A 9000. So, there is ^A in between.
How can I do one space between fields?
INSERT OVERWRITE LOCAL DIRECTORY '/home/stg/hive_training'
ROW FORMAT DELIMITED
FIELDS terminated by''
Just use space. It works.
Demo
hive
insert overwrite local directory '/tmp/test'
row format delimited
fields terminated by ' '
select 1,2,3
;
bash
</tmp/test/000000_0 od -Anone -a
1 sp 2 sp 3 nl

How to use XMLType in a SQL*Loader control file?

I have a csvfile, which contains a column of XML data. A sample record of the csv look like:
1,,,<capacidade><numfields>3</numfields><template>1</template><F1><name lang="pt">Apple</name></F1></capacidade>
I'd like to use SQL*Loader to import all the data into Oracle; I defined the ctl file as follows:
LOAD DATA
CHARACTERSET UTF8
INFILE '/home/db2inst1/result.csv'
CONTINUEIF NEXT(1:1) = '#'
INTO TABLE "TEST"."T_DATA_TEMP"
FIELDS TERMINATED BY','
( "ID"
, "SOURCE"
, "CATEGORY"
, "RAWDATA"
)
Wen running this, the error log shows that the column of RAWDATA is treated as CHARACTER data type. How can I define the RAWDATA to be a XMLType in this case so that it can be correctly insert into the Oracle?
Try something like this:
Add a comma at the end of xml so as to mark a delimiter(can't use whitespace as xml may contain spaces in between)
then create your ctl file like below
LOAD DATA
APPEND
INTO TABLE "TEST"."T_DATA_TEMP" fields OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
( ID terminated by "," ,
SOURCE terminated by ",",
CATEGORY terminated by ",",
RAWDATA char(4000) terminated by ","
)

Mapping fields in Oracle SQL Loader

When loading an external csv with Oracle SQL Loader is there a way to map the fields directly to each other in the control file?
At the moment I'm doing a simple loading, so the position of the source fields is important. Is there a way to do it otherwise? So instead of:
load data
into table1
fields terminated by "," optionally enclosed by '"'
(destination_field1, destination_field2, destination_field3)
do something like:
load data
into table1
fields terminated by "," optionally enclosed by '"'
(
source_field2 => destination_field1,
source_field1 => destination_field2,
source_field3 => destination_field3
)
Edit:
The main reason is that the order of the columns in the source file can change, therefore I can't be sure which field will be the first, second, etc.
You can include any data processing by means of Oracle functions in your control file.
E.g., this code swaps columns 1 and 2 and additionally converts source_field2 to number, silently replacing wrong values to nulls:
load data
append
into table SCHEMA.TABLE
fields terminated by ';' optionally enclosed by '"'
trailing nullcols
(
source_field1 BOUNDFILLER,
source_field2 BOUNDFILLER,
source_field3 BOUNDFILLER,
destination_field1 "to_number(regexp_substr(:source_field2, '^[-0-9,]*'),'9999999999D999','NLS_NUMERIC_CHARACTERS='', ''')",
destination_field2 ":source_field1",
destination_field3 ":source_field3"
)