可能会有一些截图中会有错误提示,是因为本地的包一直包下载有问题,截完图已经下载好了。
创建包结构
![10762a7a533efbcb2a740c1a335978e7.png](https://img-blog.csdnimg.cn/img_convert/10762a7a533efbcb2a740c1a335978e7.png)
创建一个基础信息类
所有输出到mysql数据库中的自定义MR任务的自定义key均需要实现该抽象类
![8c2ebef9faa14cffef01bff4841d8c1e.png](https://img-blog.csdnimg.cn/img_convert/8c2ebef9faa14cffef01bff4841d8c1e.png)
代码内容,主要是实现org.apache.hadoop.io.WritableComparable类,其它不需要写
![7be0489cfeb31f461d81b73986f26b85.png](https://img-blog.csdnimg.cn/img_convert/7be0489cfeb31f461d81b73986f26b85.png)
创建平台信息类
![d37e58ecb9c01b5083dad66359b999cc.png](https://img-blog.csdnimg.cn/img_convert/d37e58ecb9c01b5083dad66359b999cc.png)
创建全局配置常数类
![c3d884f9d21a82cd99a6e5b1939f2361.png](https://img-blog.csdnimg.cn/img_convert/c3d884f9d21a82cd99a6e5b1939f2361.png)
后面慢慢添加内容
![4258228c4b4f38a966900abd2ff4564a.png](https://img-blog.csdnimg.cn/img_convert/4258228c4b4f38a966900abd2ff4564a.png)
继续完成PlatformInfoKey,添加构造函数
![4f0c99665f8ae64dcbce244946e48461.png](https://img-blog.csdnimg.cn/img_convert/4f0c99665f8ae64dcbce244946e48461.png)
![60bc51602e8b0f27f847280404e9d1aa.png](https://img-blog.csdnimg.cn/img_convert/60bc51602e8b0f27f847280404e9d1aa.png)
常数类添加信息
![9e5b45afcdfaacf7347e6ceaa2e13717.png](https://img-blog.csdnimg.cn/img_convert/9e5b45afcdfaacf7347e6ceaa2e13717.png)
根据给定的参数值,构建多个不同维度的平台维度对象
完成参数验证
![d030dccc8301acbe795d1ddd4fc4d4a1.png](https://img-blog.csdnimg.cn/img_convert/d030dccc8301acbe795d1ddd4fc4d4a1.png)
然后构建平台信息
![21c7fc085c3a18f66deb54416b1a067e.png](https://img-blog.csdnimg.cn/img_convert/21c7fc085c3a18f66deb54416b1a067e.png)
添加write和readFields方法
![e6b21d22afe89dbf29355a11f4e2b14e.png](https://img-blog.csdnimg.cn/img_convert/e6b21d22afe89dbf29355a11f4e2b14e.png)
创建compareTo方法
![a0a8764d7725e2488f2da8d0fc59fa9b.png](https://img-blog.csdnimg.cn/img_convert/a0a8764d7725e2488f2da8d0fc59fa9b.png)
添加get/set、hashCode、toString、equals方法,自动生成就可以
![383edfd7362aabf5a70a732688bd40c9.png](https://img-blog.csdnimg.cn/img_convert/383edfd7362aabf5a70a732688bd40c9.png)
创建信息业务接口和实现类
![0f85dd9736a685bf07b3c61feb57ecd2.png](https://img-blog.csdnimg.cn/img_convert/0f85dd9736a685bf07b3c61feb57ecd2.png)
完成接口
![ca081b0a1616f56ec4cf65fd8dcc196e.png](https://img-blog.csdnimg.cn/img_convert/ca081b0a1616f56ec4cf65fd8dcc196e.png)
添加实现类
![f73175ec10828854bb69613d6c9b2bb2.png](https://img-blog.csdnimg.cn/img_convert/f73175ec10828854bb69613d6c9b2bb2.png)
创建一个jdbc的管理器
![815f2d2efa29850e817db3822b257824.png](https://img-blog.csdnimg.cn/img_convert/815f2d2efa29850e817db3822b257824.png)
常数类添加数据库配置
![ae361c0405f6cc83f89bba7a6dbfb622.png](https://img-blog.csdnimg.cn/img_convert/ae361c0405f6cc83f89bba7a6dbfb622.png)
JdbcManager添加jdbc连接
![c69cd4c209d71c52cd57222ea534dc35.png](https://img-blog.csdnimg.cn/img_convert/c69cd4c209d71c52cd57222ea534dc35.png)
关闭数据库连接
![bf604d96043310cf62785adf91af6811.png](https://img-blog.csdnimg.cn/img_convert/bf604d96043310cf62785adf91af6811.png)
继续完成BaseInfoConverterImpl,添加默认构造函数
![774ba01bdf79aeffa24281d36f0d483f.png](https://img-blog.csdnimg.cn/img_convert/774ba01bdf79aeffa24281d36f0d483f.png)
添加一个缓存数据类型
![b82b74d6a340bbbb0159c436cc6d9dcb.png](https://img-blog.csdnimg.cn/img_convert/b82b74d6a340bbbb0159c436cc6d9dcb.png)
继续完成BaseInfoConverterImpl,添加方法创建cache key
![203aaa02b389cc33675c922079c5c684.png](https://img-blog.csdnimg.cn/img_convert/203aaa02b389cc33675c922079c5c684.png)
创建获取ID的方法
![3fbc7156f7d6bb12c10daa129c8efdf1.png](https://img-blog.csdnimg.cn/img_convert/3fbc7156f7d6bb12c10daa129c8efdf1.png)
继续编写
![5456672a823927ed0c224ad77984e511.png](https://img-blog.csdnimg.cn/img_convert/5456672a823927ed0c224ad77984e511.png)
添加下面的代码
![1a33a3725b64bf34edf5d88690e51116.png](https://img-blog.csdnimg.cn/img_convert/1a33a3725b64bf34edf5d88690e51116.png)
补全里面的sql语句
![229a14c600eacf0efefbbd0692beb535.png](https://img-blog.csdnimg.cn/img_convert/229a14c600eacf0efefbbd0692beb535.png)
添加两个参数
![4f51e8d4e9820547687fa2acd13c2c8a.png](https://img-blog.csdnimg.cn/img_convert/4f51e8d4e9820547687fa2acd13c2c8a.png)
完成getConnection()
![d43287762467066c770bec43a29e11d5.png](https://img-blog.csdnimg.cn/img_convert/d43287762467066c770bec43a29e11d5.png)
![7838d7950f60ee93a1e498c982c471a6.png](https://img-blog.csdnimg.cn/img_convert/7838d7950f60ee93a1e498c982c471a6.png)
完成executeSql
![8e75aa21131e69d4c87413321256dc1e.png](https://img-blog.csdnimg.cn/img_convert/8e75aa21131e69d4c87413321256dc1e.png)
![948c7c8fdd60272bccdc8712f1754398.png](https://img-blog.csdnimg.cn/img_convert/948c7c8fdd60272bccdc8712f1754398.png)
![598d9d4d5c2d64bd8e60fd5b1275d821.png](https://img-blog.csdnimg.cn/img_convert/598d9d4d5c2d64bd8e60fd5b1275d821.png)
![3f664b50b5f6e3aba19530c5e7aeb9e2.png](https://img-blog.csdnimg.cn/img_convert/3f664b50b5f6e3aba19530c5e7aeb9e2.png)
设置参数
![d8c9e13dad6f293c7de41e49d40991f3.png](https://img-blog.csdnimg.cn/img_convert/d8c9e13dad6f293c7de41e49d40991f3.png)
添加关闭方法
![56f5dbc4b0e662642a7225bfd1843502.png](https://img-blog.csdnimg.cn/img_convert/56f5dbc4b0e662642a7225bfd1843502.png)
创建一个UDF,模拟数据库平台数据记录
![716cfa31f0f0ee0b27d92f16523a7665.png](https://img-blog.csdnimg.cn/img_convert/716cfa31f0f0ee0b27d92f16523a7665.png)
添加内容
![fb8f8c4fd8c54ab68e087dd046a418cd.png](https://img-blog.csdnimg.cn/img_convert/fb8f8c4fd8c54ab68e087dd046a418cd.png)
添加evaluate方法
![2b28692030a4885ccf8e3412e1a28820.png](https://img-blog.csdnimg.cn/img_convert/2b28692030a4885ccf8e3412e1a28820.png)
创建一个测试类
![e4c4ca6960fbf0b6d7720704dd7d6590.png](https://img-blog.csdnimg.cn/img_convert/e4c4ca6960fbf0b6d7720704dd7d6590.png)
创建数据库
create database reportDROP TABLE IF EXISTS `platform`;CREATE TABLE `platform` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',`platform_name` varchar(45) DEFAULT NULL COMMENT '平台名称',`platform_version` varchar(10) DEFAULT NULL COMMENT '平台版本',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='平台信息表';
![7b058f988b91a2ae7a02ee84336f08cb.png](https://img-blog.csdnimg.cn/img_convert/7b058f988b91a2ae7a02ee84336f08cb.png)
Sql语句
String querySql = "SELECT `id` FROM `platform` WHERE `platform_name` = ? AND `platform_version` = ? order by `id`";String insertSql = "INSERT INTO `platform`(`platform_name`, `platform_version`) VALUES(?, ?)";
![fe435707d25b1e41695ccadeb481bf7c.png](https://img-blog.csdnimg.cn/img_convert/fe435707d25b1e41695ccadeb481bf7c.png)
修改配置:
连接本地数据库
![a654cecd9f9a12c5ba1f283ec33f5d7d.png](https://img-blog.csdnimg.cn/img_convert/a654cecd9f9a12c5ba1f283ec33f5d7d.png)
运行结果
![d9fb25affced05ad47f1f6e2de87fa13.png](https://img-blog.csdnimg.cn/img_convert/d9fb25affced05ad47f1f6e2de87fa13.png)
数据库中
![6741430469cb41e1374ddf7e7b96406c.png](https://img-blog.csdnimg.cn/img_convert/6741430469cb41e1374ddf7e7b96406c.png)
本地测试成功开始打包项目放进集群进行测试
![6a5f089f8484b54e4d2945b1e439e9e3.png](https://img-blog.csdnimg.cn/img_convert/6a5f089f8484b54e4d2945b1e439e9e3.png)
![5003b1637fc45c0899744be40f968f1e.png](https://img-blog.csdnimg.cn/img_convert/5003b1637fc45c0899744be40f968f1e.png)
把打包好的jar包改名为hive_udf3.jar放到集群里
![fcc561df52b7c49495d070fdbe0e67c3.png](https://img-blog.csdnimg.cn/img_convert/fcc561df52b7c49495d070fdbe0e67c3.png)
进入MySQL中创建report数据库
create database report;
![97c338a4aab9864bbe0f7bebb4530ed4.png](https://img-blog.csdnimg.cn/img_convert/97c338a4aab9864bbe0f7bebb4530ed4.png)
创建表platform
![ccb46b6291835f415265a679e1ca79d5.png](https://img-blog.csdnimg.cn/img_convert/ccb46b6291835f415265a679e1ca79d5.png)
![13fe754b2a771687ebefd57164c69c77.png](https://img-blog.csdnimg.cn/img_convert/13fe754b2a771687ebefd57164c69c77.png)
进入hive
![25672881a410a72b1632d107c82196e3.png](https://img-blog.csdnimg.cn/img_convert/25672881a410a72b1632d107c82196e3.png)
把jar包添加到hive当中
![86b3ce216992991547f80860a82a6dd8.png](https://img-blog.csdnimg.cn/img_convert/86b3ce216992991547f80860a82a6dd8.png)
可以用list jar 和 delete jar 分别显示jar和删除jar
![c2e7075eef48a7b8bcf1f792fe1b1b38.png](https://img-blog.csdnimg.cn/img_convert/c2e7075eef48a7b8bcf1f792fe1b1b38.png)
创建临时函数
create temporary function convert_bl as 'com.xlgl.wzy.hive.udf.PlatformConverterUDF';
![68875a5f63a3a48266db1cfe6eec3d10.png](https://img-blog.csdnimg.cn/img_convert/68875a5f63a3a48266db1cfe6eec3d10.png)
我们可以用show_functions查看函数
![d04357b47b81bb38074d3dd1dc664658.png](https://img-blog.csdnimg.cn/img_convert/d04357b47b81bb38074d3dd1dc664658.png)
我们查看已有表的数据
![aa62530ca473c98c5727f5959c129b3d.png](https://img-blog.csdnimg.cn/img_convert/aa62530ca473c98c5727f5959c129b3d.png)
select ename,convert_bl(ename,0) lower_name from emp;
![aba0a1973dd3a0debec626a95845e35c.png](https://img-blog.csdnimg.cn/img_convert/aba0a1973dd3a0debec626a95845e35c.png)
这里报了一个异常
这个问题可能是出在maven给的版本和hive的版本不一样导致可以替换成自己的版本
![12357ccfb532aa54cb71f72561731a2a.png](https://img-blog.csdnimg.cn/img_convert/12357ccfb532aa54cb71f72561731a2a.png)
![21afa17e896d5f26593d0f258446aa87.png](https://img-blog.csdnimg.cn/img_convert/21afa17e896d5f26593d0f258446aa87.png)
然后重新打成jar包运行一下
再查询一下
select ename,convert_bl(ename,0) lower_name from emp;
![bee00c899e9c760ef101ecb384479373.png](https://img-blog.csdnimg.cn/img_convert/bee00c899e9c760ef101ecb384479373.png)
![8dbcd24fdaf5e062fbfbce91e45b2982.png](https://img-blog.csdnimg.cn/img_convert/8dbcd24fdaf5e062fbfbce91e45b2982.png)
可以在mysql中查看数据
![d02d2af993b416d12ddc65a3cde67f24.png](https://img-blog.csdnimg.cn/img_convert/d02d2af993b416d12ddc65a3cde67f24.png)