【转载】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中引 ...
随机推荐
- 论Flaks与Django的区别
1. jiaji2和DjangoTemplates模板引擎相比,jiaja2语法更简单 2. 耦合 3. 模型 3.1 模型定义 3.2 模型数据查询 Django: 自带ORM(Object-Rel ...
- 【读书笔记】自然语言处理综述 -- 第四章 -- N元语法
第四章 N元语法 本章开篇的两句话很有意思,代表了当时两个学派的思想和矛盾. 一句是"有史以来最伟大的语言学家"乔姆斯基说的:"句子的概率,在任何已知的对于这个术语的解释 ...
- Python的入门级试用(简明教程)
声明:借鉴Python 简明教程 用 Python 编写的传统的 'Hello World' 程序.使用 Python 运行你的程序的方法有两种:使用交互式解释器提示符或者使用源文件.现在我们来看一下 ...
- PHP版常用算法
PHP版常用算法最近准备面试的资料,顺便整理一下以前的基本算法,写个DEMO记录一下 //冒泡//逐行对比,满足条件则交换function bubbleSort($arrData,$sort = 'd ...
- python全栈学习 day02
pycharm 安装设置: 按照百度百科或者官网介绍下载,安装. 激活步骤 1:改host 2.输入激活信息,注意有效期. python 逻辑运算符://返回的均为bool值 与 and A and ...
- 剑指offer-面试题16-数值的整数次方-数字
/* 题目: 实现函数double Power(double base,int exponent), 求base的exponent次方. */ /* 思路: 本题需要考虑的情况较多: 1.0的负数次方 ...
- 神经网络反向传播算法&&卷积神经网络
听一遍课程之后,我并不太明白这个算法的奇妙之处?? 为啥? 神经网络反向传播算法 神经网络的训练依靠反向传播算法,最开始输入层输入特征向量,网络层计算获得输出,输出层发现输出和正确的类号不一样,这时就 ...
- LINUX 概述
初识linux Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协 ...
- hdu 2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)
新人题:n2的排序就可以过 #include <stdio.h> #include <stdlib.h> int main() { int c,n,i,j,o; ],b[],m ...
- UML 建模 -- 基础知识
1.UML简介 UML(Unidied Modeling Language)为面向对象软件设计提供统一的,标准的,可视化的建模语言.适用于以用例为驱动,以体系结构为中心的软件设计全程 2.UML模型的 ...