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 ...
 
随机推荐
- redhat 6.8 配置yum源
			
一般安装好redhat后,不能注册的话,不能使用系统自带的yum源.但是我们可以自己配置yum源来解决这一问题.下面介绍下redhat配置163yum源. 1. 检查是否安装yum包 rpm -qa ...
 - CodeIgniter 资料
			
PHP 论坛: http://codeigniter.org.cn/forums/forum-opensource-1.html 下载 CodeIgniter 项目 的最新软件包(http://www ...
 - struts2——文件下载(简单的功能)
			
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
 - DPDK l2fwd
			
dpdk的l2fwd主要做二层转发,代码分析如下. #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
 - 认证和授权(Authentication和Authorization)
			
什么是OAuth 如今很多网站的功能都强调彼此间的交互,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是,OAuth应运而生,看看官网对其的定义: An open protocol t ...
 - .NET、C#和ASP.NET三者之间的区别与联系
			
.NET.C#和ASP.NET三者之间的区别与联系 1..net(dot net) .net是一个平台,抽象的平台概念. 实现形式是库:①定义了基本的类型(通用类型系统CTS,common type ...
 - IIS注册.NET
			
IIS中ASP.NET的版本号此时可选的有1.1.2.0和4.0三个,如果想让IIS把3个版本都集成上,那NET Framework 3种都要安装,默认安装到的是C 盘. IIS注册方式如下:1.1: ...
 - java多线程补:充原子性和可见性
			
参考:http://www.cnblogs.com/mengyan/archive/2012/08/22/2651575.html 原子性:所谓原子性就是不可分割的,比如:在我们编程中直接给变量赋值, ...
 - ASP.NET Core 简单引入教程
			
0.简介 开源.跨平台 1.环境安装 参考官方教程 Core 官方文档 2.向世界问个好 sheel/cmd 下: dotnet --help // 查看帮助 dotnet new * / ...
 - MSSQL数据库分区表
			
http://blog.csdn.net/lgb934/article/details/8662956 http://database.9sssd.com/mssql/art/951