LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename
[PARTITION (partcol1=val1, partcol2=val2 ...)]
用户在hive上建external表,建表的同时指定hdfs路径,在数据拷贝到指定hdfs路径的同时,也同时完成数据插入external表。
$hadoop fs -cat /data/test/test.txt
1 hello
2 world
3 test
4 case
#字段之间以'\t'分割
#建external表:
hive> CREATE EXTERNAL TABLE MYTEST(num INT, name STRING)
> COMMENT 'this is a test'
> ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
> STORED AS TEXTFILE
> LOCATION '/data/test';
#建表:
hive> CREATE TABLE MYTEST2(num INT, name STRING)
> COMMENT 'this is a test2'
> ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
> STORED AS TEXTFILE;
#导数据入表:
hive> LOAD DATA LOCAL INPATH '/home/work/test.txt' INTO TABLE MYTEST2;
#这种方式导入的本地数据可以是一个文件,一个文件夹或者通配符,需要注意的是,如果是文件夹,文件夹内不能包含子目录,同样,通配符只能通配文件。
hive> CREATE TABLE MYTEST3(num INT, name STRING)
> COMMENT "this is a test3"
> ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
> STORED AS TEXTFILE;
hive> LOAD DATA INPATH '/data/test/test.txt' INTO TABLE MYTEST3;
hive> select * from MYTEST3 ;
hive> CREATE EXTERNAL TABLE MYTEST4(num INT) ;
hive> FROM MYTEST3 test3
> INSERT OVERWRITE TABLE MYTEST4
> select test3.num where name='world';
喜欢 (157)or分享 (0)