首页 > 代码库 > Hive安装和基础使用

Hive安装和基础使用

1、安装JDK并设置环境变量

2、上传安装包

3、解压

4、设置环境变量
# vi ~/.bash_profile或vi /etc/profile
5、进入hive shell
# hive shell

# hive

6、常见操作

查看数据库清单
hive> show databses;

查看表清单
hive> show tables;
查看表结构
hive> desc table_name;
创建数据库,location为hdfs中的路径为hdfs中的路径,不存在的目录会自动创建。
hive> create external table test(id int,name string)
    > comment ‘this is a test table‘
    > row format delimited fields terminated by ‘\t‘
    > stored as textfile
    > location ‘/data/text/test.txt‘;
hive> desc test1;
hive> select * from test1;

建议表名和数据文件名一致。
/*************************************************************************************
create external table stu(id int,name string)
comment ‘this is a test table‘
row format delimited fields terminated by ‘\t‘
stored as textfile
location ‘/data/test/stu.txt‘;
*************************************************************************************/

创建数据库后可以快速上传数据,其中/home/hadoop/filelx/test.txt中数据是以tab分割的,列数与创建的表一致。
# hadoop dfs -put /home/hadoop/filelx/test.txt /data/test/test.txt

追加载入
hive> load data local inpath ‘/home/hadoop/filelx/test.txt‘ into table test1;
覆盖载入
hive> load data local inpath ‘/home/hadoop/filelx/test.txt‘ overwrite into table test1;


建立分区表:
hive> create table t1(id int,name string ) partitioned by (hiredate string) row format delimited fields terminated by ‘,‘;
hive> create table test1(id int,name string ) partitioned by (dname string) row format delimited fields terminated by ‘,‘ stored as textfile;
hive> load data local inpath ‘/home/hadoop/filelx/1.txt‘ overwrite into table test1 partition(dname=‘manager‘);
hive> load data local inpath ‘/home/hadoop/filelx/2.txt‘ overwrite into table test1 partition(dname=‘developer‘);
查看分区
hive> show partition test1;
hive> select * from test1 where dname=‘dev‘;
hive> select * from test1 where dname=‘manager‘;

hive> insert overwrite table t1 partition(hiredate=‘20140707‘) select id,name from test1 where dname=‘develop
hive> show partition t1;