Hive是一个基于Hadoop的数据仓库工具,可以用于对存储在Hadoop 文件中的数据集进行数据整理、特殊查询和分析处理。

1.下载安装文件

http://mirror.bit.edu.cn/apache/hive/

#下载Hive安装文件

hadoop@dblab:/usr/local$ sudo wget http://mirror.bit.edu.cn/apache/hive/hive-1.2.2/apache-hive-1.2.2-bin.tar.gz 

#解压

hadoop@dblab:/usr/local$ sudo tar -zxvf apache-hive-1.2.2-bin.tar.gz -C ./

#将文件夹名改为hive

hadoop@dblab:/usr/local$ sudo mv apache-hive-1.2.2-bin  hive

#修改文件权限 

hadoop@dblab:/usr/local$ sudo chown -R hadoop:hadoop hive

2.配置环境变量

hadoop@dblab:/usr/local$ vim ~/.bashrc

export HIVE_HOME=/usr/local/hive

export PATH=$PATH:$HIVE_HOME/bin

hadoop@dblab:/usr/local$ source ~/.bashrc

3.启动Hive

hadoop@dblab:/usr/local/hive/conf$ cd /usr/local/hadoop/

hadoop@dblab:/usr/local/hadoop$ ./sbin/start-dfs.sh

hadoop@dblab:/usr/local/hadoop$ cd /usr/local/hive/

hadoop@dblab:/usr/local/hive$ ./bin/hive

hive> 

4.Hive基本操作

1)创建数据库Hive

hive> create database hive;

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database hive already exists

 hive> create database if not exists hive;

OK

hive> use hive;

OK

2)创建表

hive> create table if not exists usr(id bigint,name string,age int);

OK

hive> create table if not exists hive.usr(id bigint,name string,age int)

    > location '/usr/local/hive/warehouse/hive/usr';

OK

Time taken: 0.467 seconds

hive> create external table if not exists hive.usr(id bigint,name string,age int)

    > row format delimited fields terminated by ','

    > location '/usr/local/data';

OK

Time taken: 0.079 seconds

#在hive数据库中创建分区表usr1,通过复制usr得到

hive> create table if not exists user1 like usr;

OK

hive> show tables;

OK

user1

usr

3)创建视图

hive> create view little_usr as select id,age from usr;

4)修改数据库

hive> alter database hive set dbproperties('edit=by'='lily');

5)查看数据库

hive> show databases;

default

hive

6)查看表和视图

hive> use hive;

hive> show tables;

little_usr

user1

usr

7)描述数据库

hive> describe database hive;

hive            hdfs://localhost:9000/user/hive/warehouse/hive.db       hadoop  USER

hive> describe database extended hive;

hive            hdfs://localhost:9000/user/hive/warehouse/hive.db       hadoop  USER    {edit=by=lily}

8)描术表和视图

hive> describe hive.usr;

id              bigint                                      

name            string                                      

age            int                                         

hive> describe hive.little_usr;

id            bigint                                      

age           int    

9)修改表

#修改表名

hive> alter table usr rename to users;

hive> alter table usr add columns(sex boolean);

10)删除视图

hive> drop view if exists little_usr;

11)删除表

hive> drop table if exists usr;

12)删除数据库

hive> drop database hive;

13)退出Hive

hive> quit;

hive> exit;