MapReduce 统计 HBase 成绩表中单科排名和总分排名的完整代码
时间: 2023-02-05 22:01:49 浏览: 28
下面是使用 MapReduce 统计 HBase 成绩表中单科排名和总分排名的完整代码示例:
```java
import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
public class HBaseRanking {
public static class RankingMapper
extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable score = new IntWritable();
private Text subject = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "scores");
Get get = new Get(Bytes.toBytes(value.toString()));
Result result = table.get(get);
NavigableMap<byte[], byte[]> map = result.getFamilyMap(Bytes.toBytes("scores"));
for (Map.Entry<byte[], byte[]> entry : map.entrySet()) {
subject.set(Bytes.toString(entry.getKey()));
score.set(Bytes.toInt(entry.getValue()));
context.write(subject, score);
}
}
}
public static class RankingReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int rank = 0;
for (IntWritable val : values) {
rank++;
}
result.set(rank);
context.write(key, result);
}
}
public static class TotalRankingMapper
extends Mapper<Object, Text, IntWritable, Text> {
private final static IntWritable score = new IntWritable();
private Text name = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable