Hive insert query like SQL Hive insert query like SQL hadoop hadoop

Hive insert query like SQL


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);


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 ASSELECT 1 AS oneFROM any_table_in_your_databaseLIMIT 1;

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

CREATE TABLE my_table ASSELECT 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_tableSELECT stack(2  , "row4", 1  , "row5", 2) AS (column1, column2)FROM one;


Slightly better version of the unique2 suggestion is below:

insert overwrite table target_tableselect * from (select stack(    3,                 # generating new table with 3 records    'John', 80,        # record_1    'Bill', 61         # record_2    'Martha', 101      # record_3    ) ) s;

Which does not require the hack with using an already exiting table.