hadoop开发(1)eclipse写mapreduce的jar包

创建一个maven文件MapReduce

修改pom文件

	<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.6.0</version>
      <scope>test</scope>
    </dependency>

完整pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.panzi.cn</groupId>
  <artifactId>MapReduce</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MapReduce</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.6.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

在这里插入图片描述

创建WordCount的java文件

在这里插入图片描述
代码

package com.panzi.cn.MapReduce;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {
	// map类型<key,value>

	// keyin
	// valuein_string
	// keyout_text_string
	// valueout_int_IntWritable
	public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {

		@Override
		protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			// 获取valuein
			String valuein = value.toString();
			// valuein分片
			String keyin[] = valuein.split(" ");
			// 封装
			for (String ks : keyin) {
				context.write(new Text(ks), new IntWritable(1));
			}
		}

	}

	// reduce
	public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			context.write(key, new IntWritable(sum));
		}

	}

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub
		if (args.length < 2) {
			System.out.println("the argument are adfadf");
			System.exit(0);
		}
		Configuration conf = new Configuration();
		String[] arg = new GenericOptionsParser(conf, args).getRemainingArgs();
		Job job = new Job(conf, "hadoop"); // 设置环境参数
		job.setJarByClass(WordCount.class); // 设置整个程序的类名
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path(arg[0]));
		FileOutputFormat.setOutputPath(job, new Path(arg[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}

封装

右键项目——export——选择java——JAR File——next——
在这里插入图片描述
——next——
在这里插入图片描述
点击Finish
上传到虚拟机上的hadoop软件目录下的share/hadoop/mapreduce中

运行

[root@master1 mapreduce]# hadoop jar wordcount.jar /data/ /out2

运行成功
在这里插入图片描述

查看结果

结果保存在:

[root@master1 mapreduce]# hdfs dfs -ls /out2
[root@master1 mapreduce]# hdfs dfs -cat /out2/part-r-00000

在这里插入图片描述
结束!睡觉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值