linux如何在eclipse中运行wordcount,eclipse 运行WordCount(附源码)

1.下载hadoop插件(hadoop下载包里好像有这个插件)

放到eclipse/plugins文件夹下,重启eclipse

2.window  -> preferences  点击确定  找到 hadoop map/reduce 在右窗口填上hadoop安装地址

3.出现一个和控制台一样位置的map/reduce location ,右击空白处 选择new hadoop location

loaction name填上名字,Map/Reduce (V2) Master的端口填mapred-site.xml端口 。DFS Master填core-site.xml 确定。host都是填localhost。

4.File-->New-->Other-->Map/Reduce Project 创建文件 取名   新建java文件代码如下

5.源代码如下

package com.filex;

import java.io.IOException;

import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Mapper.Context;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.Reducer.Context;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount

{

public static void main(String[] args)

throws Exception

{

Configuration conf = new Configuration();

Job job = new Job(conf);

job.setJarByClass(WordCount.class);

job.setJobName("wordcount");

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

job.setMapperClass(WordCountMap.class);

job.setReducerClass(WordCountReduce.class);

job.setInputFormatClass(TextInputFormat.class);

job.setOutputFormatClass(TextOutputFormat.class);

/

/

//下面的两句代码,其中参数意义

//hdfs://localhost:9000/in 表示需要计数的文件夹 计算命令行下:hadoop fs -ls /in 出现的文件

//hdfs://localhost:9000/output 表示储存结果的文件夹(不要创建,同时之前不要存在这个文件夹)

//new Path(arg[0]) new Path(arg[1])也可以使用命令行传参的方式传入两个文件夹(不可以直接运行)

//

FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/in"));

FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/output"));

job.waitForCompletion(true);

}

public static class WordCountMap extends Mapper

{

private final IntWritable one = new IntWritable(1);

private Text word = new Text();

public void map(LongWritable key, Text value, Mapper.Context context) throws IOException, InterruptedException

{

String line = value.toString();

StringTokenizer token = new StringTokenizer(line);

while (token.hasMoreTokens()) {

this.word.set(token.nextToken());

context.write(this.word, this.one);

}

}

}

public static class WordCountReduce extends Reducer

{

public void reduce(Text key, Iterable values, Reducer.Context context)

throws IOException, InterruptedException

{

int sum = 0;

for (IntWritable val : values) {

sum += val.get();

}

context.write(key, new IntWritable(sum));

}

}

}

ps:注意一下注释部分,需要确认你需要计算的文件

6.直接运行 或者导出

7.如果导出,运行命令:

hadoop  jar      .jar路径                运行的类(含包路径)    类的参数

hadoop   jar   /home/user/xxx.jar  com.filex.WordCount                (输入输出文件已经设置好)

hadoop   jar   /home/user/xxx.jar  com.filex.WordCount      in put     ( 输入输出文件未设置好)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值