hive> load data local inpath 'wyp.txt'into table wyp; Copying data fromfile:/home/wyp/wyp.txt Copying file: file:/home/wyp/wyp.txt Loading data to table default.wyp Table default.wyp stats: [num_partitions: 0, num_files: 1, num_rows: 0, total_size: 67] OK Time taken: 5.967 seconds
hive> load data inpath '/home/wyp/add.txt' into table wyp; Loading data to table default.wyp Table default.wyp stats: [num_partitions: 0, num_files: 2, num_rows: 0, total_size: 215] OK Time taken: 0.47 seconds
hive> select * from wyp; OK 5 wyp1 23131212121212 6 wyp2 24134535353535 7 wyp3 25132453535353 8 wyp4 26154243434355 1 wyp 2513188888888888 2 test 3013888888888888 3 zs 34899314121 Time taken: 0.096 seconds, Fetched: 7 row(s)
从上面的执行结果我们可以看到,数据的确导入到wyp表中了!请注意load data inpath ‘/home/wyp/add.txt’ into table wyp;里面是没有local这个单词的,这个是和一中的区别。
hive> create table test( > id int, name string > ,tel string) > partitioned by > (age int) > ROW FORMAT DELIMITED > FIELDS TERMINATED BY '\t' > STORED AS TEXTFILE; OK Time taken: 0.261 seconds
下面语句就是将wyp表中的查询结果并插入到test表中: hive> insert into table test > partition (age='25') > select id, name, tel > from wyp; ##################################################################### 这里输出了一堆Mapreduce任务信息,这里省略 ##################################################################### Total MapReduce CPU Time Spent: 1 seconds 310 msec OK Time taken: 19.125 seconds
1 2 3 4 5 6 7 8 9 10
hive> select * from test; OK 5 wyp1 13121212121225 6 wyp2 13453535353525 7 wyp3 13245353535325 8 wyp4 15424343435525 1 wyp 1318888888888825 2 test 1388888888888825 3 zs 89931412125 Time taken: 0.126 seconds, Fetched: 7 row(s)
这里做一下说明: 我们知道我们传统数据块的形式insert into table values(字段1,字段2),这种形式hive是不支持的。
hive> set hive.exec.dynamic.partition.mode=nonstrict; hive> insert into table test > partition (age) > select id, name, > tel, age > from wyp; ##################################################################### 这里输出了一堆Mapreduce任务信息,这里省略 ##################################################################### Total MapReduce CPU Time Spent: 1 seconds 510 msec OK Time taken: 17.712 seconds
hive> select * from test; OK 5 wyp1 13121212121223 6 wyp2 13453535353524 7 wyp3 13245353535325 1 wyp 1318888888888825 8 wyp4 15424343435526 2 test 1388888888888830 3 zs 89931412134 Time taken: 0.399 seconds, Fetched: 7 row(s)
hive> insert overwrite table test > PARTITION (age) > select id, name, tel, age > from wyp;
更可喜的是,Hive还支持多表插入,什么意思呢?在Hive中,我们可以把insert语句倒过来,把from放在最前面,它的执行效果和放在后面是一样的,如下: hive> showcreatetable test3; OK CREATETABLE test3( id int, name string) Time taken: 0.277 seconds, Fetched: 18row(s)
hive> from wyp > insertintotable test > partition(age) > select id, name, tel, age > insertintotable test3 > select id, name > where age>25;
hive> select * from test3; OK 8 wyp4 2 test 3 zs Time taken: 4.308 seconds, Fetched: 3row(s)