先安装并启动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开发环境并本地跑起来的更多相关文章

  1. eclipse配置storm1.1.0开发环境并本地跑起来

    storm的开发环境搭建比hadoop(参见前文http://www.cnblogs.com/wuxun1997/p/6849878.html)简单,无需安装插件,只需新建一个java项目并配置好li ...

  2. 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 ...

  3. ubuntu上用eclipse搭建java、python开发环境

    上一篇文章讲到如何在windwos上用eclipse搭建java.python开发环境,这一讲将关注如何在ubuntu上实现搭建,本人使用虚拟机安装的ubuntu系统,系统版本为:14.04 lts ...

  4. windows 下用eclipse搭建java、python开发环境

    本人只针对小白!本文只针对小白!本文只针对小白! 最近闲来无事,加上之前虽没有做过eclipse上java.python的开发工作,但一直想尝试一下.于是边查找资料边试验,花了一天时间在自己的机器上用 ...

  5. 基于Eclipse的Go语言可视化开发环境

    http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...

  6. Eclipse和PyDev搭建python开发环境

                   Eclipse和PyDev搭建python开发环境 1.1整体目标 本文档作为python学习者的新手教程,通过本教程能够了解python用途.语法.在实际工作中的应 ...

  7. 利用eclipse+jdk1.8搭建Java开发环境(超具体的)

    利用eclipse+jdk1.8搭建Java开发环境 转载请声明出处:http://blog.csdn.net/u013067166/article/details/50267003 引言:eclip ...

  8. 在Fedora18上配置个人的Hadoop开发环境

    在Fedora18上配置个人的Hadoop开发环境 1.    背景 文章中讲述了类似于"personalcondor"的一种"personal hadoop" ...

  9. 如何在Eclipse中搭建MyBatis基本开发环境?(使用Eclipse创建Maven项目)

    实现要求: 在Eclipse中搭建MyBatis基本开发环境. 实现步骤: 1.使用Eclipse创建Maven项目.File >> New >> Maven Project ...

随机推荐

  1. LeetCode—-Sort List

    LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...

  2. 关于file_get_contents返回False的问题

    在本地测试中,使用file_get_contents获取远程服务器的资源是可以的: public function send_post($url, $post_data = null) { $post ...

  3. Android -- junit测试框架,logcat获取log信息

    1. 相关概念 白盒测试: 知道程序源代码. 根据测试的粒度分为不同的类型   方法测试 function test         单元测试 unit test                 集成 ...

  4. 工作队列work queues 公平分发(fair dispatch) And 消息应答与消息持久化

    生产者 package cn.wh.work; import cn.wh.util.RabbitMqConnectionUtil; import com.rabbitmq.client.Channel ...

  5. [ SSH 两种验证方式原理 ]

    SSH登录方式主要分为两种: 1. 用户名密码验证方式 说明: (1) 当客户端发起ssh请求,服务器会把自己的公钥发送给用户: (2) 用户会根据服务器发来的公钥对密码进行加密: (3) 加密后的信 ...

  6. 使用 <!-- 指定使用hibernate核心配置文件 --> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>

    在bean.xml文件中,这样使用出现问题 <!-- 指定使用hibernate核心配置文件 --> <property name="configLocations&quo ...

  7. PL/SQL通过修改配置文件的方式实现数据库的连接

    http://jingyan.baidu.com/article/c74d600080632a0f6a595d80.html

  8. 用临时用户数据目录启动Chrome,关闭安全检查等(解决Cross origin requests are only supported for HTTP?)

    Cross origin requests are only supported for HTTP? 参考:https://www.zhihu.com/question/20948649 批处理: s ...

  9. cassandra框架模型之一——Colum排序,分区策略 Token,Partitioner bloom-filter,HASH

    转自:http://asyty.iteye.com/blog/1202072 一.Cassandra框架二.Cassandra数据模型 Colum / Colum Family, SuperColum ...

  10. 编写自己的validate校验框架原理(转)

    原文链接:http://blog.csdn.net/a973893384/article/details/51517388 具体思路: 我们使用自定义注解实现.然后需要解决的是两个问题: 1是如何扫描 ...