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?
      May 23, 2019 2:47 PM IST
    0

  • 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);
      May 23, 2019 2:49 PM IST
    0
  • 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:

    CREATE TABLE test(stu_name STRING,stu_id INT,stu_marks INT)
    ROW FORMAT DELIMITED
    FIELDS TERMINATED BY ','
    STORED AS TEXTFILE;
    
    INSERT OVERWRITE TABLE test SELECT lang_name, lang_id, lang_legacy_id FROM export_table;
      January 8, 2022 3:14 PM IST
    0
  • 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; or create 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. or

      create table table2( col1 string, col2 string, col3 string) row format delimited fields terminated by ',' location 'hdfs_location';

    6. 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

      January 15, 2022 1:00 PM IST
    0
  • 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.

     
      January 17, 2022 2:05 PM IST
    0