eclipse配置hadoop2.7.2开发环境并本地跑起来
先安装并启动hadoop,怎么弄见上文http://www.cnblogs.com/wuxun1997/p/6847950.html。这里说下怎么设置IDE来开发hadoop代码和调试。首先要确保你本地装了eclipse,再下个eclipse的hadoop插件就完事了。下面细说一下:
1、到http://download.csdn.net/detail/wuxun1997/9841487下载eclipse插件并丢到eclipse的pulgin目录下,重启eclipse,Project Explorer出现DFS Locations;
2、点击Window->点Preferences->点Hadoop Map/Reduce->填D:\hadoop-2.7.2并OK;
3、点击Window->点Show View->点MapReduce Tools下的Map/Reduce Locations->点右边角一个带+号的小象图标"New hadoop location"->eclipse已填好默认参数,但以下几个参数需要修改以下,参见上文中的两个配置文件core-site.xml和hdfs-site.xml:
General->Map/Reduce(V2) Master->Port改为9001
General->DSF Master->Port改为9000
Advanced paramters->dfs.datanode.data.dir改为ffile:/hadoop/data/dfs/datanode
Advanced paramters->dfs.namenode.name.dir改为file:/hadoop/data/dfs/namenode
4、点击Finish后在DFS Locations右键点击左边三角图标,出现hdsf文件夹,可以直接在这里操作hdsf,右键点击文件图标选"Create new Dictionery"即可新增,再次右键点击文件夹图标选Reflesh出现新增的结果;此时在localhost:50070->Utilities->Browse the file system也可以看到新增的结果;
5、新建hadoop项目:File->New->Project->Map/Reduce Project->next->输入自己取的项目名如hadoop再点Finish
6、这里的代码演示最常见的分词例子,统计的是中文小说里的人名并降序排列。为了中文分词需要导入一个jar,在这里下载http://download.csdn.net/detail/wuxun1997/9841659。项目结构如下:
hadoop
|--src
|--com.wulinfeng.hadoop.wordsplit
|--WordSplit.java
|--IKAnalyzer.cfg.xml
|--myext.dic
|--mystopword.dic
WordSplit.java
package com.wulinfeng.hadoop.wordsplit; import java.io.IOException;
import java.io.StringReader; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.map.InverseMapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme; public class WordSplit { /**
* map实现分词
* @author Administrator
*
*/
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private static final IntWritable one = new IntWritable(1);
private Text word = new Text(); public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
StringReader input = new StringReader(value.toString());
IKSegmenter ikSeg = new IKSegmenter(input, true); // 智能分词
for (Lexeme lexeme = ikSeg.next(); lexeme != null; lexeme = ikSeg.next()) {
this.word.set(lexeme.getLexemeText());
context.write(this.word, one);
}
}
} /**
* reduce实现分词累计
* @author Administrator
*
*/
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
this.result.set(sum);
context.write(key, this.result);
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String inputFile = "/input/people.txt"; // 输入文件
Path outDir = new Path("/out"); // 输出目录
Path tempDir = new Path("/tmp" + System.currentTimeMillis()); // 临时目录 // 第一个任务:分词
System.out.println("start task...");
Job job = Job.getInstance(conf, "word split");
job.setJarByClass(WordSplit.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(inputFile));
FileOutputFormat.setOutputPath(job, tempDir); // 第一个任务结束,输出作为第二个任务的输入,开始排序任务
job.setOutputFormatClass(SequenceFileOutputFormat.class);
if (job.waitForCompletion(true)) {
System.out.println("start sort...");
Job sortJob = Job.getInstance(conf, "word sort");
sortJob.setJarByClass(WordSplit.class);
sortJob.setMapperClass(InverseMapper.class);
sortJob.setInputFormatClass(SequenceFileInputFormat.class); // 反转map键值,计算词频并降序
sortJob.setMapOutputKeyClass(IntWritable.class);
sortJob.setMapOutputValueClass(Text.class);
sortJob.setSortComparatorClass(IntWritableDecreasingComparator.class);
sortJob.setNumReduceTasks(1); // 输出到out目录文件
sortJob.setOutputKeyClass(IntWritable.class);
sortJob.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(sortJob, tempDir); // 如果已经有out目录,先删再创建
FileSystem fileSystem = outDir.getFileSystem(conf);
if (fileSystem.exists(outDir)) {
fileSystem.delete(outDir, true);
}
FileOutputFormat.setOutputPath(sortJob, outDir); if (sortJob.waitForCompletion(true)) {
System.out.println("finish and quit....");
// 删掉临时目录
fileSystem = tempDir.getFileSystem(conf);
if (fileSystem.exists(tempDir)) {
fileSystem.delete(tempDir, true);
}
System.exit(0);
}
}
} /**
* 实现降序
*
* @author Administrator
*
*/
private static class IntWritableDecreasingComparator extends IntWritable.Comparator {
public int compare(WritableComparable a, WritableComparable b) {
return -super.compare(a, b);
} public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
return -super.compare(b1, s1, l1, b2, s2, l2);
}
}
}
IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">myext.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典 -->
<entry key="ext_stopwords">mystopword.dic</entry>
</properties>
myext.dic
高育良
祁同伟
陈海
陈岩石
侯亮平
高小琴
沙瑞金
李达康
蔡成功
mystopword.dic
你
我
他
是
的
了
啊
说
也
和
在
就
这里直接在eclipse跑WordSplit类,右键选择Run as -> Run on hadoop。上面是输入输出都是本地文件,在D盘建一个input目录,里面放个文件名叫people.txt的小说,是网上荡下来的热剧《人民的名义》。为了分词需要设置文件格式:把people.txt去Notepad++里打开,点编码->以UTF-8以无BOM格式编码。在myext.dic里输入一些不想再拆分的人名,在mystopword.dic输入想要过滤掉的一些谓词和助词,跑完去D:\out里看part-r-00000文件即可知道谁是猪脚。
如果想把输入输出设置到hdfs也容易,只要把WordSplit.java里的路径加个前缀hdfs://localhost:9000就完事了:
String inputFile = "hdfs://localhost:9000/input/people.txt"; // 输入文件
Path outDir = new Path("hdfs://localhost:9000/out"); // 输出目录
Path tempDir = new Path("hdfs://localhost:9000/tmp" + System.currentTimeMillis()); // 临时目录
当然你得先把小说传到hdfs上才能跑,可以在cmd里用hdfs命令,也可以直接在eclipse里操作,怎么弄看上面第4步。跑完再点Reflesh可以直接看结果文件。如果要重启hadoop,记得先把eclipse关了,在命令行里起了hadoop再打开eclipse接着玩。
eclipse配置hadoop2.7.2开发环境并本地跑起来的更多相关文章
- eclipse配置storm1.1.0开发环境并本地跑起来
storm的开发环境搭建比hadoop(参见前文http://www.cnblogs.com/wuxun1997/p/6849878.html)简单,无需安装插件,只需新建一个java项目并配置好li ...
- Windows 8.0上Eclipse 4.4.0 配置CentOS 6.5 上的Hadoop2.2.0开发环境
原文地址:http://www.linuxidc.com/Linux/2014-11/109200.htm 图文详解Windows 8.0上Eclipse 4.4.0 配置CentOS 6.5 上的H ...
- ubuntu上用eclipse搭建java、python开发环境
上一篇文章讲到如何在windwos上用eclipse搭建java.python开发环境,这一讲将关注如何在ubuntu上实现搭建,本人使用虚拟机安装的ubuntu系统,系统版本为:14.04 lts ...
- windows 下用eclipse搭建java、python开发环境
本人只针对小白!本文只针对小白!本文只针对小白! 最近闲来无事,加上之前虽没有做过eclipse上java.python的开发工作,但一直想尝试一下.于是边查找资料边试验,花了一天时间在自己的机器上用 ...
- 基于Eclipse的Go语言可视化开发环境
http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...
- Eclipse和PyDev搭建python开发环境
Eclipse和PyDev搭建python开发环境 1.1整体目标 本文档作为python学习者的新手教程,通过本教程能够了解python用途.语法.在实际工作中的应 ...
- 利用eclipse+jdk1.8搭建Java开发环境(超具体的)
利用eclipse+jdk1.8搭建Java开发环境 转载请声明出处:http://blog.csdn.net/u013067166/article/details/50267003 引言:eclip ...
- 在Fedora18上配置个人的Hadoop开发环境
在Fedora18上配置个人的Hadoop开发环境 1. 背景 文章中讲述了类似于"personalcondor"的一种"personal hadoop" ...
- 如何在Eclipse中搭建MyBatis基本开发环境?(使用Eclipse创建Maven项目)
实现要求: 在Eclipse中搭建MyBatis基本开发环境. 实现步骤: 1.使用Eclipse创建Maven项目.File >> New >> Maven Project ...
随机推荐
- Luogu-3250 [BJOI2017]魔法咒语(AC自动机,矩阵快速幂)
Luogu-3250 [BJOI2017]魔法咒语(AC自动机,矩阵快速幂) 题目链接 题解: 多串匹配问题,很容易想到是AC自动机 先构建忌讳词语的AC自动机,构建时顺便记录一下这个点以及它的所有后 ...
- Buildbot初探
什么是Buildbot Buildbot是一个持续集成和自动化测试框架,我在毕业刚进VMware不久的一个和以色列人合作的项目中接触到Buildbot,当时我真的恨死它了...经常随意的提交了一些代码 ...
- 高亮显示UILabel中的子串
I. 用户在搜索框中,输入关键字进行检索时,APP对搜索结果进行显示,有以下两种情况: 1. 匹配一次,如检索关键字为人名 这种情况,实现比较容易.写一个UILabel的category, 用rang ...
- scala学习手记40 - case表达式里的模式变量和常量
再来看一下之前的一段代码: def process(input: Any) { input match { case (a: Int, b: Int) => println("Proc ...
- ssh登录CentOS服务器(Aliyun)
ssh登录 (一).在本机使用命令生成ssh密钥:(-C代表注释,如果有多个密钥,加个注释就会很好区分) ssh-keygen -t rsa -C "mingwei" (二).将本 ...
- .Net Core使用jexus配置https
今天搞了一下怎么从http换成https,写一篇博客记录该过程.关于jexus的安装和使用请看我之前的一篇博客<Jexus部署Asp.Net Core项目>,唯一的不同是,将jexus升级 ...
- eclipse中修改工程的Android版本
项目根目录下project.properties的记录项目中所需要的环境信息,比如Android的版本等 project.properties示例如下: [html] view plaincopy # ...
- C#远程开机
什么是网络唤醒网络唤醒实现了对网络的集中管理,即在任何时刻,网管中心的IT管理人员可以经由网络远程唤醒一台处于休眠或关机状态的计算机.使用这一功能,IT管理人员可以在下班后,网络流量最小以及企业的正常 ...
- python 爬虫001-http请求过程
HTTP 请求流程 一次完整的HTTP请求过程从TCP三次握手建立连接成功后开始,客户端按照指定的格式开始向服务端发送HTTP请求,服务端接收请求后,解析HTTP请求,处理完业务逻辑,最后返回一个HT ...
- nt":false,"tarball":"http://registry.npm.taobao.org/babel-preset-stag
npm ERR! Unexpected end of input at 1:12777 npm ERR! nt":false,"tarball":"http:/ ...