技术博客 技术博客
  • JAVA
  • 仓颉
  • 设计模式
  • 人工智能
  • Spring
  • Mybatis
  • Maven
  • Git
  • Kafka
  • RabbitMQ
  • RocketMQ
  • Redis
  • Zookeeper
  • Nginx
  • 数据库套件
  • MySQL
  • Elasticsearch
  • MongoDB
  • Hadoop
  • ClickHouse
  • Hbase
  • Hive
  • Flink
  • Flume
  • SQLite
  • linux
  • Docker
  • Jenkins
  • Kubernetes
  • 工具
  • 前端
  • AI
GitHub (opens new window)
  • JAVA
  • 仓颉
  • 设计模式
  • 人工智能
  • Spring
  • Mybatis
  • Maven
  • Git
  • Kafka
  • RabbitMQ
  • RocketMQ
  • Redis
  • Zookeeper
  • Nginx
  • 数据库套件
  • MySQL
  • Elasticsearch
  • MongoDB
  • Hadoop
  • ClickHouse
  • Hbase
  • Hive
  • Flink
  • Flume
  • SQLite
  • linux
  • Docker
  • Jenkins
  • Kubernetes
  • 工具
  • 前端
  • AI
GitHub (opens new window)
  • mysql

    • MySQL 问题汇总
    • MySQL 索引介绍
    • MySQL 锁介绍
    • MySQL 索引优化工具 explain
    • MySQL 主从复制(GTID)
    • MySQL 8安装
    • MySQL 8.x新特性总结
    • MySQL UDF以及新类型JSON
    • MySQL 高可用MGR(一) 理论
    • MySQL 高可用MGR(二) 搭建
    • MySQL 高可用MGR(三) 测试
  • Elasticsearch

    • ES 7.8.0(一) 入门介绍
    • ES 7.8.0(二) 读、写和写索引流程以及文档分析过程
    • ES 7.8.0(三) 文档冲突
  • mongodb

    • mongodb
  • hadoop

    • Hadoop 伪分布式及集群
    • Hadoop 指令
    • Hadoop 读写流程详解
    • Hadoop SpringBoot集成
    • Hadoop MapReduce机制
    • Hadoop YARN
    • Hadoop MapReduce配置和编写job及数据倾斜的解决
    • Hadoop MapReduce自定义格式输入输出
  • clickhouse

    • ClickHouse 介绍及安装
    • ClickHouse 数据类型
    • ClickHouse 表引擎
    • ClickHouse SQL操作
    • ClickHouse 副本配置
    • ClickHouse 分片与集群部署
    • ClickHouse Explain及建表优化
    • ClickHouse 语法优化规则
    • ClickHouse 查询优化
    • ClickHouse 数据一致性
    • ClickHouse 物化视图
    • ClickHouse MaterializeMySQL引擎
    • ClickHouse 监控及备份
  • hbase

    • Hbase 介绍及安装
    • Hbase 优化
    • Hbase phoenix安装及使用
    • Hbase LSM-TREE
  • hive

    • Hive 介绍及安装
    • Hive 内外部表、分区表、分桶表概念及hiveSQL命令
    • Hive 数据类型
    • Hive 函数 MySQL联合
      • 函数
        • regexp_replace
        • regexp_extract
      • UDF
        • 实现步骤
      • 配置Mysql
    • Hive 数据倾斜和优化
    • Hive Sqoop安装及指令
  • flink

    • Flink 介绍及安装
    • Flink 配置介绍及Demo
    • Flink API讲解
    • Flink 运行架构
    • Flink 时间语义及Watermark
    • Flink 状态管理
    • Flink 容错,检查点,保存点
    • Flink 状态一致性
    • Flink Table API 和 Flink SQL
    • Flink CEP编程
    • Flink Joining编程
    • Flink CDC
  • flume

    • Flume 日志收集系统介绍及安装
    • Flume Source支持的类型
    • Flume Sink支持的类型
    • Flume Channel支持的类型
    • Flume Selector
    • Flume Interceptor拦截器类型
    • Flume Process
  • sqlite

    • SQLite介绍
目录

Hive 函数 MySQL联合

本文及后续所有文章都以 3.1.2 做为版本讲解和入门学习

# 函数

返回类型 函数名 描述
int      length(string a) 返回字符串 a 的长度 select length (a) from table_name
string reverse(string a) 返回字符串 a 的反转结果 select reverse (a) from table_name
string concat(string a,string b...) 字符串连接函数 select concat (id,name) from table_name
string concat_ws(string sep,string a,string b...) 带分隔符的字符串连接函数 select concat_ws (',',id,name) from table_name
string substr(string a,index int,lastindex int) 截取字符串
string upper(string a) 转大写
string lower(string a) 转小写
string trim(string a) 去两边空格
string ltrim(string a) 左边去空格
string rtrim(string a) 右边去空格
string regexp_replace(string a,string b,string c) 将字符串 a 中的符合 java 正则表达式 b 的部分替换为 c,有些情况下要使用转义字符如 [] 为 [*],类似 oracle 的 regexp_replace 函数
string regexp_extract(string a,string patten,int index) 将字符串 subject 按照 pattern 正则表达式的规则拆分,返回 index 指定的字符
string repeat(string a,int n) 返回重复 n 次后的 str 字符串
array split(string a,string pat) 分割字符串函数
row explode(array a) 命令可以将一行数据,按指定规则切分多行 select explode (split (a,',')) from table_name

# regexp_replace

正则替换

hive> select regexp_replace('foobar','oo|ar','');
OK
fb
Time taken: 0.317 seconds, Fetched: 1 row(s)
1
2
3
4

# regexp_extract

会提取 () 里的内容,下标从 1 开始,0 默认全部

hive> select regexp_extract('foothebar','foo(.*)(bar)',1);
OK
the
Time taken: 0.307 seconds, Fetched: 1 row(s)
hive> select regexp_extract('foothebar','foo(.*)(bar)',2);
OK
bar
Time taken: 0.314 seconds, Fetched: 1 row(s)
hive> select regexp_extract('foothebar','foo(.*)(bar)',0);
OK
foothebar
Time taken: 0.658 seconds, Fetched: 1 row(s)
1
2
3
4
5
6
7
8
9
10
11
12

# UDF

如果 hive 的内置函数不够用,我们也可以自定义函数来使用,这样的函数称为 hive 的用户自定义函数,简称 UDF。

# 实现步骤

maven

<dependencies>
    <!-- hive -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>3.1.2</version>
    </dependency>
    <!-- hadoop -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>1.2.1</version>
    </dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

实现

package com.example.demo;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

/**
 * @author big uncle
 * @date 2021/5/19 14:52
 * 自定义一个函数,接收两个参数,如果参数1为 null值,则用第二个参数做为返回值
 **/
public class Ifv extends GenericUDF{

    /**
     * 执行一次 检查参数个数 和 参数类型
    **/
    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        System.out.println("initialize 被调用了 ");
        if(arguments.length != 2){
            throw new UDFArgumentException("arg length must is 2");
        }
        // 返回一个String对象检查器
        ObjectInspector outputOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
        return outputOI;
    }

    /**
     * 处理数据
    **/
    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        System.out.println("evaluate 被调用了");
        if(arguments[0].get() != null){
            return arguments[0].get().toString();
        }
        String arg1 = arguments[1].get().toString();
        return arg1;
    }

    /**
     * explain 详细计划
    **/
    @Override
    public String getDisplayString(String[] children) {
        System.out.println("getDisplayString 被调用了");
        return getStandardDisplayString(getFuncName(), children);
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

添加到 hive,在 hive 中输入如下

add jar /root/udf-1.0.0.RELEASE.jar;
1

创建成一个函数

create temporary function ifv as 'com.example.demo.Ifv';
1

调用

hive> select ifv(null,"aaaa");
initialize 被调用了 
evaluate 被调用了
initialize 被调用了 
evaluate 被调用了
initialize 被调用了 
evaluate 被调用了
OK
aaaa
Time taken: 6.55 seconds, Fetched: 1 row(s)
1
2
3
4
5
6
7
8
9
10

谁都不要在告诉我 initialize 只调用一次,mmp 官网这么说我都不信

# 配置 Mysql

  1. 进入 mysql 数据库先创建 hive 库
create database hive character set latin1;
1
  1. 在 hive/conf 目录下创建 hive-site.xml
<configuration>

  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://node113:3306/hive?createDatabaseIfNotExist=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai</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>Admin@123</value>
  </property>

</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  1. 下载 mysql 对应的驱动,丢到 hive/lib/ 目录下
  2. 删除 hive/bin/metastore_db
  3. 在 hive/bin/ 目录下执行如下
./schematool -dbType mysql -initSchema
1
  1. 启动 hive/bin/
./hive
1

当 hive 启动成功后,会在 mysql 中创建很多的表

表 作用
DBS 管理 hive 中创建的库
TBLS 管理 hive 中创建的表,其中 TBL_TYPE:MANAGED_TABLE (内部表),EXTERNAL_TABLE (外部表)
PARTITIONS
上次更新: 6/11/2025, 4:10:30 PM
Hive 数据类型
Hive 数据倾斜和优化

← Hive 数据类型 Hive 数据倾斜和优化→

Theme by Vdoing | Copyright © 2023-2025
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式