I am new to hive, and want to know if there is anyway to insert data into hive table like we do in SQL. I want to insert my data into hive like
INSERT INTO tablename VALUES (value1,value2..)
I have read that you can load the data from a file to hive table or you can import data from one table to hive table but is there any way to append the data as in SQL?
Ways to insert data into hive table: for demonstration, I am using table name as table1 and table2
1)
create table table2 as select * from table1 where 1=1;
orcreate table table2 as select * from table1;
2)
insert overwrite table table2 select * from table1;
--it will insert data from one to another. Note: It will refresh the target.3)
insert into table table2 select * from table1;
--it will insert data from one to another. Note: It will append into the target.4)
load data local inpath 'local_path' overwrite into table table1;
--it will load data from local into the target table and also refresh the target table.5)
load data inpath 'hdfs_path' overwrite into table table1;
--it will load data from hdfs location iand also refresh the target table. or6)
load data local inpath 'local_path' into table table1;
--it will load data from local and also append into the target table.7)
load data inpath 'hdfs_path' into table table1;
--it will load data from hdfs location and also append into the target table.8)
insert into table2 values('aa','bb','cc');
--Lets say table2 have 3 columns only.9) Multiple insertion into hive table
Yes you can insert but not as similar to SQL.
In SQL we can insert the row level data, but here you can insert by fields (columns).
During this you have to make sure target table and the query should have same datatype and same number of columns.
eg:
Slightly better version of the unique2 suggestion is below:
Which does not require the hack with using an already exiting table.
You can use below approach. With this, You don't need to create temp table OR txt/csv file for further select and load respectively.
Where tempTable_with_atleast_one_records is any table with atleast one record.
But problem with this approach is that If you have INSERT statement which inserts multiple rows like below one.
Then, You need to have separate INSERT hive statement for each rows. See below.
You can't do insert into to insert single record. It's not supported by Hive. You may place all new records that you want to insert in a file and load that file into a temp table in Hive. Then using insert overwrite..select command insert those rows into a new partition of your main Hive table. The constraint here is your main table will have to be pre partitioned. If you don't use partition then your whole table will be replaced with these new records.
You could definitely append data into an existing table. (But it is actually not an append at the HDFS level). It's just that whenever you do a LOAD or INSERT operation on an existing Hive table without
OVERWRITE
clause the new data will be put without replacing the old data. A new file will be created for this newly inserted data inside the directory corresponding to that table. For example :I have a file named demo.txt which has 2 lines :
Create a table and load this file into it
Now,if I do a SELECT on this table it'll give me :
Suppose, I have one more file named demo2.txt which has :
And I do a LOAD again on this table without using overwrite,
Now, if I do a SELECT now, it'll give me,
HTH