大数据 java hive udf函数的示例代码(手机号码脱敏)
weixin_41734687 人气:2本文着重讲解了大数据 java hive 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> <https://img.qb5200.com/download-x/dependency> <!--添加hive依赖--> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>${hive.version}</version> <https://img.qb5200.com/download-x/dependency> <https://img.qb5200.com/download-x/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<https://img.qb5200.com/download-x/descriptorRef> <https://img.qb5200.com/download-x/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 https://img.qb5200.com/download-x/datahttps://img.qb5200.com/download-x/data_coehttps://img.qb5200.com/download-x/data_asset/prodhttps://img.qb5200.com/download-x/db/tmp/udf/ // 修改文件权限 hdfs dfs -chmod -R 777 hdfs://idc-nnhttps://img.qb5200.com/download-x/datahttps://img.qb5200.com/download-x/data_coehttps://img.qb5200.com/download-x/data_asset/prodhttps://img.qb5200.com/download-x/db/tmp/udf/ //注册udf函数 create function tmp.pul as '填写自己的组织名称.PhoneUnlookUdf' using jar 'hdfs://idc-nnhttps://img.qb5200.com/download-x/datahttps://img.qb5200.com/download-x/data_coehttps://img.qb5200.com/download-x/data_asset/prodhttps://img.qb5200.com/download-x/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
4 添加hive udf函数
集群的某些问题,不能直接通过添加服务器上本地文件到hive增加udf;需要将文件上传到hdfs,然后定义udf函数。
4.1 上传jar包到集群
// 上传udf jar到集群 hdfs dfs -put udf-1.0-SNAPSHOT-jar-with-dependencies.jar https://img.qb5200.com/download-x/datahttps://img.qb5200.com/download-x/data_coehttps://img.qb5200.com/download-x/data_asset/prodhttps://img.qb5200.com/download-x/db/tmp/udf/
4.2 修改集群hdfs文件权限
// 修改文件权限 hdfs dfs -chmod -R 777 hdfs://idc-nnhttps://img.qb5200.com/download-x/datahttps://img.qb5200.com/download-x/data_coehttps://img.qb5200.com/download-x/data_asset/prodhttps://img.qb5200.com/download-x/db/tmp/udf/
4.3 注册UDF
//注册udf函数 create function tmp.pul as 'cn.mcd.com.PhoneUnlookUdf' using jar 'hdfs://idc-nnhttps://img.qb5200.com/download-x/datahttps://img.qb5200.com/download-x/data_coehttps://img.qb5200.com/download-x/data_asset/prodhttps://img.qb5200.com/download-x/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;
···
总结
加载全部内容