彷徨 | MapReduce各种执行(Linux执行,eclipse执行)与读取和存储(从HDFS读取以及从本地读取)

版权声明:版权所有,转载请注明出处.谢谢 https://blog.csdn.net/weixin_35353187/article/details/81952135

1 . 读取HDFS中的文件 , 利用Linux平台MapReduce框架执行 , 结果写入 HDFS中 .

map

package hadoop_day05.zhang.firstMR;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/**
 * keyin : 表示输入的key的类型,输入的key是maptask所读取到的一行文本的起始偏移量  long
 * valuein : 表示输入的value的类型,输入的value是maptask所读取的一行文本的内容 String
 * 
 * keyout : 表示将输出的key的类型,我们在本逻辑中输出单词做key,String
 * valueout : 表示将要输出的value的类型,我们在本逻辑中输出1作为value,Integer
 * 
 * 但是,在MapReduce中,maptask输出的key,value需要经过网络传给reducetask,
 * 所以,这些key对象和value对象,都要可以被序列化和反序列化,
 * 虽然Long,String等jdk中的数据类型都实现了Serializable接口,可以被序列化,
 * 但是Serializable序列化机制产生的序列化数据相当臃肿,会大大降低网络传输的效率,
 * 所以hadoop专门自己设计了一套序列化机制,接口为Writable接口
 * 
 * long ---> LongWritable
 * String ---> Text
 * Integer ---> IntWritable
 * Double ---> DoubleWritable
 * @author Administrator
 *
 */

public class MapTask extends Mapper<LongWritable, Text, Text, IntWritable>{

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
			throws IOException, InterruptedException {
		//value每行的数
		String[] split = value.toString().split(" ");
		for (String word : split) {
			context.write(new Text(word), new IntWritable(1));
		}
		
	}
}

Reduce

package hadoop_day05.zhang.firstMR;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

/**
 * 本实例逻辑中
 * 将map传来的键值对<hello , 1> , <hello , 1> , <java , 1> 进行统计
 * 
 * 怎么调reduce方法,---> reduce task 程序
 * 它怎么调 ---> 它对每一组<key,value>调一次
 * @author Administrator
 *
 */

public class ReduceTask extends Reducer<Text, IntWritable, Text, IntWritable>{
	
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values , Context context) throws IOException, InterruptedException {
		
		int count = 0 ;
		for (IntWritable value : values) {
			count = count + value.get();
			//count++;
		}
		context.write(key, new IntWritable(count));
	}
	
}

客户端执行程序Driver

package hadoop_day05.zhang.firstMR;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * 提交咱们的任务?
 * job客户端和mr程序本身之间没有任何关系逻辑关系
 * 它和mr之间的关系,就像 : 运载火箭 和 卫星 之间的关系
 * 运载火箭只负责将火箭发射
 * job客户端只负责将程序交给yarn集群处理
 * @author root
 */
public class Driver {
	public static void main(String[] args) throws Exception {
		
		//加载classpat中的hadoop配置文件
		Configuration conf = new Configuration();
		
		//job API对象,在提交MR job去运行时,有俩种提交目的地选择:1.本地模拟器 2.yarn集群
		
		//conf.set("mapreduce.framework.name", "yarn");  //  mapred-site.xml
		//conf.set("fs.defaultFS", "hdfs://cts01:9000");  // core-site.xml
		//conf.set("yarn.resourcemanager.hostname", "cts01");
		//conf.set("mapreduce.app-submission.cross-platform", "true");  //跨平台提交;从windows上运行客户端提交mr job到linux服务器(yarn)
		
		Job job = Job.getInstance(conf);
		
		//设置job的map和reduce是哪个,并且设置是哪个做任务提交?
		job.setMapperClass(MapTask.class);
		job.setReducerClass(ReduceTask.class);
		job.setJarByClass(Driver.class);
		
		//设置输出类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		//设置输入和输出目的地?
		FileInputFormat.addInputPath(job, new Path("/wc.txt"));
		FileOutputFormat.setOutputPath(job, new Path("/wordcount/wc-output"));
		
		// 提交之后会监控运行状态?
		boolean completion = job.waitForCompletion(true);
		System.out.println(completion?"程序执行完毕,没毛病!!!":"程序有问题,程序出bug了,赶紧加班调试!!!");
		
	}

}

