2.Hive安装

Hive安装

1 hive安装地址

1)Hive 官网地址:http://hive.apache.org/
2)文档查看地址:https://cwiki.apache.org/confluence/display/Hive/GettingStarted
3)下载地址:http://archive.apache.org/dist/hive/

2 Hive安装

2.1 Hive安装和配置

1)将hive的包上传到/opt/software

2)解压到/opt/module

[code1997@hadoop02 software]$ tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/module/

3)修改名字为hive

[code1997@hadoop02 module]$ cd /opt/module/
[code1997@hadoop02 module]$ mv apache-hive-1.2.1-bin/ hive

4)修改/opt/module/hive/conf 目录下的 hive-env.sh.template 名称为 hive-env.sh

我这里使用复制的方式:

[code1997@hadoop02 conf]$ cp hive-env.sh.template hive-env.sh

5)配置hive-env.sh文件

  • 配置 HADOOP_HOME 路径:export HADOOP_HOME=/opt/module/hadoop-2.7.2
  • 配置 HIVE_CONF_DIR 路径:export HIVE_CONF_DIR=/opt/module/hive/conf

image-20210103144532350

2.2 hadoop配置

1)启动 hdfs 和 yarn

[code1997@hadoop02 hadoop-2.7.2]$ sbin/start-dfs.sh
[code1997@hadoop03 hadoop-2.7.2]$ sbin/start-yarn.sh
2.3 Hive基本操作

1)启动hive

[code1997@hadoop02 hive]$ bin/hive

2)查看数据库

[code1997@hadoop02 hive]$ bin/hive

3)使用数据库:default

hive> use default;

4)显示default数据库中的表

hive> show tables;

5)创建一张表student

hive> create table student(id int,name string);

6)查看表结构

hive> desc student;

7)向表中插入数据

hive> insert into student values(1001,'zhangsan');

image-20210103151910455

8)查看表中数据(做计算会走MR)

hive> select * from student;

image-20210103152018869

9)退出hive

hive> quit;

10)查看hdfs:http://hadoop02:50070/explorer.html#/

image-20210103152735901

3 将本地文件导入Hive

​ 需求:将本地/opt/module/data/student.txt 这个目录下的数据导入到 hive 的 student(id int, name
string)表中。

3.1 准备数据

/opt/module/data下创建一个文件student.txt

[code1997@hadoop02 data]$ touch student.txt
[code1997@hadoop02 data]$ vim student.txt

内容如下:使用tab分割符分开

1002    lisi
1003    wangwu
1004    zhaoliu
1005    tianqi
3.2 Hive实际操作

1)启动hive

[code1997@hadoop02 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’

create table student(id int,name string) row format delimited fields terminated by '\t'

7)加载文件到数据库表student中

hive> load data local inpath '/opt/module/data/student.txt' into table student;

8)hive查询结果

hive> select * from student;

image-20210103155715612

问题:

再开一个窗口启动hive:

image-20210103155914979

原因:Metastore 默认存储在自带的 derby 数据库中,推荐使用 MySQL 存储 Metastore;

3.3 安装mysql

参考:https://blog.youkuaiyun.com/qq_44739500/article/details/108850853

如果下载速度慢,参考:https://blog.youkuaiyun.com/u013492736/article/details/70224164

3.4 配置元数据存储到mysql

1)将mysql驱动放到/opt/module/hive/lib

image-20210103213445099

2)在/opt/module/hive/conf 目录下创建一个 hive-site.xml

[code1997@hadoop02 conf]$ touch hive-site.xml
[code1997@hadoop02 conf]$ vim 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://hadoop02:3306/metastore?createDatabaseIfNotExist=true</value>
        <description>JDBC connect string for a JDBCmetastore</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>19971001</value>
        <description>password to use against metastore database</description>
    </property>
</configuration>

3)配置完毕后,如果启动 hive 异常,可以重新启动虚拟机。(重启后,别忘了启动 hadoop 集群)

3.5 测试

1)在hive中创建一个表aa。

image-20210103215240266

2)查看hdfs

image-20210103215304753

3)查看mysql

image-20210103215325338

4)查看metastore中的表

image-20210103215625644

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值