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 ...
随机推荐
- saltstack之keepalived的安装配置
使用saltstack编译安装keepalived: 创建相应的目录,并在目录下创建相应的sls配置文件 [root@node1 ~]# mkdir /srv/salt/prod/keepalived ...
- openwrt中如何在一个软件包中使能busybox中的工具
答:在软件包的Makefile中定义一个宏Package/package-name/config 举例:笔者自己制作了一个名为hello的软件包,但是这个软件包依赖busybox中的ifdown de ...
- POJ-1458 LCS(线性动态规划)
此题经典线性动态规划. 代码如下: #include<iostream> #include<cstdio> #include<cstdlib> #include&l ...
- ElasticSearch集群故障案例分析: 警惕通配符查询
最近ElasticSearch集群出现了 https://elasticsearch.cn/article/171 文章中描述的情况,现在转载全文警示下自己. 许多有RDBMS/SQL背景的开发者,在 ...
- windows下,python3安装django和mysql驱动
1.安装python3和django (1)Python 下载地址:https://www.python.org/downloads/ (2)Django 下载地址:https://www.djang ...
- mac下搭建基于vue-cli 3.0的Element UI 项目
1.安装yarn管理工具(包含node.js); 2.安装全局vue-cli全家桶: yarn global add @vue/cli 3.创建.测试一个vue-cli项目: vue create a ...
- window 中安装 redis的遇到的一此bug
一.下载 链接:https://github.com/MicrosoftArchive/redis/releases 说明: 如果是是下载msi版本的话会默认帮我们配置好测试 环境,如果是下载zi ...
- 解决 对路径bin\roslyn..的访问被拒绝
使用visual studio开发,一重新编译就会报错: 对路径“bin\roslyn\System.Reflection.Metadata.dll”的访问被拒绝 一开始的解决办法就是把bin下的文件 ...
- codevs 1085 数字游戏 dp或者暴搜
1085 数字游戏 2003年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单 ...
- [Vue]vue中各选项及钩子函数执行顺序
在vue中,实例选项和钩子函数和{{}}表达式都是不需要手动调用就可以直接执行的. 一.生命周期图示 二.vue中各选项及钩子函数执行顺序 1.在页面首次加载执行顺序有如下: beforeCreate ...