溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何用MapReduce求各個(gè)部門的總工資

發(fā)布時(shí)間:2021-12-30 14:12:43 來源:億速云 閱讀:203 作者:iii 欄目:云計(jì)算

本篇內(nèi)容介紹了“如何用MapReduce求各個(gè)部門的總工資”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

數(shù)據(jù)

     EMPNO       ENAME        JOB                   MGR   HIREDATE                      SAL         COMM      DEPTNO

      7369 SMITH      CLERK           7902 17-12月-80            800                    20
      7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300         30
      7521 WARD       SALESMAN        7698 22-2月 -81           1250        500         30
      7566 JONES      MANAGER         7839 02-4月 -81           2975                    20
      7654 MARTIN     SALESMAN        7698 28-9月 -81           1250       1400         30
      7698 BLAKE      MANAGER         7839 01-5月 -81           2850                    30
      7782 CLARK      MANAGER         7839 09-6月 -81           2450                    10
      7839 KING       PRESIDENT            17-11月-81           5000                    10
      7844 TURNER     SALESMAN        7698 08-9月 -81           1500          0         30
      7900 JAMES      CLERK           7698 03-12月-81            950                    30
      7902 FORD       ANALYST         7566 03-12月-81           3000                    20
      7934 MILLER     CLERK           7782 23-1月 -82           1300                    10

代碼

package cn.kissoft.hadoop.week07;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import cn.kissoft.hadoop.util.HdfsUtil;

/**
 * Homework-01:求各個(gè)部門的總工資
 * 
 * @author wukong(jinsong.sun@139.com)
 */
public class TotalSalaryByDeptMR extends Configured implements Tool {

	public static class M extends Mapper<LongWritable, Text, Text, IntWritable> {

		@Override
		public void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String line = value.toString();
			String deptno = line.substring(79).trim();
			String sal = line.substring(57, 68).trim();
			int salary = Integer.valueOf(sal);

			context.write(new Text(deptno), new IntWritable(salary));
		}
	}

	public static class R extends Reducer<Text, IntWritable, Text, IntWritable> {

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

	@Override
	public int run(String[] args) throws Exception {
		Configuration conf = getConf();
		Job job = new Job(conf, "Job-TotalSalaryByDeptMR");
//		job.setJarByClass(this.getClass());
		
		job.setMapperClass(M.class);
		job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        
		job.setReducerClass(R.class);
		
		job.setOutputFormatClass(TextOutputFormat.class);
//		job.setOutputKeyClass(NullWritable.class); // 指定輸出的KEY的格式
		job.setOutputKeyClass(Text.class); // 指定輸出的KEY的格式
		job.setOutputValueClass(IntWritable.class); // 指定輸出的VALUE的格式

		
		FileInputFormat.addInputPath(job, new Path(args[0])); // 輸入路徑
		FileOutputFormat.setOutputPath(job, new Path(args[1])); // 輸出路徑
		
		return job.waitForCompletion(true) ? 0 : 1; 
//		job.waitForCompletion(true);
//		return job.isSuccessful() ? 0 : 1;
	}

	/**
	 * 
	 * @param args hdfs://bd11:9000/user/wukong/w07/emp.txt hdfs://bd11:9000/user/wukong/w07/out01/
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		checkArgs(args);

		HdfsUtil.rm(args[1], true);
		
		Date start = new Date();
		int res = ToolRunner.run(new Configuration(), new TotalSalaryByDeptMR(), args);
		printExcuteTime(start, new Date());

		System.exit(res);
	}

	/**
	 * 判斷參數(shù)個(gè)數(shù)是否正確,如果無參數(shù)運(yùn)行則顯示以作程序說明。
	 * 
	 * @param args
	 */
	private static void checkArgs(String[] args) {
		if (args.length != 2) {
			System.err.println("");
			System.err.println("Usage: Test_1 < input path > < output path > ");
			System.err
					.println("Example: hadoop jar ~/Test_1.jar hdfs://localhost:9000/home/james/Test_1 hdfs://localhost:9000/home/james/output");
			System.err.println("Counter:");
			System.err.println("\t" + "LINESKIP" + "\t"
					+ "Lines which are too short");
			System.exit(-1);
		}
	}

	/**
	 * 打印程序運(yùn)行時(shí)間
	 * 
	 * @param start
	 * @param end
	 */
	private static void printExcuteTime(Date start, Date end) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		float time = (float) ((end.getTime() - start.getTime()) / 60000.0);
		System.out.println("任務(wù)開始:" + formatter.format(start));
		System.out.println("任務(wù)結(jié)束:" + formatter.format(end));
		System.out.println("任務(wù)耗時(shí):" + String.valueOf(time) + " 分鐘");
	}
}

運(yùn)行結(jié)果

10	8750
20	6775
30	9400

控制臺(tái)

