既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
// 检查参数数量是否为1,如果不是则抛出异常
if (arguments.length != 1) {
throw new UDFArgumentLengthException(
"The function iptoloc(ip) takes exactly 1 argument.");
}
// 检查第一个参数是否为StringObjectInspector类型,如果不是则抛出异常
if (!(arguments[0] instanceof StringObjectInspector)) {
throw new UDFArgumentTypeException(0,
"The argument must be a string, but " + arguments[0].getTypeName()
+ " was given.");
}
// 将第一个参数设置为字符串类型的ObjectInspector
this.stringInspector = (StringObjectInspector) arguments[0];
// 设置输出类型为字符串,通过反射创建ObjectInspector实例
this.outputOI = ObjectInspectorFactory.getReflectionObjectInspector(String.class,
ObjectInspectorFactory.ObjectInspectorOptions.JAVA);
// 打印输出类型的ObjectInspector信息
System.out.println(outputOI);
return outputOI; // 返回输出类型的ObjectInspector,供其他方法使用
}
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
// 获取传入的IP地址参数
String ip = stringInspector.getPrimitiveJavaObject(arguments[0].get());
// 从指定文件中读取地区信息
//File file = new File("/root/eduhq/data/area.json");
File file = new File("/resources/area.json");
ObjectMapper mapper = new ObjectMapper();
List<String> provinces = new ArrayList<>();
List<String> cities = new ArrayList<>();
try {
// 解析JSON文件
JsonNode rootNode = mapper.readTree(file);
// 遍历JSON节点,获取省份和城市信息
for (JsonNode node : rootNode) {
String province = node.path("province").getTextValue();
String city = node.path("city").getTextValue();
provinces.add(province);
cities.add(city);
}
} catch (IOException e) {
throw new HiveException("Failed to read area.json file: " + e.getMessage(), e);
}
// 根据IP地址进行分类标注
int index = ipToIndex(ip);
String province = provinces.get(index);
String city = cities.get(index);
// 返回分类标注结果
ObjectNode result = mapper.createObjectNode();
result.put("province", province);
result.put("city", city);
return result.toString();
}
@Override
public String getDisplayString(String[] children) {
return "iptoloc(" + children[0] + ")";
}
private int ipToIndex(String ip) {
// 根据IP地址的某种算法得到索引值
// 这里简单地使用IP地址的字符长度模拟算法
return ip.length() % 7;
}
}
org.apache.hive
hive-exec
3.1.2
### (三)任务三:数据统计
**1.子任务一:HDFS 文件上传下载**
( 1 ) 将包“ com.hive.udf ”导出为名为 hive-udf- behavior-1.0.0.jar 的 JAR 文件, 并 保 存在 本地 的
/root/eduhq/udf\_jars 目录中;
**答:**`https://blog.csdn.net/gb4215287/article/details/132793531`
(2)将打包文件 hive-udf-behavior-1.0.0.jar 上传到HDFS 的/hive/udf\_jars 目录下;
**答:**`hadoop fs -put hive-udf-behavior-1.0.0.jar /hive/udf_jars`
(3)在 Hive 客户端,创建永久函数 url\_trans 和get\_city\_by\_ip,并将它们与开发好的 class 相关联;
**答:**
add jar /root/eduhq/udf_jars/hive-udf-behavior-1.0.0.jar
create function url_trans as com.hive.udf.url_trans;
create function get_city_by_ip as com.hive.udf.get_city_by_ip;
(4)在 Hive 客户端,使用 select 语句测试url\_trans和get\_city\_by\_ip 函数;
\*\*答:\*\*根据具体作用使用。例如:
select get_city_by_ip(ip);
(5)启动 Hive 的动态分区功能,并将 Hive 设置为非严格模式;
**答:**
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nostrict;
(6)使用 insert overwrite … select …子句将ods\_behavior\_log 表中数据插入分区表 dwd\_behavior\_log中,并实现根据 dt 进行动态分区。
**答:**`insert overwrite table dwd_behavior_log PARTITION (dt) select *,date_format(dt,'yyyy-MM-dd') from ods_behavior_log;`
**2.子任务二:数据统计**
(1)查看dwd\_behavior\_log表的所有现有分区;
**答:**`SHOW PARTITIONS dwd_behavior_log;`
(2)查看外部表dwd\_behavior\_log的前3行数据,并验证URL协议是否被统一为“http”,以及通过IP是否能够获取到“省份”和“城市”信息;
**答:**
SELECT * FROM dwd_behavior_log LIMIT 3;
SELECT URL FROM dwd_behavior_log WHERE URL LIKE ‘http://%’;
返回所有以“http://”开头的URL。如果返回的行数大于0,则说明URL协议被统一为“http”。
通过ip获取省份,例如:select get_city_by_ip(ip);
(3)统计外部表dwd\_behavior\_log数据总行数。
![img](https://img-blog.csdnimg.cn/img_convert/bddae67717c590de2fb8dc80f5affeb5.png)
![img](https://img-blog.csdnimg.cn/img_convert/02e492c650a6211bfee56b00775aa04c.png)
![img](https://img-blog.csdnimg.cn/img_convert/1ed6c29895430247ed8925534377a703.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**