项目实战从0到1之hive(11)实现*脱敏的udf函数执行过程

Hive UDFHive UDF 函数1 POM 文件2.UDF 函数3 利用idea打包4 添加hive udf函数4.1 上传jar包到集群4.2 修改集群hdfs文件权限4.3 注册UDF4.4 使用UDF

Hive UDF 函数

1、 POM 文件

<?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>填写自己的组织名称</groupId>
 <artifactId>udf</artifactId>
 <version>1.0-SNAPSHOT</version>
 <properties>
  <project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
  <!--Hadoop版本更改成自己的版本-->
  <hadoop.version>2.6.0-cdh5.13.3</hadoop.version>
  <hive.version>1.1.0-cdh5.13.3</hive.version>
 </properties>

 <repositories>
  <!--加入Hadoop原生态的maven仓库的地址-->
  <repository>
   <id>Apache Hadoop</id>
   <name>Apache Hadoop</name>
   <url>https://repo1.maven.org/maven2/</url>
  </repository>
  <!--加入cdh的maven仓库的地址-->
  <repository>
   <id>cloudera</id>
   <name>cloudera</name>
   <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
  </repository>
 </repositories>

 <dependencies>
  <!--添加hadoop依赖-->
  <dependency>
   <groupId>org.apache.hadoop</groupId>
   <artifactId>hadoop-common</artifactId>
   <version>${hadoop.version}</version>
  </dependency>
  <!--添加hive依赖-->
  <dependency>
   <groupId>org.apache.hive</groupId>
   <artifactId>hive-exec</artifactId>
   <version>${hive.version}</version>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
     <!--这部分可有可无,加上的话则直接生成可运行jar包-->
     <!--<archive>-->
      <!--<manifest>-->
       <!--<mainClass>填写自己的组织名称.PhoneUnlookUdf</mainClass>-->
      <!--</manifest>-->
     <!--</archive>-->
     <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
    </configuration>
    <executions>
     <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
       <goal>single</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>

</project>

2、UDF 函数

package 填写自己的组织名称;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;

// 上传udf jar到集群 hdfs dfs -put udf-1.0-SNAPSHOT-jar-with-dependencies.jar /data/data_coe/data_asset/prod/db/tmp/udf/
// 修改文件权限 hdfs dfs -chmod -R 777 hdfs://idc-nn/data/data_coe/data_asset/prod/db/tmp/udf/
//注册udf函数 create function tmp.pul as '填写自己的组织名称.PhoneUnlookUdf' using jar 'hdfs://idc-nn/data/data_coe/data_asset/prod/db/tmp/udf/udf-1.0-SNAPSHOT-jar-with-dependencies.jar

public class PhoneUnlookUdf extends UDF {
//重写evaluate方法
 public String evaluate(String phone){
  if (phone.length() == 11){
   String res = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
   System.out.println(res);
   return res;
  } else {
   return phone;
  }

 }
}

3、 利用idea打包

先点clean,在点package

idea打包

4、添加hive udf函数

集群的某些问题,不能直接通过添加服务器上本地文件到hive增加udf;需要将文件上传到hdfs,然后定义udf函数。

4.1 上传jar包到集群

// 上传udf jar到集群 hdfs dfs -put udf-1.0-SNAPSHOT-jar-with-dependencies.jar /data/data_coe/data_asset/prod/db/tmp/udf/

4.2 修改集群hdfs文件权限

// 修改文件权限 hdfs dfs -chmod -R 777 hdfs://idc-nn/data/data_coe/data_asset/prod/db/tmp/udf/

4.3 注册UDF

 //注册udf函数 create function tmp.pul as 'cn.mcd.com.PhoneUnlookUdf' using jar 'hdfs://idc-nn/data/data_coe/data_asset/prod/db/tmp/udf/udf-1.0-SNAPSHOT-jar-with-dependencies.jar

4.4 使用UDF

···
打开集群hive客户端:
select tmp.pul(phone) from tmp.tmp_order limit 3;
···

作者:大码王

-------------------------------------------

个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•?ω•?)っ???!

原文地址:https://www.cnblogs.com/huanghanyu/p/13637245.html