ParseException Missing TABLE - csv

I created mytable table in mydb, and when I do show tables on that db I see mytable.
However, when I do:
LOAD DATA INPATH '/my/hdfs/path/myfile.csv' OVERWRITE INTO mydb.mytable;
I get
ParseException line 1:73 missing TABLE at 'mydb' near '<EOF>'
I can do a describe on the table, and I see my headers.
But how do I get the data from myfile.csv into the Hive table?

In your query TABLE key is missing.So,instead of
LOAD DATA INPATH '/my/hdfs/path/myfile.csv' OVERWRITE INTO mydb.mytable;
use this---->
use mydb;
LOAD DATA INPATH '/my/hdfs/path/myfile.csv' OVERWRITE INTO TABLE mytable;

Related

LOAD DATA INFILE and spatial data

On MySQL 5.7, I used to populate the "point" column of a table with LOAD DATA INFILE and a SET clause, i.e.
LOAD DATA INFILE 'myfile.txt'
REPLACE INTO TABLE mytable
(#x, #y)
SET geom = Point(#x, #y);
This worked just fine.
I upgraded to MySQL 8.0.12, and I now get an error ERROR 1364 (HY000): Field 'geom' doesn't have a default value.
Has something changed in the way LOAD DATA INFILE handles SET clauses ?
Thanks !
This bug has been confirmed in 8.0.12.
The only workaround I found was :
Drop the geom column
Load data into the (x, y) columns
Recreate the geom column without a not null constraint
Update the geom column
Alter the column to add the not null constraint
Simply method is to create table without spatial (point/geo) fields and load data as text, next load data from this (temporary) table to target table like below:
load data to temporary table with simply types of columns
insert to target table from temporary table converting needed columns to spatial.

IF ELSE inside Load data infile mysql

I am trying to insert some data using Load data infile into a mysql table that already has some data. The table contains id and name. My csv file contains three fields: id, name and code. The table schema also has these three fields, but currently, the table has NULL for the code field. I am trying to insert the code from csv to an existing row in the table if it matches the name, else I am trying to insert a complete new row.
The code I have tried is as follows:
LOAD DATA INFILE 'table1.csv'
INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#code, #name, #other_columns)
IF EXISTS (SELECT * FROM table1 where name=#name);
BEGIN
set code=#Code;
END
ELSE
BEGIN
set code=#Code, name=#name;
END
By doing so, I am getting a mysql syntax error, but am unable to figure it out. Can anyone point me in the right direction, or suggest to me another approach? I have thousands of new rows to insert and thousands of existing rows to modify based on the certain field, (name in this case).
MySQL does not allow the LOAD DATA INFILE statement inside a stored program, which is where the IF statement appears. Break up your task into two parts. First, LOAD DATA INFILE into a temporary table. Then create a stored program that replaces the loaded data into your table1 from the temporary table.

LOAD DATA INFILE doesn't work with 1265 warning code

I want to use LOAD DATA INFILE command of MYSQL to insert about 900,000 row to my table called INFO of a database called A.
the INFO tables is defined like below:
create table INFO (id varchar(10) PRIMARY KEY, receive_time datetime,
check_time datetime, ver varchar(10) NOT NULL)
before inserting in this table, just for testing, I create another database B, and create a table with the same definition of INFO called TMP_INFO. I run this query on the temporary database B and table TMP_INFO and it works:
LOAD DATA INFILE '/home/develop/list.txt' IGNORE INTO TABLE TMP_INFO
LINES TERMINATED BY '\n' IGNORE 1 LINES (id, #receive_time, #check_time, #ver)
set receive_time=NOW(), check_time=NOW(), ver='8754';
the list.txt file is similar to this :
8754
0087653565
0000986759
0000654327
0000453219
but the problem is that when I run the same query on the database A and INFO table, with the same list.txt file, it doesn't work. I get no error, but N warnings (N = number of lines in line.txt - 1 )!!! all the warnings are the same showed bellow:
warning code 1265:Data truncated for column 'id' at row ...
I am going to use this query within a C program.
I am confused, and don't have any idea, Any Idea please?

insert and edit in mysql rows from a file with bash

i am using bash to load a file in mysql, and i have:
mysql --local-infile=1 -u user data_base_1 < file.sql
and file.sql is :
..$ cat file.sql
load data local infile '/folder/load.csv' into table table_1 fields terminated by '|'
The code works fine.
The problem is that if the PK of one row in the file exist, the row is not inserted, and i need if the row exist insert and replace the row in the table. How can i do it?
Who can help me?
Thanks
You can specify REPLACE with LOAD DATA:
LOAD DATA LOCAL INFILE '/folder/load.csv' REPLACE INTO TABLE table_1 FIELDS TERMINATED BY '|'
Or else use the mysqlimport --replace option.
http://dev.mysql.com/doc/refman/5.6/en/mysqlimport.html#option_mysqlimport_replace
You could load into a temporary table and then execute two SQL statements:
UPDATE table
WHERE ... (match found)
;
INSERT into table(...)
SELECT ...
FROM temp_table
WHERE NOT EXISTS(...)

export table from one DB and import/merge into another DB with phpMyAdmin

I have a DB1 with a table called 'mytable' and i want to export this with all it's data into DB2 which already has a table called 'mytable'.
So basically merge the data together. But can seem to do this, if i export and import i always get the following error
Table 'mytable' already exists.
Whats the best way to do this.
Thanks
Ben
Using phpmyadmin -
DB1 -> goto table -> Export table
DB2 -> goto table -> Import Table
Reason:
Exported sql file contains -
CREATE TABLE IF NOT EXISTS table_name ( ...
And
INSERT INTO table_name ( ...
Hence, if table already exists, new rows will simply be inserted into the table.