Hive insert query like SQL

2019-01-21 09:01发布

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?

13条回答
啃猪蹄的小仙女
2楼-- · 2019-01-21 09:35

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

查看更多
神经病院院长
3楼-- · 2019-01-21 09:43

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;
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-21 09:46

Slightly better version of the unique2 suggestion is below:

insert overwrite table target_table
select * 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.

查看更多
Evening l夕情丶
5楼-- · 2019-01-21 09:46

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.

INSERT INTO TABLE tablename SELECT value1,value2 FROM tempTable_with_atleast_one_records LIMIT 1.

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.

INSERT INTO yourTable values (1 , 'value1') , (2 , 'value2') , (3 , 'value3') ;

Then, You need to have separate INSERT hive statement for each rows. See below.

INSERT INTO TABLE yourTable SELECT 1 , 'value1' FROM tempTable_with_atleast_one_records LIMIT 1;
INSERT INTO TABLE yourTable SELECT 2 , 'value2' FROM tempTable_with_atleast_one_records LIMIT 1;
INSERT INTO TABLE yourTable SELECT 3 , 'value3' FROM tempTable_with_atleast_one_records LIMIT 1;
查看更多
【Aperson】
6楼-- · 2019-01-21 09:46

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.

查看更多
何必那么认真
7楼-- · 2019-01-21 09:48

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 :

ABC
XYZ

Create a table and load this file into it

hive> create table demo(foo string);
hive> load data inpath '/demo.txt' into table demo;

Now,if I do a SELECT on this table it'll give me :

hive> select * from demo;                        
OK    
ABC    
XYZ

Suppose, I have one more file named demo2.txt which has :

PQR

And I do a LOAD again on this table without using overwrite,

hive> load data inpath '/demo2.txt' into table demo;

Now, if I do a SELECT now, it'll give me,

hive> select * from demo;                       
OK
ABC
XYZ
PQR

HTH

查看更多
登录 后发表回答