程序写好后 , 打包成jar包 , 传给Linux平台 , hadoop jar jar包名称 全类名执行即可

eg :hadoop jar wc.jar hadoop_day05.zhang.firstMR.Driver

右键项目,export打包成jar包

                                    

hadoop jar wc.jar hadoop_day05.zhang.firstMR.Driver

2 . 读取HDFS中的文件 , 直接在eclipse中执行 , 结果写入 HDFS中 .

Map与Reduce与1相同,不再重复

客户端执行程序Driver

package hadoop_day05.zhang.eclipse;

import java.io.IOException;

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.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import hadoop_day05.zhang.firstMR.MapTask;
import hadoop_day05.zhang.firstMR.ReduceTask;

/**
 * 从eclipse提交到集群
 * @author Administrator
 *
 */

public class Driver {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		//声明使用哪个用户提交
		System.setProperty("HADOOP_USER_NAME", "root");
		
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://hadoop01:9000"); //设置hdfs集群在哪里
		conf.set("mapreduce.framework.name", "yarn"); //提交到哪里   yarn   local
		conf.set("yarn.resourcemanager.hostname", "hadoop01");  //resourcemeanger 在哪里
		conf.set("mapreduce.app-submission.cross-platform", "true"); //windows 提交任务到linux上需要设置的参数
		
		Job job = Job.getInstance(conf);
		
		//设置map和reduce,以及提交的jar
		job.setMapperClass(MapTask.class);
		job.setReducerClass(ReduceTask.class);
		//job.setJarByClass(Driver.class);
		job.setJar("C:\\Users\\Administrator\\Desktop\\wc.jar");
		
		//设置输入输出类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		//设置输入和输出目录
		FileInputFormat.addInputPath(job, new Path("/wc.txt"));
		FileOutputFormat.setOutputPath(job, new Path("/wordcount/eclipse-out/"));
		
		//判断文件是否存在
		FileSystem fs = FileSystem.get(conf);
		if(fs.exists(new Path("/wordcount/eclipse-out/"))) {
			fs.delete(new Path("/wordcount/eclipse-out/"), true);
		}
		
		//提交任务
		boolean completion = job.waitForCompletion(true);
		System.out.println(completion?"优秀":"失败");
		
	}
}

主要参数

//声明使用哪个用户提交
System.setProperty("HADOOP_USER_NAME", "root");
		
Configuration conf = new Configuration();
//设置hdfs集群在哪里
conf.set("fs.defaultFS", "hdfs://hadoop01:9000"); 
//提交到哪里   yarn   local
conf.set("mapreduce.framework.name", "yarn"); 
//resourcemeanger 在哪里
conf.set("yarn.resourcemanager.hostname", "hadoop01");  
//windows 提交任务到linux上需要设置的参数
conf.set("mapreduce.app-submission.cross-platform", "true"); 

3 . 读取HDFS中的文件 , 直接在eclipse中执行 , 从本地读取文件,结果写入本地 .

Map与Reduce与1相同,不再重复

客户端执行程序Driver

package hadoop_day05.zhang.local;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


import hadoop_day05.zhang.firstMR.MapTask;
import hadoop_day05.zhang.firstMR.ReduceTask;

/**
 * 从本地读取结果写入本地
 * 小数据测试,测试完成后结果写入集群
 * @author Administrator
 *
 */

public class Driver {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		//直接在eclipse执行,声明是哪个用户执行,权限问题
		System.setProperty("HADOOP_USER_NAME", "root");
		
		Configuration conf = new Configuration();
		
		Job job = Job.getInstance(conf);
		
		//设置map和reduce,以及提交的jar
		job.setMapperClass(MapTask.class);
		job.setReducerClass(ReduceTask.class);
		job.setJarByClass(Driver.class);
		
		//设置输入和输出类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		//设置输入和输出目录
		FileInputFormat.addInputPath(job, new Path("e:\\data\\word.txt"));
		FileOutputFormat.setOutputPath(job, new Path("e:\\data\\out\\wc"));
		
		//判断文件是否存在
		File file = new File("e:\\data\\out\\wc");
		if(file.exists()) {
			FileUtils.deleteDirectory(file);
		}
		
		//提交任务
		boolean completion = job.waitForCompletion(true);
		System.out.println(completion?"成功":"失败");
		
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_35353187/article/details/81952135