如何在 java mapreduce hadoop 中获得两个键的最大计数
How to get max count over two keys in java mapreduce hadoop
我有一个包含 6 列的 txt 文件,我对第三和第四列、城市和产品感兴趣,这是一个示例:
2015-01-01;09:00:00;New York;shoes;214.05;Amex >
我需要按城市获取销量最大的产品。我已经有了按城市聚合和计算所有产品的代码,这里是 class 映射器和 class 缩减器的代码:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class ContaMaxCidadeProdutoMapper extends Mapper<Object, Text, Text, IntWritable> {
private final static Text cidadeproduto = new Text();
private final static IntWritable numeroum = new IntWritable(1);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] linha=value.toString().split(";");
cidadeproduto.set(linha[2] +" "+linha[3]);
context.write(cidadeproduto, numeroum);
}
}
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class ContaMaxCidadeProdutoReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int contValue = 0;
for (IntWritable value : values) {
contValue += value.get();
}
context.write(key, new IntWritable(contValue));
}
}
按城市获取每个产品的数量工作正常,但现在我需要按城市获取最大数量的产品。我知道如何获取整个数据集的最大计数乘积,但我不知道如何按城市获取。我将不胜感激任何提示!
谢谢
我将从解释map/reduce的基础开始,其中有两个基本部分:
- 地图:将您的原始输入转换为您可以使用的值(在您的情况下,city/product 对和一个数字)
- Reduce:对于每个 city/product 对,将所有数字相加。
在您当前的应用程序中,无论输入内容如何,您都选择了数字 1。对一堆 1 求和与计算它们的效果相同。
相反,您需要将它映射到另一个值,方法是从您的输入字符串中提取它并将其解析为 Double
,然后发送它代替 numeroum
.
您想获得 城市 数量最多的产品。正如我所见,您希望每个城市都拥有该特定城市的最大销售额的产品,不是吗?
我宁愿用 2 个 M-R 对来做。第一对与你的相似:
public void map(Object key, Text value, Context context) {
String[] linha = value.toString().split(";");
cidadeproduto.set(linha[2] + "&" + linha[3]);
context.write(cidadeproduto, new IntWritable(1));
}
public void reduce(Text key, Iterable<IntWritable> values, Context context){
int contValue = 0;
for (IntWritable value : values) {
contValue += value.get();
}
context.write(key, new IntWritable(contValue));
}
和第二对。
映射器将重新组合您的数据,使城市成为关键,产品和数量成为价值:
public void map(Object key, Text value, Context context) {
String[] row = value.toString().split(";");
String city = row[0].split("&")[0];
String product = row[0].split("&")[1];
String count = row[1];
context.write(new Text(city), new Text(product + "&" + count));
}
然后reduce 将保持每个城市的最大值:
public void reduce(Text key, Iterable<Text> values, Context context){
int maxVal = Integer.MIN_VALUE;
String maxProd = "None";
for (IntWritable value : values) {
String ss = value.toString().split("&");
int cnt = Integer.parseInt(ss[1]);
if(cnt > maxVal){
maxVal = cnt;
maxProd = ss[0];
}
}
context.write(key, new Text(maxProd));
}
我有一个包含 6 列的 txt 文件,我对第三和第四列、城市和产品感兴趣,这是一个示例:
2015-01-01;09:00:00;New York;shoes;214.05;Amex >
我需要按城市获取销量最大的产品。我已经有了按城市聚合和计算所有产品的代码,这里是 class 映射器和 class 缩减器的代码:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class ContaMaxCidadeProdutoMapper extends Mapper<Object, Text, Text, IntWritable> {
private final static Text cidadeproduto = new Text();
private final static IntWritable numeroum = new IntWritable(1);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] linha=value.toString().split(";");
cidadeproduto.set(linha[2] +" "+linha[3]);
context.write(cidadeproduto, numeroum);
}
}
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class ContaMaxCidadeProdutoReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int contValue = 0;
for (IntWritable value : values) {
contValue += value.get();
}
context.write(key, new IntWritable(contValue));
}
}
按城市获取每个产品的数量工作正常,但现在我需要按城市获取最大数量的产品。我知道如何获取整个数据集的最大计数乘积,但我不知道如何按城市获取。我将不胜感激任何提示! 谢谢
我将从解释map/reduce的基础开始,其中有两个基本部分:
- 地图:将您的原始输入转换为您可以使用的值(在您的情况下,city/product 对和一个数字)
- Reduce:对于每个 city/product 对,将所有数字相加。
在您当前的应用程序中,无论输入内容如何,您都选择了数字 1。对一堆 1 求和与计算它们的效果相同。
相反,您需要将它映射到另一个值,方法是从您的输入字符串中提取它并将其解析为 Double
,然后发送它代替 numeroum
.
您想获得 城市 数量最多的产品。正如我所见,您希望每个城市都拥有该特定城市的最大销售额的产品,不是吗?
我宁愿用 2 个 M-R 对来做。第一对与你的相似:
public void map(Object key, Text value, Context context) {
String[] linha = value.toString().split(";");
cidadeproduto.set(linha[2] + "&" + linha[3]);
context.write(cidadeproduto, new IntWritable(1));
}
public void reduce(Text key, Iterable<IntWritable> values, Context context){
int contValue = 0;
for (IntWritable value : values) {
contValue += value.get();
}
context.write(key, new IntWritable(contValue));
}
和第二对。
映射器将重新组合您的数据,使城市成为关键,产品和数量成为价值:
public void map(Object key, Text value, Context context) {
String[] row = value.toString().split(";");
String city = row[0].split("&")[0];
String product = row[0].split("&")[1];
String count = row[1];
context.write(new Text(city), new Text(product + "&" + count));
}
然后reduce 将保持每个城市的最大值:
public void reduce(Text key, Iterable<Text> values, Context context){
int maxVal = Integer.MIN_VALUE;
String maxProd = "None";
for (IntWritable value : values) {
String ss = value.toString().split("&");
int cnt = Integer.parseInt(ss[1]);
if(cnt > maxVal){
maxVal = cnt;
maxProd = ss[0];
}
}
context.write(key, new Text(maxProd));
}