【转载】Java嵌入Pig编程
转自:https://wiki.apache.org/pig/EmbeddedPig
Embedding Pig In Java Programs
Sometimes you want more control than Pig scripts can give you. If so, you can embed Pig Latin in Java (just like SQL can be embedded in programs using JDBC).
The following steps need to be carried out:
Make sure pig.jar is on your classpath.
Create an instance of PigServer. See Javadoc for more details.
Issue commands through that PigServer by calling PigServer.registerQuery().
To retrieve results, either call PigServer.openIterator() or PigServer.store().
If you have user defined functions, register them by calling PigServer.registerJar().
Example
Let's assume you need to count the number of occurrences of each word in a document. Let's also assume that you have EvalFunction Tokenize that parses a line of text and returns all the words for that line. The function is located in /mylocation/tokenize.jar.
The PigLatin script for the computation will look like this:
register /mylocation/tokenize.jar
A = load 'mytext' using TextLoader();
B = foreach A generate flatten(tokenize($0));
C = group B by $1;
D = foreach C generate flatten(group), COUNT(B.$0);
store D into 'myoutput';
The same computation can be performed with this Java program:
import java.io.IOException;
import org.apache.pig.PigServer; public class WordCount {
public static void main(String[] args) { PigServer pigServer = new PigServer(); try {
pigServer.registerJar("/mylocation/tokenize.jar");
runMyQuery(pigServer, "myinput.txt";
}
catch (IOException e) {
e.printStackTrace();
}
} public static void runMyQuery(PigServer pigServer, String inputFile) throws IOException {
pigServer.registerQuery("A = load '" + inputFile + "' using TextLoader();");
pigServer.registerQuery("B = foreach A generate flatten(tokenize($0));");
pigServer.registerQuery("C = group B by $1;");
pigServer.registerQuery("D = foreach C generate flatten(group), COUNT(B.$0);"); pigServer.store("D", "myoutput");
}
}
Notes:
- The jar which contains your functions must be registered.
The four calls to pigServer.registerQuery() simply cause the query to be parsed and enquired. The query is not actually executed until pigServer.store() is called.
- The input data referred to on the load statement, must be on HDFS in the specified location.
The final result is placed into myoutput file in the your current working directory on HDFS. (By default this is your home directory on HDFS.)
To run your program, you need to first compile it by using the following command:
javac -cp <path>pig.jar WordCount.java
If the compilation is successful, you can then run your program:
java -cp <path>pig.jar WordCount
【转载】Java嵌入Pig编程的更多相关文章
- java图形化编程
转载 学习Java Swing图形化编程,我们首先要了解三个最基本的概念:顶层容器,控件,布局. 下面就来介绍一下这三个基本概念 1.顶层容器 什么是顶层容器?当我们使用Java进行图形编程的时候,图 ...
- java matlab混合编程之返回值Struct类型
java matlab混合编程的时候当返回值是Struct类型(matlab中的返回类型)如何来取得(java中)其值? 上网找,看到这个网页:http://www.mathworks.cn/cn/h ...
- Java 脚本化编程指南
Java 脚本化编程指南 Java脚本化API为谁准备? 脚本语言的一些有用的特性是: 方便:大多数脚本语言都是动态类型的.您通常可以创建新的变量,而不声明变量类型,并且您可以重用变量来存储不同类型的 ...
- 【Socket】Java Socket基础编程
Socket是Java网络编程的基础,了解还是有好处的, 这篇文章主要讲解Socket的基础编程.Socket用在哪呢,主要用在进程间,网络间通信.本篇比较长,特别做了个目录: 一.Socket通信基 ...
- java基础-网络编程(Socket)技术选型入门之NIO技术
java基础-网络编程(Socket)技术选型入门之NIO技术 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.传统的网络编程 1>.编写socket通信的MyServer ...
- 《转载》Python并发编程之线程池/进程池--concurrent.futures模块
本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...
- [转载] java的书
1. Java 语言基础 谈到Java 语言基础学习的书籍,大家肯定会推荐Bruce Eckel 的<Thinking in Java >.它是一本写的相当深刻的技术书籍,Java 语言基 ...
- Java入门网络编程-使用UDP通信
程序说明: 以下代码,利用java的网络编程,使用UDP通信作为通信协议,描述了一个简易的多人聊天程序,此程序可以使用公网或者是局域网进行聊天,要求有一台服务器.程序一共分为2个包,第一个包:udp, ...
- Java Stream函数式编程案例图文详解
导读 作者计划把Java Stream写成一个系列的文章,本文只是其中一节.更多内容期待您关注我的号! 一.什么是Java Stream? Java Stream函数式编程接口最初是在Java 8中引 ...
随机推荐
- Sass环境安装-Sass sublime 编辑器插件编译方法
首先官网(http://www.ruby-lang.org/en/downloads/)下载 ruby (1)打开链接进入到下载页面,点击如下位置进行下载 (2)下载页面 (3)进入到各个版本的列表页 ...
- tensor数据基操----索引与切片
玩过深度学习图像处理的都知道,对于一张分辨率超大的图片,我们往往不会采取直接压平读入的方式喂入神经网络,而是将它切成一小块一小块的去读,这样的好处就是可以加快读取速度并且减少内存的占用.就拿医学图像处 ...
- 第60届IMO 第5题
题目 巴斯银行发行的硬币在一面上铸有H,在另一面上铸有T,哈利有枚这样的硬币并将这些硬币从左至 右排成一行,他反复地进行如下操作:如果恰有k(>0)枚硬币H面朝上,则他将从左至右的第k枚硬币 ...
- webkit 技术内幕 笔记 一
目前应用最广的渲染引擎项目-webkit webkit 内部渲染html网页的原理 webkit 内容工作原理 web 的接入口浏览器 浏览器的内核-渲染引擎(像webkit)
- java使用JDBC连接hive(使用beeline与hiveserver2)
首先虚拟机上已经安装好hive. 下面是连接hive需要的操作. 一.配置. 1.查找虚拟机的ip 输入 ifconfig 2.配置文件 (1)配置hadoop目录下的core-site.xml和hd ...
- nodepad++ | 变成 _
点击右下角切换
- 转载:polyphase filter
http://www.ws.binghamton.edu/fowler/fowler%20personal%20page/ee521.htm http://www.ws.binghamton.edu/ ...
- window snmp
https://blog.csdn.net/weixin_30367543/article/details/99923014 https://jingyan.baidu.com/article/e3c ...
- centos 部署 aspnetMVC 网页
在Linux上运行ASP.NET网站或WebApi的传统步骤是,先安装libgdiplus,再安装mono,然后安装Jexus.在这个过程中,虽然安装Jexus是挺简便的一件事,但是安装mono就相对 ...
- vue使用饿了么element-ui框架中的上传组件进度条无法使用,:on-progress钩子无法触发的原因
自己写的例子都是好好的,调试了半天,在项目里怎么都出不来 最终终于找到原因: 在上传文件时要做进度显示需要用到xhr.upload.onprogress事件,此时如果你的项目里用到mock.js模拟数 ...