为Hadoop创建JAR包文件Creating a JAR for Hadoop
We've seen the internals of MapReduce in the last post. Now we can make a little change to the WordCount and create a JAR for being executed by Hadoop.
If we look at the result of the WordCount we ran before, the lines of the file are only split by space, and thus all other punctuation characters and symbols remain attached to the words and in some way "invalidate" the count. For example, we can see some of these wrong values:
KING 1
KING'S 1
KING. 5
King 6
King, 4
King. 2
King; 1
The word is always king but sometimes it appears in upper case, sometimes in lower case, or with a punctuation character after it. So we'd like to update the behaviour of the WordCount program to count all the occurrences of any word, aside from the punctuation and other symbols. In order to do so, we have to modify the code of the mapper, since it's here that we get the data from the file and split it.
If we look at the code of mapper:
public static class TokenizerMapper extends Mapper<object, text,="" intwritable=""> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
we see that the StringTokenizer takes the line as the parameter; before doing that, we remove all the symbols from the line using a RegExp that maps each of these symbols into a space:
String cleanLine = value.toString().toLowerCase().replaceAll("[_|$#<>\\[\\]\\*/\\\\,;,.\\-:()?!\"']", " ");
that simply says "if you see any of these character _, |, $, #, <, >, [, ], *, /, \, ,, ;, ., -, :, ,(, ), ?, !, ", ' transform it into a space". All the backslashes are needed for correctly escaping the characters. Then we trim the token to avoid empty tokens:
itr.nextToken().trim()
So now the code looks like this (the updates to the original file are printed in bold):
public static class TokenizerMapper extends Mapper<object, text,="" intwritable=""> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String cleanLine = value.toString().toLowerCase().replaceAll("[_|$#<>\\^=\\[\\]\\*/\\\\,;,.\\-:()?!\"']", " ");
StringTokenizer itr = new StringTokenizer(cleanLine);
while (itr.hasMoreTokens()) {
word.set(itr.nextToken().trim());
context.write(word, one);
}
}
}
The complete code of the class is available on my github repository.
We now want to create the JAR to be executed; to do it, we have to go to the output directory of our wordcount file (the directory that contains the .class files) and create a manifest filenamed manifest_file that contains something like this:
Manifest-Version: 1.0
Main-Class: samples.wordcount.WordCount
in which we tell the JAR which class to execute at startup. More details in Java official documentation. Note that there's no need to add classpath info, because the JAR will be run by Hadoop that already has a classpath that contains all needed libraries.
Now we can launch the command:
$ jar cmf manifest_file wordcount.jar .
that creates a JAR named wordcount.jar that contains all classes starting from this directory (the . parameter) e using the content of manifest_file to create a Manifest.
We can run the batch as we saw before. Looking at the result file we can check that there are no more symbols and punctuation characters; the occurrences of the word king is now the sum of all occurrences we found before:
king 20
which is what we were looking for.
from: http://andreaiacono.blogspot.com/2014/02/creating-jar-for-hadoop.html
为Hadoop创建JAR包文件Creating a JAR for Hadoop的更多相关文章
- 有引用外部jar包时(J2SE)生成jar文件
一.工程没有引用外部jar包时(J2SE) 选中工程---->右键,Export...--->Java--->选择JAR file--->next-->选择jar fil ...
- jmeter,badboy,jar包文件 常数吞吐量计时器?
badboy录制脚本 1.按f2 红色开始录制 URL输入:https://www.so.com/ 2.搜索框输入zxw 回车键搜索 3.选中关键字(刮例如zxw软件——>tools——> ...
- spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途
Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...
- Eclipse用Runnable JAR file方式打jar包,并用该jar包进行二次开发
目录: 1.eclipse创建Java项目(带jar包的) 2. eclipse用Export的Runnable JAR file方式打jar包(带jar包的) 打jar包 1)class2json1 ...
- 多个jar包合并成一个jar包(ant)
https://blog.csdn.net/gzl003csdn/article/details/53539133 多个jar包合并成一个jar 使用Apache的Ant是一个基于Java的生成工具. ...
- spring boot:在项目中引入第三方外部jar包集成为本地jar包(spring boot 2.3.2)
一,为什么要集成外部jar包? 不是所有的第三方库都会上传到mvnrepository, 这时我们如果想集成它的第三方库,则需要直接在项目中集成它们的jar包, 在操作上还是很简单的, 这里用luos ...
- 多个jar包合并成一个jar包的办法
步骤: 1.将多个JAR包使用压缩软件打开,并将全包名的类拷贝到一个临时目录地下. 2.cmd命令到该临时目录下,此时会有很多.class文件,其中需要带完整包路径 3.执行 jar -cvfM te ...
- Ant打包可运行的Jar包(加入第三方jar包)
本章介绍使用ant打包可运行的Jar包. 打包jar包最大的问题在于如何加入第三方jar包使得jar文件可以直接运行.以下用一个实例程序进行说明. 程序结构: 关键代码: package com.al ...
- 【Maven学习】Maven打包生成普通jar包、可运行jar包、包含所有依赖的jar包
http://blog.csdn.net/u013177446/article/details/54134394 ******************************************* ...
随机推荐
- 深入理解 WordPress 数据库中的用户数据 wp_user
WordPress 使用 wp_users 数据表存储用户的主要数据,该数据表结构类似于wp_posts 和 wp_comments 数据表,存储的是需要经常访问的用户数据,该数据表的结构以及该数据表 ...
- mvc部署
权限中加入windows 2008中加入SERVICE,windows2003中加入NETWORK SERVICE
- 百度地图sdk定位和遇到的坑
封装定位服务类: import android.content.Context; import com.baidu.location.BDAbstractLocationListener; impor ...
- Python写网络爬虫爬取腾讯新闻内容
最近学了一段时间的Python,想写个爬虫,去网上找了找,然后参考了一下自己写了一个爬取给定页面的爬虫. Python的第三方库特别强大,提供了两个比较强大的库,一个requests, 另外一个Bea ...
- spring+atomikos+mybatis 多数据源事务(动态切换)
注:自动切换,是为不同的数据源,却要对应相同的dao层: 1.与无事务版的一样,创建DynamicDataSource类,继承AbstractRoutingDataSource package com ...
- list 往指定的下标插入元素
list 往指定的下标插入元素 import java.util.*; public class ListExample{ public static void main(String[] args) ...
- 如何安装及使用PuTTY
http://www.ytyzx.net/index.php?title=%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85%E5%8F%8A%E4%BD%BF%E7%94%A8 ...
- LOJ#2471「九省联考 2018」一双木棋 MinMax博弈+记搜
题面 戳这里 题解 因为每行取的数的个数是单调不增的,感觉状态数不会很多? 怒而记搜,结果过了... #include<bits/stdc++.h> #define For(i,x,y) ...
- JDK源码(1.7) -- java.util.Queue<E>
java.util.Queue<E> 源码分析(JDK1.7) -------------------------------------------------------------- ...
- Codeforces Round #202 (Div. 1) D. Turtles DP
D. Turtles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/problem/B ...