[atguigu@hadoop102 software]$ tar -zxvf
apache-hive-1.2.1-bin.tar.gz -C /opt/module/
(3)修改 apache-hive-1.2.1-bin.tar.gz 的名称为 hive
[atguigu@hadoop102 module]$ mv apache-hive-1.2.1-bin/ hive
(4)修改/opt/module/hive/conf 目录下的 hive-env.sh.template 名称为 hive-env.sh
[atguigu@hadoop102 conf]$ mv hive-env.sh.template hive-env.sh
(5)配置 hive-env.sh 文件 (a)配置 HADOOP_HOME 路径
export HADOOP_HOME=/opt/module/hadoop-2.7.2
(b)配置 HIVE_CONF_DIR 路径
export HIVE_CONF_DIR=/opt/module/hive/conf
[atguigu@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
[atguigu@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
(2)在 HDFS 上创建/tmp 和/user/hive/warehouse 两个目录并修改他们的同组权限可写 (可不操作,系统会自动创建)
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir /tmp
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir -p
/user/hive/warehouse
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /tmp
[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w
/user/hive/warehouse
[atguigu@hadoop102 hive]$ bin/hive
(2)查看数据库
hive> show databases;
(3)打开默认数据库
hive> use default;
(4)显示 default 数据库中的表
hive> show tables;
(5)创建一张表
hive> create table student(id int, name string);
(6)显示数据库中有几张表
hive> show tables;
(7)查看表的结构
hive> desc student;
(8)向表中插入数据
hive> insert into student values(1000,"ss");
(9)查询表中数据
hive> select * from student;
(10)退出 hive
hive> quit;
需求 将本地/opt/module/data/student.txt 这个目录下的数据导入到 hive 的 student(id int, name string)表中。 1.数据准备 在/opt/module/data 这个目录下准备数据 (1)在/opt/module/目录下创建 data
[atguigu@hadoop102 module]$ mkdir data
(2)在/opt/module/datas/目录下创建 student.txt 文件并添加数据
[atguigu@hadoop102 datas]$ touch student.txt
[atguigu@hadoop102 datas]$ vi student.txt
1001 zhangshan
1002 lishi
1003 zhaoliu
注意以 tab 键间隔。
[atguigu@hadoop102 hive]$ bin/hive
(2)显示数据库
hive> show databases;
(3)使用 default 数据库
hive> use default;
(4)显示 default 数据库中的表
hive> show tables;
(5)删除已创建的 student 表
hive> drop table student;
(6)创建 student 表, 并声明文件分隔符’\t’
hive> create table student(id int, name string) ROW FORMAT
DELIMITED FIELDS TERMINATED
BY '\t';
(7)加载/opt/module/data/student.txt 文件到 student 数据库表中。
hive> load data local inpath '/opt/module/data/student.txt' into
table student;
(8)Hive 查询结果
hive> select * from student;
OK
1001 zhangshan
1002 lishi
1003 zhaoliu
Time taken: 0.266 seconds, Fetched: 3 row(s)
Exception in thread "main" java.lang.RuntimeException:
java.lang.RuntimeException:
Unable to instantiate
org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClien t
at
org.apache.hadoop.hive.ql.session.SessionState.start(Session
State.java:522)
at
org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:677)
at
org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621
)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce
ssorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe
thodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.RuntimeException: Unable to instantiate
org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClien
t
at
org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(
MetaStoreUtils.java:1523)
at
org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<in
it>(RetryingMetaStoreClient.java:86)
at
org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.get
Proxy(RetryingMetaStoreClient.java:132)
at
org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.get
Proxy(RetryingMetaStoreClient.java:104)
at
org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClien
t(Hive.java:3005)
at
org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:302
4)
at
org.apache.hadoop.hive.ql.session.SessionState.start(Session
State.java:503)
... 8 more
原因是,Metastore 默认存储在自带的 derby 数据库中,推荐使用 MySQL 存储 Metastore;
[root@hadoop102 桌面]# rpm -qa|grep mysql
mysql-libs-5.1.73-7.el6.x86_64
(2)卸载
[root@hadoop102 桌 面 ]# rpm -e --nodeps
mysql-libs-5.1.73-7.el6.x86_64 2.解压 mysql-libs.zip 文件到当前目录
[root@hadoop102 software]# unzip mysql-libs.zip
[root@hadoop102 software]# ls
mysql-libs.zip
mysql-libs
3.进入到 mysql-libs 文件夹下
[root@hadoop102 mysql-libs]# ll
总用量 76048
-rw-r--r--. 1 root root 18509960 3 月 26 2015
MySQL-client-5.6.24-1.el6.x86_64.rpm
-rw-r--r--. 1 root root 3575135 12 月 1 2013
mysql-connector-java-5.1.27.tar.gz
-rw-r--r--. 1 root root 55782196 3 月 26 2015
MySQL-server-5.6.24-1.el6.x86_64.rpm
[root@hadoop102 mysql-libs]# rpm -ivh
MySQL-server-5.6.24-1.el6.x86_64.rpm
[root@hadoop102 mysql-libs]# cat /root/.mysql_secret
OEXaQuS8IWkG19Xs
[root@hadoop102 mysql-libs]# service mysql status
[root@hadoop102 mysql-libs]# service mysql start
[root@hadoop102 mysql-libs]# rpm -ivh
MySQL-client-5.6.24-1.el6.x86_64.rpm
[root@hadoop102 mysql-libs]# mysql -uroot -pOEXaQuS8IWkG19Xs
mysql>SET PASSWORD=PASSWORD('000000');
mysql>exit
配置只要是 root 用户+密码,在任何主机上都能登录 MySQL 数据库。
[root@hadoop102 mysql-libs]# mysql -uroot -p000000
mysql>show databases;
mysql>use mysql;
mysql>show tables;
mysql>desc user;
mysql>select User, Host, Password from user;
mysql>update user set host='%' where host='localhost';
delete from user where Host='hadoop102';
delete from user where Host='127.0.0.1';
delete from user where Host='::1';
mysql>flush privileges;
mysql>quit;
[root@hadoop102 mysql-libs]# tar -zxvf
mysql-connector-java-5.1.27.tar.gz
[root@hadoop102 mysql-connector-java-5.1.27]# cp
/opt/software/mysql-libs/mysql-connector-java-5.1.27/mysql-c
onnector-java-5.1.27-bin.jar/opt/module/hive/lib/
[atguigu@hadoop102 conf]$ touch hive-site.xml
[atguigu@hadoop102 conf]$ vi hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseI
fNotExist=true</value>
<description>JDBC connect string for a JDBC
metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC
metastore</description>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>username to use against metastore
database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>000000</value>
<description>password to use against metastore
database</description>
</property>
</configuration>
[atguigu@hadoop102 mysql-libs]$ mysql -uroot -p000000
查看有几个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+ 2.再次打开多个窗口,分别启动 hive
[atguigu@hadoop102 hive]$ bin/hive
mysql> show databases;
+--------------------+
| Database | +--------------------+
| information_schema |
| metastore |
| mysql |
| performance_schema |
| test |
+--------------------+
[atguigu@hadoop102 hive]$ bin/hiveserver2
[atguigu@hadoop102 hive]$ bin/beeline
Beeline version 1.2.1 by Apache Hive
beeline>
beeline> !connect jdbc:hive2://hadoop102:10000(回车)
Connecting to jdbc:hive2://hadoop102:10000
Enter username for jdbc:hive2://hadoop102:10000: atguigu(回车)
Enter password for jdbc:hive2://hadoop102:10000: (直接回车)
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://hadoop102:10000> show databases;
+----------------+--+
| database_name |
+----------------+--+
| default |
| hive_db2 |
+----------------+--+
[atguigu@hadoop102 hive]$ bin/hive -help
usage: hive
-d,--define <key=value> Variable subsitution to apply
to hive
commands. e.g. -d A=B or --define
A=B
--database <databasename> Specify the database to use
-e <quoted-query-string> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
--hiveconf <property=value> Use value for given property
--hivevar <key=value> Variable subsitution to apply
to hive
commands. e.g. --hivevar A=B
-i <filename> Initialization SQL file
-S,--silent Silent mode in interactive
shell
-v,--verbose Verbose mode (echo executed SQL
to the console)
[atguigu@hadoop102 hive]$ bin/hive -e "select id from student;"
[atguigu@hadoop102 datas]$ touch hivef.sql
文件中写入正确的 sql 语句
select * from student;
(2)执行文件中的 sql 语句
[atguigu@hadoop102 hive]$ bin/hive -f
/opt/module/datas/hivef.sql
(3)执行文件中的 sql 语句并将结果写入文件中
[atguigu@hadoop102 hive]$ bin/hive -f
/opt/module/datas/hivef.sql >
/opt/module/datas/hive_result.txt
1)Default 数据仓库的最原始位置是在 hdfs 上的:/user/hive/warehouse 路径下。 2)在仓库目录下,没有对默认的数据库 default 创建文件夹。如果某张表属于 default数据库,直接在数据仓库目录下创建一个文件夹。 3)修改 default 数据仓库原始位置(将 hive-default.xml.template 如下配置信息拷贝到hive-site.xml 文件中)。
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>
配置同组用户有执行权限
bin/hdfs dfs -chmod g+w /user/hive/warehouse
1)在 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>
2)重新启动 hive,对比配置前后差异。 (1)配置前,如图 6-2 所示
(2)配置后,如图 6-3 所示
[atguigu@hadoop102 conf]$ pwd
/opt/module/hive/conf
[atguigu@hadoop102 conf]$ mv hive-log4j.properties.template
hive-log4j.properties
(2)在 hive-log4j.properties 文件中修改 log 存放位置hive.log.dir=/opt/module/hive/logs
hive>set;
(2)命令行参数方式 启动 Hive 时,可以在命令行添加-hiveconf param=value 来设定参数。 例如:
[atguigu@hadoop103 hive]$ bin/hive -hiveconf
mapred.reduce.tasks=10;
注意:仅对本次 hive 启动有效查看参数设置:
hive (default)> set mapred.reduce.tasks;
(3)参数声明方式 可以在 HQL 中使用 SET 关键字设定参数 例如:
hive (default)> set mapred.reduce.tasks=100;
注意:仅对本次 hive 启动有效。 查看参数设置
hive (default)> set mapred.reduce.tasks;
上述三种设定方式的优先级依次递增。即配置文件<命令行参数<参数声明。注意某些系统级的参数,例如 log4j 相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。