Hive部署
- 上传hive安装包到系统
选用0.13版本:apache-hive-0.13.1-bin.tar.gz 由于hive依赖于Hadoop框架,所以首先启动Hadoop相关守护进程
-》namenode
-》datanode
-》Resourcemanager
-》nodemanager
-》historyserver- 解压hive安装包
tar -zxf apache-hive-0.13.1-bin.tar.gz -C /opt/modules/
- cd 到modules目录对解压文件重命名:
mv apache-hive-0.13.1-bin/ hive-0.13.1-bin
在HDFS上创建对应的目录,并赋予权限
/user/hive/warehouse-》数据仓库目录,用户保存hive的所有的数据
$ bin/hdfs dfs -mkdir /tmp $ bin/hdfs dfs -mkdir /user/hive/warehouse $ bin/hdfs dfs -chmod g+w /tmp $ bin/hdfs dfs -chmod g+w /user/hive/warehouse
- hadoop目录下命令:
bin/hdfs dfs -mkdir -p /user/hive/warehouse
- 执行赋权限命令(同组可写权限)
bin/hdfs dfs -chmod g+w /tmp
bin/hdfs dfs -chmod g+w /user/hive/warehouse
配置hive-default.xml
- 将conf下hive-default.xml.template改名为hive-default.xml,它包含了hive所有的配置项。其中.template结尾文件代表不启用,不生效的
<!--代表hive在HDFS上的默认目录路径--> <property> <name>hive.metastore.warehouse.dir</name> <value>/user/hive/warehouse</value> <description>location of default database for the warehouse</description> </property>
修改hive-env.sh.template,将template去掉
- 指定Hadoop的工作目录路径:HADOOP_HOME=/opt/modules/hadoop-2.5.0
- 指定用户自定义文件的所在目录路径:export HIVE_CONF_DIR=/opt/modules/hive-0.13.1-bin/conf
- hive根目录下启动hive
bin/hive
- hive提供了一个可以交互的shell命令行
- 创建数据库
create database if not exists hadoop10;
创建一张表
create table student( num int, name string )row format delimited fields terminated by'\t';
需要指定分隔符,让数据文件与表的结构对应
- 加载数据
- 先创建一个数据文件:
vi /opt/datas/student.txt
load data local inpath '/opt/datas/student.txt' into table student;
- 先创建一个数据文件:
- 查询数据
select * from student;
MySql安装
- Derby数据库只能起一个实例,因此需要安装Mysql
配置MySQL数据库用于存储元数据
- 第一步
-》查询:$ sudo rpm -qa | grep mysql
-》卸载:$ sudo rpm -e --nodeps mysql-libs-5.1.66-2.el6_3.x86_64
-》安装:$ sudo yum -y install mysql-server
-》注意:确保可以连接到互联网
- 第二步
-》查询MySQL服务状态:$ sudo service mysqld status
-》启动服务:$ sudo service mysqld start
-》设置开机启动:$ sudo chkconfig mysqld on
-》设置MySQL管理员密码:mysqladmin -u root password '123456'
-》进入MySQL:$ mysql -uroot -p
第三步
-》设置用户连接权限:允许root用户登录任意一个数据库,任意一张表grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
- 第四步:清空表的数据(为了实验环境)
mysql> delete from user where host='127.0.0.1';
mysql> delete from user where host='hadoop-senior01.ibeifeng.com';
mysql> delete from user where host='localhost';
第五步:刷新并退出
mysql> flush privileges; mysql> quit;
- 第六步
sudo service mysqld restart
- 第一步
创建一个hive-site.xml用户自定义的配置文件
-》拷贝一份默认的配置文件过来,重命名:cp hive-default.xml.template hive-site.xml
-》清空文件内容,文件较多:vi hive-site.xml,光标移动到configuration 使用10000dd删除下面的内容。
-》将文件标记补齐:</configuration>
配置hive-site.xml的自定义文件
<!--指定连接MySQL的主机以及端口号和数据库名称--> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://hadoop-senior01.ibeifeng.com:3306/metastore?createDatabaseIfNotExist=true</value> </property> <!--指定MySQL驱动--> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <!--指定连接MySQL的用户名和密码--> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>123456</value> </property>
- 运行bin/hive
- 运行bin/hive 提示错误缺少驱动
- 将驱动上传到software目录下:mysql-connector-java-5.1.27-bin.jar
- 拷贝到hive/lib目录下:
cp -r mysql-connector-java-5.1.27-bin.jar /opt/modules/hive-0.13.1-bin/lib/
- 在mysql中可以看到metastore数据了
- 运行bin/hive 提示错误缺少驱动
Hive的数据库和表的基本操作
- 1. DDL与DML
- DDL数据定义语言:(create/drop/alter/truncate/show/describe), Statistics (analyze), Indexes, Archiving
- DML数据控制语言:(load/insert/update/delete, import/export, explain plan)
创建语句
创建数据库
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_name=property_value, ...)];
创建表
create table if not exists tbname( colum type,... comment ) row format delimited fields terminated by'\t' stored as textfile;
- 删除数据库
DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
创建操作
- 删除之前创建的student数据库:bin/hdfs dfs -rm -r /user/hive/warehouse/hadoop10.db
hive端操作
create database if not exists hadoop10; use hadoop10; create table student( num int, name string ) row format delimited fields terminated by'\t';
- 加载数据:
load data local inpath '/opt/datas/student.txt' into table student;
- 运行一个MR程序:
hive> select name from student;
- select * 默认是不会运行MR程序,必须以某一个字段才可以
- select * 默认是不会运行MR程序,必须以某一个字段才可以
- 描述一张表的三种方式
- desc student;
- desc extended student;
- desc formatted student;
- hive默认创建的表,都是管理表,Table Type: MANAGED_TABLE
- desc student;
- 查看hive提供的方法
hive> show functions;
- 对方法进行描述:
desc function extended upper;
- 配置日志文件
- log4j没有运行,手动配置日志文件
- 重命名:mv hive-log4j.properties.template hive-log4j.properties
- 创建logs目录
- 修改日志文件位置
- 启动hive:看到日志配置文件路径改变了
- 日志文件所在位置
- log4j没有运行,手动配置日志文件
如何显示当前数据以及表头列名
- 在hive-site.xml中
<!--显示当前数据以及表头列名-->
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
- 在hive-site.xml中
- bin/hive -help:查看命令
- 链接一个指定的数据库: bin/hive –database hadoop10
- 直接查看数据库:bin/hive -e “show databases”
- 将结果重定向到一个文件中:bin/hive -e “show databases” > hive.txt
- 执行一个带有SQL语句的文件
vi /opt/datas/hive.sql
写入:show databases;bin/hive -f /opt/datas/hive.sql
- 链接一个指定的数据库: bin/hive –database hadoop10
hive 常用shell
- !代表Linux本地文件系统
- dfs代表 HDFS文件系统
- HDFS上创建文件夹:location
- 指定数据库的位置:
create database if not exists db01_loc LOCATION '/locate';
创建表
create table db01_loc.tb01( name string ) row format delimited fields terminated by'\t'; show tables in db01_loc;
create database if not exists db02; drop database db02;
drop database db01_loc CASCADE;