一、创建一个封装类
public class Things implements WritableComparable<Things> {
//订单id
private String order;
//用户id
private String user;
//商品名字
private String things;
//单价
private float money;
//数量
private int count;
//商品总和
private float sum;
public Things() {
}
public Things(String order, String user, String things, float money, int count) {
this.order = order;
this.user = user;
this.things = things;
this.money = money;
this.count = count;
this.sum = this.money * this.count;
}
public Things(String s, float count) {
}
public float getSum() {
return money * count;
}
public void setSum() {
this.sum = money * count;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getThings() {
return things;
}
public void setThings(String things) {
this.things = things;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return "Things{" +
"order='" + order + '\'' +
", user='" + user + '\'' +
", things='" + things + '\'' +
", money=" + money +
", count=" + count +
", sum=" + sum +
'}';
}
//CompareTo方法排序
@Override
public int compareTo(Things o) {
return (int) o.getSum() - (int) this.getSum();
}
//序列化
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeUTF(this.order);
dataOutput.writeUTF(this.user);
dataOutput.writeUTF(this.things);
dataOutput.writeFloat(this.money);
dataOutput.writeInt(this.count);
dataOutput.writeFloat(this.sum);
}
//反序列化
@Override
public void readFields(DataInput dataInput) throws IOException {
this.order = dataInput.readUTF();
this.user = dataInput.readUTF();
this.things = dataInput.readUTF();
this.money = dataInput.readFloat();
this.count = dataInput.readInt();
this.sum = dataInput.readFloat();
}
}
二、创建一个Mapper的编写类
public class SumMapper extends Mapper<LongWritable,Text,Text,Things> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] split = line.split(",");
context.write(new Text(split[0]),new Things(split[0],split[1], split[2], Float.parseFloat(split[3]), Integer.parseInt(split[4])));
}
}
三、创建Reducer的编写类
public class SumReducer extends Reducer<Text, Things, Text, Things> {
@Override
protected void reduce(Text key, Iterable<Things> values, Context context) throws IOException, InterruptedException {
List<Things> list = new ArrayList<Things>();
for (Things value : values) {
Things things = new Things();
things.setOrder(value.getOrder());
things.setUser(value.getUser());
things.setThings(value.getThings());
things.setMoney (value.getMoney());
things.setCount (value.getCount());
things.setSum();
list.add(things);
}
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
if (i == 3) {
return;
}
context.write(new Text(list.get(i).getOrder()), list.get(i));
}
}
}
四、创建一个测试类
public class DingdanTest {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(DingdanTest.class);
job.setMapperClass(SumMapper.class);
job.setReducerClass(SumReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Things.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Things.class);
File file = new File("output");
if (file.exists()) {
FileUtils.deleteDirectory(file);
}
FileInputFormat.setInputPaths(job, new Path("input"));
FileOutputFormat.setOutputPath(job, new Path("output"));
job.setNumReduceTasks(1);
Boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : 1);
}
}
Pom配置文件:
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>