5.配置hive

5.1 hive下载地址

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

解压缩

[root@master ~]# tar xf apache-hive-3.1.2-bin.tar.gz

[root@master ~]# mv apache-hive-3.1.2-bin /usr/local/hive

5.2准备数据库hive

192.168.101.54提供hive服务的数据库hive,用户密码均为hive

建表语句

create database hive default character set utf8;

grant all privileges on hive.* to 'hive'@'%' identified by 'hive';

flush privileges;

5.3配置 Hive

a重命名以下配置文件:

cd /usr/local/hive/conf/

cp hive-env.sh.template hive-env.sh

cp hive-default.xml.template hive-site.xml

cp hive-log4j2.properties.template hive-log4j2.properties

cp hive-exec-log4j2.properties.template hive-exec-log4j2.properties

 

 

 

b修改 hive-env.sh:

export JAVA_HOME=/usr/local/jdk  

export HADOOP_HOME=/usr/local/hadoop   

export HIVE_HOME=/usr/local/hive

export HIVE_CONF_DIR=$HIVE_HOME/conf    

 

 

 

c修改 hive-site.xml:

修改对应属性的 value

vi hive-site.xml

  <property>

    <name>hive.exec.scratchdir</name>

    <value>/tmp/hive-${user.name}</value>

    <description>HDFS root scratch dir for Hive jobs which gets

    created with write all (733) permission. For each connecting user,

    an HDFS scratch dir: ${hive.exec.scratchdir}/<username> is created,

    with ${hive.scratch.dir.permission}.

    </description>

  </property>

 

 

 

  <property>

    <name>hive.exec.local.scratchdir</name>

    <value>/tmp/${user.name}</value>

    <description>Local scratch space for Hive jobs</description>

  </property>

  <property>

    <name>hive.downloaded.resources.dir</name>

    <value>/tmp/hive/resources</value>

    <description>Temporary local directory for added resources in the remote

    file system.</description>

  </property>

 

 

 

  <property>

    <name> hive.querylog.location</name>

    <value>/tmp/${user.name}</value>

    <description>Location of Hive run time structured log file</description>

  </property>

 

 

 

  <property>

    <name>hive.server2.logging.operation.log.location</name>

    <value>/tmp/${user.name}/operation_logs</value>

    <description>Top level directory where operation logs are stored if logging functionality is enabled</description>

  </property>

 

 

 

d配置 Hive Metastore

Hive Metastore 是用来获取 Hive 表和分区的元数据,本例中使用 mariadb 来存储此类元数据。

mysql-connector-java-5.1.32-bin.jar 放入 $HIVE_HOME/lib

 

 

 

并在 hive-site.xml 中配置 MySQL 数据库连接信息。

<property>

    <name>javax.jdo.option.ConnectionURL</name>

<value>jdbc:mysql://192.168.101.54:3306/hive</value>

</property>

(下图内容以上述文本为准)

 

 

 

<property>

    <name>javax.jdo.option.ConnectionDriverName</name>

    <value>com.mysql.jdbc.Driver</value>

</property>

(下图内容以上述文本为准)

 

 

 

<property>

    <name>javax.jdo.option.ConnectionUserName</name>

    <value>hive</value>

</property>

(下图内容以上述文本为准)

 

 

 

<property>

    <name>javax.jdo.option.ConnectionPassword</name>

    <value>hive</value>

</property>

(下图内容以上述文本为准)

 

eHive 创建 HDFS 目录

start-dfs.sh   #如果在安装配置hadoop是已经启动,则此命令可省略

hdfs dfs -mkdir /tmp

hdfs dfs -mkdir -p /usr/hive/warehouse

hdfs dfs -chmod g+w /tmp

hdfs dfs -chmod g+w /usr/hive/warehouse

 

 

 

f hive-site.xml

  3213       Ensures commands with OVERWRITE (such as INSERT OVERWRITE) acquire Exclusive locks fortransactional tables.  This ensures that inserts (w/o over

        write) running concurrently

删除&#*

5.4初始化schematool

添加环境变量

vim /etc/profile

 

#hive  

export HIVE_HOME=/usr/local/hive

export PATH=$PATH:$HIVE_HOME/bin

 

source /etc/profile

Hive 2.1 版本开始, 在启动 Hive 之前需运行 schematool 命令来执行初始化操作:

schematool -dbType mysql -initSchema

 

 

 

 

5.5启动 Hive,输入命令 Hive

 

 

 

5.6测试 Hive

Hive中创建一个表:

hive> create table test_hive(id int, name string)

> row format delimited fields terminated by '\t'

#字段之间用tab键进行分割

> stored as textfile;

 # 设置加载数据的数据类型,默认是TEXTFILE,如果文件数据是纯文本,就是使用 [STORED AS TEXTFILE],然后从本地直接拷贝到HDFS上,hive直接可以识别数据

hive> show tables;

 

 

 

可以看到表已经创建成功,输入 quit ; 退出 Hive,接下来以文本形式创建数据:

 

 

 

进入 Hive,导入数据:

hive> load data local inpath '/opt/test_db.txt' into table test_hive;

hive> select * from test_hive

 

 

 

5.7后台运行hiveserver2

远程访问,开放10000端口。10002beelineGUI界面。

运行服务

[root@master ~]# nohup hive –service hiveserver2 &

[root@master ~]# netstat -aunltp |grep 10000

[root@master ~]# ps -aux| grep hiveserver2

 

 

 

复制jar

[root@master lib]# cp /usr/local/jdk/lib/tools.jar /usr/local/hive/lib/