使用MapReduce和Hadoop的销售数据排序系统的代码,按月份升序和销售量降序排序
时间: 2024-03-14 15:45:28 浏览: 66
以下是使用MapReduce和Hadoop的销售数据排序系统的代码,按月份升序和销售量降序排序:
Mapper类:SalesMapper.java
```java
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class SalesMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text outputKey = new Text();
private Text outputValue = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] record = value.toString().split(",");
String month = record[0];
String sales = record[1];
outputKey.set(month);
outputValue.set(sales);
context.write(outputKey, outputValue);
}
}
```
Reducer类:SalesReducer.java
```java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class SalesReducer extends Reducer<Text, Text, Text, Text> {
private Text outputValue = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
List<Integer> salesList = new ArrayList<Integer>();
for (Text value : values) {
salesList.add(Integer.parseInt(value.toString()));
}
// 对销售量进行降序排序
Collections.sort(salesList, Collections.reverseOrder());
StringBuilder sb = new StringBuilder();
for (int sales : salesList) {
sb.append(sales).append(",");
}
// 去掉最后一个逗号
sb.setLength(sb.length() - 1);
outputValue.set(sb.toString());
context.write(key, outputValue);
}
}
```
Driver类:SalesSortDriver.java
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class SalesSortDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Sales Sort");
job.setJarByClass(SalesSortDriver.class);
job.setMapperClass(SalesMapper.class);
job.setReducerClass(SalesReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
TextInputFormat.setInputPaths(job, new Path(args[0]));
TextOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}
```
在运行Hadoop作业时,您需要将输入数据以逗号分隔的格式存储在文本文件中,并将其传递给作业的第一个参数。作业的第二个参数将是输出目录。您可以在输出目录中找到按月份升序和销售量降序排序的结果。
阅读全文