0% found this document useful (0 votes)
8 views10 pages

Electricity Consumption Analysis Code

bài tập lab map_reduce JAVA

Uploaded by

Giang Trường
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Electricity Consumption Analysis Code

bài tập lab map_reduce JAVA

Uploaded by

Giang Trường
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BÀI TẬP ÁP DỤNG

[Link]

import [Link];

import [Link];

import [Link];

import [Link];

import [Link].*;
import [Link].*;

import [Link];

import [Link];

public class ElectricityAnalysis {

public static class TokenizerMapper extends Mapper<Object, Text, Text, Text> {

public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {

if ([Link]().startsWith("Year")) return; // bỏ dòng tiêu đề

String[] fields = [Link]().split(",");

String year = fields[0];

String month = fields[1];

String consumption = fields[2];

[Link](new Text(year), new Text(month + ":" + consumption));

public static class SumReducer extends Reducer<Text, Text, Text, Text> {

public void reduce(Text key, Iterable<Text> values, Context context) throws IOException,
InterruptedException {

int total = 0, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;

String maxMonth = "", minMonth = "";

for (Text val : values) {

String[] parts = [Link]().split(":");

String month = parts[0];

int consumption = [Link](parts[1]);

total += consumption;
if (consumption > max) {

max = consumption;

maxMonth = month;

if (consumption < min) {

min = consumption;

minMonth = month;

String output = "Total=" + total +

", MaxMonth=" + maxMonth + "(" + max + ")" +

", MinMonth=" + minMonth + "(" + min + ")";

[Link](key, new Text(output));

public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();

Job job = [Link](conf, "Electricity Analysis");

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link](job, new Path(args[0]));

[Link](job, new Path(args[1]));

[Link]([Link](true) ? 0 : 1);

}
}

[Link]

import [Link];

import [Link];

import [Link];

import [Link].*;

import [Link].*;

import [Link];

import [Link];

public class FindMaxElectricity {

public static class MapperMax extends Mapper<Object, Text, Text, IntWritable> {

public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {

String[] parts = [Link]().split("\\t");

if ([Link] < 2) return;

String year = parts[0];

String[] tokens = parts[1].split(",");

for (String token : tokens) {

if ([Link]().startsWith("Total=")) {

int total = [Link]([Link]().split("=")[1]);

[Link](new Text("year"), new IntWritable(total << 16 | [Link](year))); //


encoded year + total

}
public static class ReducerMax extends Reducer<Text, IntWritable, Text, Text> {

public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,
InterruptedException {

int maxTotal = Integer.MIN_VALUE;

int minTotal = Integer.MAX_VALUE;

int maxYear = 0, minYear = 0;

for (IntWritable val : values) {

int combined = [Link]();

int total = combined >>> 16;

int year = combined & 0xFFFF;

if (total > maxTotal) {

maxTotal = total;

maxYear = year;

if (total < minTotal) {

minTotal = total;

minYear = year;

[Link](new Text("Highest Total Year"), new Text(maxYear + " (" + maxTotal + ")"));

[Link](new Text("Lowest Total Year"), new Text(minYear + " (" + minTotal + ")"));

public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();

Job job = [Link](conf, "Find Max Electricity");


[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link](job, new Path(args[0])); // input là output_dir của job trước

[Link](job, new Path(args[1])); // ví dụ: output_max

[Link]([Link](true) ? 0 : 1);

You might also like