zoukankan      html  css  js  c++  java
  • 002.hive-UDF自定义函数

    IDEA

    配置文件 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.star</groupId>
        <artifactId>scala_demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    
        <dependencies>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>2.7.3</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-jdbc</artifactId>
                <version>2.1.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-exec</artifactId>
                <version>2.1.0</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.25</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>

    java  注意方法名一定要用这个 evaluate

    import   org.apache.hadoop.hive.ql.exec.UDF;
    import   org.apache.hadoop.hive.ql.exec.Description;
    @Description(
        name = "myudf",
        value = "this is a function",
        extended = "eg:select myudf(1,2) = > 1+2,select myudf("hello","world")=>helloworld"
    )
    
    public class MyUDF extends UDF{
        public int evaluate(int i,int j){
           return i+j;
        }
        public String evaluate(String a ,String b){
            return a+b;
        }
    }

    打jar包

     jar 上传到 /soft/apache-hive-2.1.1-bin/lib

    重启hive服务

    [centos@s101 /home/centos]$hiveserver2

    连接hive

    [centos@s101 /home/centos]$beeline -u jdbc:hive2://localhost:10000

    注册函数

    0: jdbc:hive2://localhost:10000> show databases;
    +----------------+--+
    | database_name |
    +----------------+--+
    | default |
    | mydb |
    +----------------+--+
    2 rows selected (4.71 seconds)


    0: jdbc:hive2://localhost:10000> use mydb;

    0: jdbc:hive2://localhost:10000> create function myudf as 'MyUDF';
    No rows affected (0.839 seconds)

    0: jdbc:hive2://localhost:10000> show functions;

    0: jdbc:hive2://localhost:10000> use mydb
    . . . . . . . . . . . . . . . .> ;
    No rows affected (14.597 seconds)
    0: jdbc:hive2://localhost:10000> create function myudf as 'MyUDF';
    No rows affected (0.464 seconds)
    0: jdbc:hive2://localhost:10000> desc function extended myudf;
    +-------------------------------------------------------------------------+--+
    | tab_name |
    +-------------------------------------------------------------------------+--+
    | this is a function |
    | Synonyms: mydb.myudf |
    | eg:select myudf(1,2) = > 1+2,select myudf("hello","world")=>helloworld |
    +-------------------------------------------------------------------------+--+
    3 rows selected (4.17 seconds)
    0: jdbc:hive2://localhost:10000> select mydb.myudf("df","df");
    +-------+--+
    | _c0 |
    +-------+--+
    | dfdf |
    +-------+--+
    1 row selected (4.564 seconds)

  • 相关阅读:
    小结:机器学习基础部分
    概率图:HMM:Evaluation问题(前向算法/后向算法)
    概率图:GMM求解:EM优化算法的导出(从【ELBO+KL】和【ELBO+Jensen】两个角度导出)
    概率图:GMM:EM算法及其收敛性证明
    概率图:高斯混合模型(GMM)
    概率图基础:D-separation;全局Markov性质;Markov Blanket
    概率图基础:概率基本概念、条件独立性、图求解联合概率的规则合理性推理
    mysql索引失效
    mysql 统计行数count(*)
    mysql如何收缩表空间
  • 原文地址:https://www.cnblogs.com/star521/p/15136792.html
Copyright © 2011-2022 走看看