Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive insert query like SQL

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?

like image 922
Y0gesh Gupta Avatar asked Jul 02 '13 12:07

Y0gesh Gupta


People also ask

How do I run an insert query in Hive?

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.

Does Hive support insert?

INSERT ... VALUES, UPDATE, DELETE, and MERGE SQL statements are supported in Apache Hive 0.14 and later. The INSERT ... VALUES statement enable users to write data to Apache Hive from values provided in SQL statements. The UPDATE and DELETE statements enable users to modify and delete values already written to Hive.

Can we insert data in Hive?

Hive provides multiple ways to add data to the tables. We can use DML(Data Manipulation Language) queries in Hive to import or add data to the table. One can also directly put the table into the hive with HDFS commands. In case we have data in Relational Databases like MySQL, ORACLE, IBM DB2, etc.


2 Answers

Some of the answers here are out of date as of Hive 0.14

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-InsertingvaluesintotablesfromSQL

It is now possible to insert using syntax such as:

CREATE TABLE students (name VARCHAR(64), age INT, gpa DECIMAL(3, 2));  INSERT INTO TABLE students   VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32); 
like image 154
mattinbits Avatar answered Sep 23 '22 10:09

mattinbits


You can use the table generating function stack to insert literal values into a table.

First you need a dummy table which contains only one line. You can generate it with the help of limit.

CREATE TABLE one AS SELECT 1 AS one FROM any_table_in_your_database LIMIT 1; 

Now you can create a new table with literal values like this:

CREATE TABLE my_table AS SELECT stack(3   , "row1", 1   , "row2", 2   , "row3", 3 ) AS (column1, column2) FROM one ; 

The first argument of stack is the number of rows you are generating.

You can also add values to an existing table:

INSERT INTO TABLE my_table SELECT stack(2   , "row4", 1   , "row5", 2 ) AS (column1, column2) FROM one ; 
like image 27
unique2 Avatar answered Sep 21 '22 10:09

unique2