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

Weather Analysis

The document provides a Java MapReduce program for analyzing weather data, categorizing temperatures into 'Hot', 'Cold', or 'Moderate' based on input from a CSV file. It includes instructions for saving the Java code, compiling it, and running it using Hadoop commands. The output displays the weather conditions for various locations and dates based on the temperature data processed.

Uploaded by

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

Weather Analysis

The document provides a Java MapReduce program for analyzing weather data, categorizing temperatures into 'Hot', 'Cold', or 'Moderate' based on input from a CSV file. It includes instructions for saving the Java code, compiling it, and running it using Hadoop commands. The output displays the weather conditions for various locations and dates based on the temperature data processed.

Uploaded by

aarthi.acs24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

3.

Develop a Map Reduce program that mines weather data and displays appropriate
messages indicating the weather conditions of the day.

Java :
Open a notepad or editor and save the below program as WeatherAnalysis.java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WeatherAnalysis {

public static class WeatherMapper extends Mapper<LongWritable, Text, Text,


Text> {
public void map(LongWritable key, Text value, Context context) throws
IOException, InterruptedException {
String line = value.toString();

if (!line.startsWith("Date")) { // skip header


String[] fields = line.split(",");
String date = fields[0];
String location = fields[1];
double temperature = Double.parseDouble(fields[2]);

String condition;
if (temperature >= 35) {
condition = "Hot";
} else if (temperature <= 15) {
condition = "Cold";
} else {
condition = "Moderate";
}

context.write(new Text(date + " - " + location), new Text("Weather is " +


condition));
}
}
}

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


public void reduce(Text key, Iterable<Text> values, Context context) throws
IOException, InterruptedException {
for (Text val : values) {
context.write(key, val);
}
}
}

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


Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Weather Condition Analyzer");

job.setJarByClass(WeatherAnalysis.class);
job.setMapperClass(WeatherMapper.class);
job.setReducerClass(WeatherReducer.class);

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);

FileInputFormat.addInputPath(job, new Path(args[0])); // input path


FileOutputFormat.setOutputPath(job, new Path(args[1])); // output path

System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

Save input file as Weather.csv in( C:/users/documents/input/):


2025-04-01,Delhi,38
2025-04-01,Shimla,10
2025-04-01,Mumbai,30
2025-04-01,Bengaluru,28
2025-04-01,Chennai,36
2025-04-02,Delhi,37
2025-04-02,Shimla,12
2025-04-02,Mumbai,32
2025-04-02,Bengaluru,29
2025-04-02,Chennai,35
2025-04-03,Delhi,34
2025-04-03,Shimla,8
2025-04-03,Mumbai,31
2025-04-03,Bengaluru,27
2025-04-03,Chennai,37
2025-04-04,Delhi,40
2025-04-04,Shimla,14
2025-04-04,Mumbai,33
2025-04-04,Bengaluru,26
2025-04-04,Chennai,38

In Command Prompt:(Always run CMD as administrator only)

c:\Users\KUMARESH\Documents>javac -classpath
"C:\hadoop\share\hadoop\common\*;C:\hadoop\share\hadoop\common\lib\*;C:\hadoop\share\
hadoop\mapreduce\*;C:\hadoop\share\hadoop\mapreduce\lib\*;C:\hadoop\share\hadoop\hdfs\
*;C:\hadoop\share\hadoop\hdfs\lib\*" -d classes WeatherAnalysis.java

c:\Users\KUMARESH\Documents>jar -cvf WeatherAnalysis.jar -C classes/ .


c:\Users\KUMARESH\Documents>start-all.cmd

c:\Users\KUMARESH\Documents>hdfs dfs -mkdir /inputWeather

c:\Users\KUMARESH\Documents>hdfs dfs -put input/Weather.csv /inputWeather

c:\Users\KUMARESH\Documents>hadoop jar WeatherAnalysis.jar WeatherAnalysis /inputWeather


/output4

Output:
c:\Users\KUMARESH\Documents>hdfs dfs -cat /output4/part-r-00000

You might also like