美文网首页
【2】安装hive

【2】安装hive

作者: 07b287742148 | 来源:发表于2018-09-17 23:56 被阅读12次

环境准备

已安装jdk

已安装hadoop

已安装mysql,作元数据库

hive2.3

mysql-connector-java-5.1.35.jar

  1. 解压重命名配置环境变量
  2. 修改配置文件

hive-env.sh

export JAVA_HOME=/home/common/jdk1.8
export HADOOP_HOME=/home/common/hadoop2.7
export HIVE_HOME=/home/common/hive2.3

hive-site.xml

<configuration>
<!-- 元数据存储地址-->
<property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://localhost:3306/hivedb?createDatabaseIfNotExist=true</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>xxxx</value>
</property>
</configuration>
  1. 将mysql驱动放入hive/lib/下

  2. 进入bin目录下,初始化元数据库

./schematool -dbType mysql -initSchema

  1. 安装完成,命令行启动hive

> hive

hive基本使用

  1. hive 外表关联

//关联 文件,hdfs

CREATE EXTERNAL TABLE test_table(
    id INT, 
    name string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'   //要指明跟文件的对应的分隔符方式
LOCATION 'hdfs:///hive/test';  //本地地址或者hdfs地址

//关联 hbase

CREATE EXTERNAL TABLE test_table(
    rowkey string,
    f1_m string
) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:id,info:name")  //列族,列名映射关系  
TBLPROPERTIES ("hbase.table.name" = "hbaseTable"); //表名

  1. hive分区
create table emp_partition(
id int,
name string
)
partitioned by (createtime string)

查看表结构会发现分区字段也在表里,并且还有分区信息


hive> desc emp_partition;
OK
    id                  int                                         
    name                string                                                                          
    createtime              string                                      
             
    # Partition Information      
    # col_name              data_type               comment             
             
    createtime               string 

根据分区加载数据


load data inpath '/hive/test' overwrite into table emp_partition partition(createtime='20180509');

insert overwrite table emp_partition partition(createtime='20180509') select * from emp_partition;

动态传入分区参数

#启动动态分区功能
set hive.exec.dynamic.partition=true;  
#允许全部分区都是动态分区
set hive.exec.dynamic.partition.mode=nostrick;


insert overwrite table emp_partition partition(createtime)     //宽表    
select id,name,createtime from emp_partition_detail     //明细表
where substr(createtime,1,6)='201710';

  1. 桶表
 create table test_table (id int,name string)
 clustered by (id) sorted by (id asc) into 4 buckets;
 
 
 桶表划分类似于mapreduce最后的reduce对应的分区数。
 
  1. 桶与分区
对应hdfs上

    桶是把数据文件划分成n份来保存;

    分区是把每一份的数据分成多个子目录来保存;



    原始数据存储:
    /hive/db/table/data
    
    分桶后(n=3):
    /hive/db/table1/data
    /hive/db/table2/data
    /hive/db/table3/data
    
    分区后(n=3,分区字段createtime):
    /hive/db/table1/20180101
                   /20180102
    /hive/db/table2/20180101
                   /20180102
    /hive/db/table3/20180101
                   /20180102               
    
    
 

hive编写UDF函数

  1. 创建maven项目,导入依赖
<dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>

    </dependencies>
  1. 实现json数组转为字符串,主要是写evaluate函数实现逻辑功能
package com.pein.udf;

import com.google.gson.JsonObject;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.util.ArrayList;


public class UDFJsonArray extends UDF {

    public String evaluate(ArrayList<JsonObject> arrJson){
        StringBuilder sb = new StringBuilder();
        try {
            for(JsonObject json : arrJson){
                sb.append(json.toString()+"|");
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }



    public static void main(String[] args) {
        UDFJsonArray udf = new UDFJsonArray();
        ArrayList<JsonObject> arrJson = new ArrayList<JsonObject>();
        for(int i = 0; i < 3; i++){
            JsonObject json = new JsonObject();
            json.addProperty("name","tmo"+i*10);
            json.addProperty("age",22);
            json.addProperty("gender","man");
            arrJson.add(json);
        }

        String res = udf.evaluate(arrJson);

        System.out.println(res);


    }
}
  1. maven打成jar包, demo.jar

    放到linux路径下,/home/hive/demo.jar

  2. 进入hive,添加自己打的包

> hive

hive> add jar /home/hive/demo.jar 

hive> create temporary functin arraystr as 'com.pein.udf.UDFJsonArray'

hive> SELECT arraystr(JSON数组字段) ;

相关文章

网友评论

      本文标题:【2】安装hive

      本文链接:https://www.haomeiwen.com/subject/jeujnftx.html