pig 自定义udf中读取hdfs 文件
最近几天,在研究怎么样把日志中的IP地址转化成具体省份城市。
希望写一个pig udf
IP数据库采用的纯真IP数据库文件qqwry.dat,可以从http://www.cz88.net/下载。
这里关键点在于怎么样读取这个文件,浪费了二天时间,现在把代码记录下来供和我遇到相同问题的朋友参考。
pig script
register /usr/local/pig/mypigudf.jar;
define ip2address my.pig.func.IP2Address('/user/anny/qqwry.dat'); a = load '/user/anny/hdfs/logtestdata/ipdata.log' as (ip:chararray);
b = foreach a generate ip,ip2address(ip) as cc:map[chararray];
c = foreach b generate ip,cc#'province' as province,cc#'city' as city,cc#'region' as region;
dump c;
java写的pig udf:
package my.pig.func; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import my.pig.func.IPConvertCity.IPSeeker;
import my.pig.func.IPConvertCity.IPUtil;
import my.pig.func.IPConvertCity.LogFactory; import org.apache.log4j.Level;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.impl.logicalLayer.schema.Schema; public class IP2Address extends EvalFunc<Map<String, Object>> {
private String lookupFile = "";
private RandomAccessFile objFile = null; public IP2Address(String file) {
this.lookupFile = file;
} @Override
public Map<String, Object> exec(Tuple input) throws IOException {
if (input == null || input.size() == 0 || input.get(0) == null)
return null;
Map<String, Object> output = new HashMap<String, Object>();
String str = (String) input.get(0);
try {
if (str.length() == 0)
return output; if (objFile == null) {
try {
objFile = new RandomAccessFile("./qqwry.dat", "r");
} catch (FileNotFoundException e1) {
System.out.println("IP地址信息文件没有找到" + lookupFile);
return null;
}
}
IPSeeker seeker = new IPSeeker(objFile);
String country = seeker.getCountry(str);
output = IPUtil.splitCountry(country); return output;
} catch (Exception e) {
return output;
}
} @Override
public Schema outputSchema(Schema input) {
return new Schema(new Schema.FieldSchema(null, DataType.MAP));
} public List<String> getCacheFiles() {
List<String> list = new ArrayList<String>(1);
list.add(lookupFile + "#qqwry.dat");
return list;
}
}
Search for "Distributed Cache" in this page of the Pig docs: http://pig.apache.org/docs/r0.11.0/udf.html
The example it shows using the getCacheFiles() method should ensure that the file is accessible to all the nodes in the cluster.
参考文章:http://stackoverflow.com/questions/17514022/access-hdfs-file-from-udf
http://stackoverflow.com/questions/19149839/pig-udf-maxmind-geoip-database-data-file-loading-issue
pig 自定义udf中读取hdfs 文件的更多相关文章
- 在spark udf中读取hdfs上的文件
某些场景下,我们在写UDF实现业务逻辑时候,可能需要去读取某个文件. 我们可以将此文件上传个hdfs某个路径下,然后通过hdfs api读取该文件,但是需要注意: UDF中读取文件部分最好放在静态代码 ...
- Spark设置自定义的InputFormat读取HDFS文件
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/problem_spark_reading_hdfs_serial ...
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- Spark读取HDFS文件,任务本地化(NODE_LOCAL)
Spark也有数据本地化的概念(Data Locality),这和MapReduce的Local Task差不多,如果读取HDFS文件,Spark则会根据数据的存储位置,分配离数据存储最近的Execu ...
- 记录一次读取hdfs文件时出现的问题java.net.ConnectException: Connection refused
公司的hadoop集群是之前的同事搭建的,我(小白一个)在spark shell中读取hdfs上的文件时,执行以下指令 >>> word=sc.textFile("hdfs ...
- cocos2d-x 3.0rc2中读取sqlite文件
cocos2d-x 3.0rc2中读取sqlite文件的方式,在Android中直接读取软件内的会失败.须要复制到可写的路径下 sqlite3* dbFile = NULL; std::string ...
- Spark读取HDFS文件,文件格式为GB2312,转换为UTF-8
package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.hadoop.conf.Configur ...
- 【解惑】深入jar包:从jar包中读取资源文件
[解惑]深入jar包:从jar包中读取资源文件 http://hxraid.iteye.com/blog/483115 TransferData组件的spring配置文件路径:/D:/develop/ ...
- java 从jar包中读取资源文件
在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...
随机推荐
- 设置 vadio 和checkbox是否选中
1.js方案 <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>te ...
- fastdfs安装与配置
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...
- bzoj 3657 斐波那契数列(fib.cpp/pas/c/in/out)
空间 512M 时限2s [题目描述] 有n个大于1的正整数a1,a2,…,an,我们知道斐波那契数列的递推式是f(i)=f(i-1)+f(i-2),现在我们修改这个递推式变为f(i)=f(i-1) ...
- asm-3.3.1.jar详解 (转)
Java字节码操纵框架.它可以直接以二进制形式动态地生成stub类或其他代理类,或者在装载时动态地修改类.ASM提供类似于BCEL和SERP之类的工具包的功能,但是被设计得更小巧.更快速,这使它适用于 ...
- Windows窗体应用程序常用的几个类的属性、方法以及事件
System.Diagnostics.Process 属性 public bool EnableRaisingEvents { get; set; }//获取或设置在进程终止时是否应激发 Exited ...
- STL list用法总结
2017-08-20 15:17:30 writer:pprp list是一种线性复杂度的容器,很快 /* name : usage of List writer : pprp declare : n ...
- eclipse 打开 maven项目,出现红色的感叹号
解决: 选中有感叹号的项目右键,用maven>>update project解决.[然后我再添加java system 类库就可以了]
- JavaScript对象、JSON对象、JSON字符串的区别
一.首先看下什么是JSON JSON:JavaScript Object Natation,JavaScript对象的表现形式,已经发展成一种轻量级的数据交换格式. JavaScript对象的表现形式 ...
- JavaScript设计模式与开发实践:惰性函数
Web开发中,因为浏览器之间的差异实现差异,一些嗅探工作总是不可避免的,比如我们需要在各个浏览器中能够通用事件绑定函数addEvent //一般写法 //缺点:当他每次被调用的时候都都会执行里面的if ...
- 嵌入式--arm
两年前的东西了,整理一下,说不定以后就会用到了. arm对于s3c2440的这个arm的驱动的整理. 其中包括:adc,beeper 蜂鸣器,key 按键,rtc ,timer定时器,UART等的驱动 ...