[关闭]
@songlaf 2016-05-19T10:23:55.000000Z 字数 1545 阅读 608

作业十五、【Hive表基本操作】

北风网大数据培训


一) 使用 load 方式加载数据到 Hive 表的六种方式

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
*数据源是否在本地(hive所在的linux服务器系统)
--local
--HDFS
*是否需要覆盖目标数据表
--overwrite
--默认是append
*是否是分区表
--partition
如下的表

  1. create table user
  2. (
  3. id string,
  4. name string
  5. )
  6. partitioned by (
  7. Sex string,
  8. Age string)
  9. row format delimited fields terminated by ',';

1.1)从本地系统导入数据到Hive表

  1. load data local inpath "/home/sjf/userlist.txt" into table user partition(sex='F',age=21);

1.2)从HDFS导入数据到Hive表

  1. #上传文件到hdfs
  2. bin/hdfs dfs -put /home/sjf/userlist1.txt /input/userlist1.txt
  3. #导入文件
  4. load data inpath "/input/userlist1.txt" into table user partition(sex='F',age=22);

1.3)加载数据的时候是否覆盖

  1. load data local inpath "/home/sjf/userlist3.txt" overwrite into table user partition(sex='F',age=27);

1.4)创建表的时候用select完成

  1. create table user_backup as select * from user;

1.5)创建表的时候可以通过insert into完成

  1. create table user_temp like user;
  2. insert into table user_temp partition(sex='F',age=22) select id,name from user where age = 22 and sex='F';

1.6)通过创建表的时候location

  1. #新建一个文本文件wu.txt,内容如下:
  2. 1,诸葛亮
  3. 2,黄忠
  4. 3,糜芳
  5. 4,张苞
  6. #上传文件到hdfs
  7. bin/hdfs dfs -mkdir /user/hive/warehouse/db_s9.db/wu
  8. bin/hdfs dfs -put /home/sjf/wu.txt /user/hive/warehouse/db_s9.db/wu
  9. #创建表,指定location为/user/hive/warehouse/db_s9.db/wu
  10. create table wu
  11. (
  12. id string,
  13. name string
  14. )
  15. row format delimited fields terminated by ','
  16. location '/user/hive/warehouse/db_s9.db/wu';

查询结果
kkkkkk.jpg-41.9kB

二)保存查询结果

2.1)导出到本地文件系统

  1. insert overwrite local directory '/home/sjf/wu' row format delimited fields terminated by ',' select * from wu ;

查看结果
mo.jpg-18.3kB
(2)导出到HDFS

  1. #建立到处目录
  2. bin/hdfs dfs -mkdir /output1
  3. #导出数据
  4. insert overwrite directory '/output1' select * from wu;

查看结果
256.jpg-79.3kB
(3)hive -e
hive -e 'select * from wu' >> 1.txt

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注