2.1.5 在hdfs 中创建下面的目录 ,并且授权
hadoop fs -mkdir -p /user/hive/warehouse
hadoop fs -mkdir -p /user/hive/tmp
hadoop fs -mkdir -p /user/hive/log
hadoop fs -chmod -R 777 /user/hive/warehouse
hadoop fs -chmod -R 777 /user/hive/tmp
hadoop fs -chmod -R 777 /user/hive/log
2.1.6 修改hive-site.xml
将 hive-site.xml 文件中以下几个配置项的值设置成上一步中创建的几个路径。
<property>
<name>hive.exec.scratchdir</name>
<value>/user/hive/tmp</value>
</property>
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
<property>
<name>hive.querylog.location</name>
<value>/user/hive/log</value>
</property>
2.1.7 需要在 hive-site.xml 文件中配置 MySQL 数据库连接信息。
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hd01:3306/hive?createDatabaseIfNotExist=true&characterEncoding=UTF-8&useSSL=false</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>123456</value>
</property>
2.1.8 创建tmp文件
mkdir /home/hd/apps/hive/tmp
2.1.9 在配置文件 hive-site.xml 里面
把 "${system:java.io.tmpdir}" 改成 /home/hd/apps/hive/tmp/ 此处有4个地方 把 "{system:user.name}" 改成 {user.name} 此处有3个地方
2.1.10 配置jdbc驱动包
mysql的jdbc 驱动包下载地址:
下载完成后把mysql-connector-java-5.1.27.jar 然后放入 $HIVE_HOME/lib 目录下
2.1.11 初始化hive
从 Hive 2.1 版本开始, 我们需要先运行 schematool 命令来执行初始化操作。
schematool -dbType mysql -initSchema
看到schemaTool completed 则初始化完成
2.1.12 启动metastore服务
$HIVE_HOME/bin/hive --service metastore
检测hive 是否成功 直接在命令行输入hive即可
2.1.13 进入控制台
[hd@hd01 hive]# ./bin/hive
which: no hbase in
(/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/hd/apps/java/bin:/home/hd/apps/hadoop/bin:/home/hd/apps/hadoop/sbin:/root/bin:/home/hd/apps/java/bin:/home/hd/apps/hadoop/bin:/ho
me/hd/apps/hadoop/sbin:/home/hd/apps/hive/bin)
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/hd/apps/hive/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hd/apps/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Logging initialized using configuration in file:/home/hd/apps/hive/conf/hive-log4j2.properties Async: true
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
hive> show databases;
OK
default
Time taken: 8.295 seconds, Fetched: 1 row(s)
hive>
2.2 Hive建表操作
2.2.1 建表
create table test_user (
user_id int comment 'userID',
user_name string comment 'userName'
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
2.2.2 准备数据
准备数据,用户id和用户名字,用,分割,存在hive_table_data.txt
2020001,小王
2020002,小李
2020003,小明
2020004,阿狗
2020005,姚明
2.2.3 写入表中
从本地LOAD DATA overwrite方式,覆盖方式写入表中
hive> LOAD DATA LOCAL INPATH '/xx/hive_table_data.txt' OVERWRITE INTO TABLE test_user;
Loading data to table default.test_user
Table default.test_user stats: [numFiles=1, totalSize=75]
OK
2.2.4 查看hive数据
查看hive表中数据:
hive> select * from test_user;
OK
2020001 小王
2020002 小李
2020003 小明
2020004 阿狗
2020005 姚明
2.2.5 查看HDFS路径上数据
查看HDFS路径上数据:
hadoop fs -ls /user/hive/warehouse/test_user
Found 1 items
-rwxrwxrwx 3 deploy hive 75 2020-05-08 18:20 /user/hive/warehouse/test_user/hive_table_data.txt
2.2.6 本地LOAD数据
从本地LOAD DATA append方式,append方式同样名字的文件,会自动进行重命名
hive> LOAD DATA LOCAL INPATH '/xxx/hive_table_data.txt' INTO TABLE test_user;
Loading data to table default.test_user
Table default.test_user stats: [numFiles=2, totalSize=150]
2.2.7 查看HDFS路径上的数据被重命名
查看HDFS路径上的数据,可以看到同样的文件被重命名了:
hadoop fs -ls /user/hive/warehouse/test_user
Found 2 items
-rwxrwxrwx 3 deploy hive 75 2020-05-08 18:20 /user/hive/warehouse/test_user/hive_table_data.txt
-rwxrwxrwx 3 deploy hive 75 2020-05-08 18:24 /user/hive/warehouse/test_user/hive_table_data_copy_1.txt
2.2.8 删除表
删除表,删除表后,元数据和数据文件也相应删除
drop table test_user
查看HDFS路径,发现数据也删除了:
hadoop fs -ls /user/hive/warehouse/test_user
ls: `/user/hive/warehouse/test_user': No such file or directory
2.2.9 外部表创建&删除
外部表创建&删除,创建外部表指定数据路径,将刚刚的文件上传到需要指定的路径中,可以看到可以查询到数据
hive> create external table test_user (
> user_id int comment 'userID',
> user_name string comment 'userName'
> )
> ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
> LOCATION '/user/xx/tmp/test_hive/';
OK
Time taken: 0.09 seconds
hive> select * from test_user;
OK
2020001 小王
2020002 小李
2020003 小明
2020004 阿狗
2020005 姚明
2020001 小王
2020002 小李
2020003 小明
2020004 阿狗
2020005 姚明
2.2.10 删除外部表d
hive> drop table test_user;
OK
hadoop fs -ls /user/xx/tmp/test_hive/
Found 2 items
-rw-r--r-- 3 data_udd bigdata 75 2020-05-08 18:38 /user/xx/tmp/test_hive/hive_table_data.txt
-rw-r--r-- 3 data_udd bigdata 75 2020-05-08 18:38 /user/xx/tmp/test_hive/hive_table_data_copy_1.txt