hive的基本使用
新建表
创建数据(文本以tab分隔)
~ vi /home/cos/demo/t_hive.txt
16 2 3
61 12 13
41 2 31
17 21 3
71 2 31
1 12 34
11 2 34
创建新表
hive> CREATE TABLE t_hive (a int, b int, c int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’;
导入数据t_hive.txt到t_hive表
hive> LOAD DATA LOCAL INPATH ‘/home/cos/demo/t_hive.txt’ OVERWRITE INTO TABLE t_hive ;
查看表和数据
查看表
hive> show tables;
正则匹配表名
hive>show tables ‘t‘;
查看表数据
hive> select * from t_hive;
查看表结构
hive> desc t_hive;
修改表
增加一个字段
hive> ALTER TABLE t_hive ADD COLUMNS (new_col String);
hive> desc t_hive;
OK
a int
b int
c int
new_col string
Time taken: 0.086 seconds
重命令表名
hive>ALTER TABLE t_hive RENAME TO t_hadoop;
OK
Time taken: 0.45 seconds
hive> show tables;
OK
t_hadoop
Time taken: 0.07 seconds
删除表
hive> DROP TABLE t_hadoop;
数据导入
创建表结构
hive> CREATE TABLE t_hive (a int, b int, c int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’;
从操作本地文件系统加载数据(LOCAL)
hive> LOAD DATA LOCAL INPATH ‘/home/cos/demo/t_hive.txt’ OVERWRITE INTO TABLE t_hive ;
创建表t_hive2
hive> CREATE TABLE t_hive2 (a int, b int, c int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’;
从HDFS加载数据
hive> LOAD DATA INPATH ‘/user/hive/warehouse/t_hive/t_hive.txt’ OVERWRITE INTO TABLE t_hive2;
从其他表导入数据
hive> INSERT OVERWRITE TABLE t_hive2 SELECT * FROM t_hive ;
创建表并从其他表导入数据
hive> CREATE TABLE t_hive AS SELECT * FROM t_hive2 ;
仅复制表结构不导数据
hive> CREATE TABLE t_hive3 LIKE t_hive;
数据导出
通过Hive导出到本地文件系统
hive> INSERT OVERWRITE LOCAL DIRECTORY ‘/tmp/t_hive’ SELECT * FROM t_hive;