14/08/31 23:01:01 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
14/08/31 23:01:01 WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
14/08/31 23:01:01 INFO input.FileInputFormat: Total input paths to process : 1
14/08/31 23:01:02 WARN snappy.LoadSnappy: Snappy native library not loaded
14/08/31 23:01:02 INFO mapred.JobClient: Running job: job_local248108448_0001
14/08/31 23:01:02 INFO mapred.LocalJobRunner: Waiting for map tasks
14/08/31 23:01:02 INFO mapred.LocalJobRunner: Starting task: attempt_local248108448_0001_m_000000_0
14/08/31 23:01:02 INFO mapred.Task:  Using ResourceCalculatorPlugin : null
14/08/31 23:01:02 INFO mapred.MapTask: Processing split: hdfs://bd11:9000/user/wukong/w07/emp.txt:0+1119
14/08/31 23:01:02 INFO mapred.MapTask: io.sort.mb = 100
14/08/31 23:01:02 INFO mapred.MapTask: data buffer = 79691776/99614720
14/08/31 23:01:02 INFO mapred.MapTask: record buffer = 262144/327680
14/08/31 23:01:02 INFO mapred.MapTask: Starting flush of map output
14/08/31 23:01:02 INFO mapred.MapTask: Finished spill 0
14/08/31 23:01:02 INFO mapred.Task: Task:attempt_local248108448_0001_m_000000_0 is done. And is in the process of commiting
14/08/31 23:01:02 INFO mapred.LocalJobRunner: 
14/08/31 23:01:02 INFO mapred.Task: Task 'attempt_local248108448_0001_m_000000_0' done.
14/08/31 23:01:02 INFO mapred.LocalJobRunner: Finishing task: attempt_local248108448_0001_m_000000_0
14/08/31 23:01:02 INFO mapred.LocalJobRunner: Map task executor complete.
14/08/31 23:01:02 INFO mapred.Task:  Using ResourceCalculatorPlugin : null
14/08/31 23:01:02 INFO mapred.LocalJobRunner: 
14/08/31 23:01:02 INFO mapred.Merger: Merging 1 sorted segments
14/08/31 23:01:02 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 110 bytes
14/08/31 23:01:02 INFO mapred.LocalJobRunner: 
14/08/31 23:01:02 INFO mapred.Task: Task:attempt_local248108448_0001_r_000000_0 is done. And is in the process of commiting
14/08/31 23:01:02 INFO mapred.LocalJobRunner: 
14/08/31 23:01:02 INFO mapred.Task: Task attempt_local248108448_0001_r_000000_0 is allowed to commit now
14/08/31 23:01:02 INFO output.FileOutputCommitter: Saved output of task 'attempt_local248108448_0001_r_000000_0' to hdfs://bd11:9000/user/wukong/w07/out01
14/08/31 23:01:02 INFO mapred.LocalJobRunner: reduce > reduce
14/08/31 23:01:02 INFO mapred.Task: Task 'attempt_local248108448_0001_r_000000_0' done.
14/08/31 23:01:03 INFO mapred.JobClient:  map 100% reduce 100%
14/08/31 23:01:03 INFO mapred.JobClient: Job complete: job_local248108448_0001
14/08/31 23:01:03 INFO mapred.JobClient: Counters: 19
14/08/31 23:01:03 INFO mapred.JobClient:   File Output Format Counters 
14/08/31 23:01:03 INFO mapred.JobClient:     Bytes Written=24
14/08/31 23:01:03 INFO mapred.JobClient:   File Input Format Counters 
14/08/31 23:01:03 INFO mapred.JobClient:     Bytes Read=1119
14/08/31 23:01:03 INFO mapred.JobClient:   FileSystemCounters
14/08/31 23:01:03 INFO mapred.JobClient:     FILE_BYTES_READ=426
14/08/31 23:01:03 INFO mapred.JobClient:     HDFS_BYTES_READ=2238
14/08/31 23:01:03 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=138578
14/08/31 23:01:03 INFO mapred.JobClient:     HDFS_BYTES_WRITTEN=24
14/08/31 23:01:03 INFO mapred.JobClient:   Map-Reduce Framework
14/08/31 23:01:03 INFO mapred.JobClient:     Reduce input groups=3
14/08/31 23:01:03 INFO mapred.JobClient:     Map output materialized bytes=114
14/08/31 23:01:03 INFO mapred.JobClient:     Combine output records=0
14/08/31 23:01:03 INFO mapred.JobClient:     Map input records=12
14/08/31 23:01:03 INFO mapred.JobClient:     Reduce shuffle bytes=0
14/08/31 23:01:03 INFO mapred.JobClient:     Reduce output records=3
14/08/31 23:01:03 INFO mapred.JobClient:     Spilled Records=24
14/08/31 23:01:03 INFO mapred.JobClient:     Map output bytes=84
14/08/31 23:01:03 INFO mapred.JobClient:     Total committed heap usage (bytes)=326107136
14/08/31 23:01:03 INFO mapred.JobClient:     SPLIT_RAW_BYTES=105
14/08/31 23:01:03 INFO mapred.JobClient:     Map output records=12
14/08/31 23:01:03 INFO mapred.JobClient:     Combine input records=0
14/08/31 23:01:03 INFO mapred.JobClient:     Reduce input records=12
任務(wù)開始:2014-08-31 23:01:01
任務(wù)結(jié)束:2014-08-31 23:01:03
任務(wù)耗時(shí):0.024416666 分鐘

“如何用MapReduce求各個(gè)部門的總工資”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI