编写mapreduce程序从HBase的一张表中求某一列的平均数

 表中的数据

求HBase数据库中data_t表中的attention列的均值

package com.hbase.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.List;

public class GetAttentionMean {

	public static void main(String[] args) throws Exception {
		Configuration conf = HBaseConfiguration.create();

		FileSystem fs = FileSystem.get(conf);

		Job job = Job.getInstance(conf);

		job.setJarByClass(GetAttentionMean.class);

		Scan scan = new Scan();
		scan.addColumn("Info".getBytes(), "attention".getBytes());

		TableMapReduceUtil.initTableMapperJob(
				"data_t".getBytes(), // 指定表名
				scan, // 指定扫描数据的条件
				MyMapper.class, // 指定mapper class
				Text.class, // mapper阶段的输出的key的类型
				DoubleWritable.class, // mapper阶段的输出的value的类型
				job // job对象
		);

		job.setReducerClass(MyReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(DoubleWritable.class);

		Path outputPath = new Path("/attention/mean");

		if (fs.exists(outputPath)) {
			fs.delete(outputPath, true);
		}

		FileOutputFormat.setOutputPath(job, outputPath);

		boolean isSuccess = job.waitForCompletion(true);
		
		if (!isSuccess) {
			throw new IOException("任务运行错误!");
		}

		System.exit(isSuccess ? 0 : 1);
	}

	public static class MyMapper extends TableMapper<Text, DoubleWritable> {

		Text outKey = new Text("attention_mean");
		DoubleWritable outValue = new DoubleWritable();

		@Override
		protected void map(ImmutableBytesWritable key, Result value, Context context)
				throws IOException, InterruptedException {

			boolean isContainsColumn = value.containsColumn("Info".getBytes(), "attention".getBytes());

			if (isContainsColumn) {
				List<Cell> listCells = value.getColumnCells("Info".getBytes(), "attention".getBytes());
				Cell cell = listCells.get(0);
				byte[] cloneValue = CellUtil.cloneValue(cell);
				double attention = Double.valueOf(Bytes.toString(cloneValue));
				outValue.set(attention);
				context.write(outKey, outValue);
			}

		}

	}

	public static class MyReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {

		DoubleWritable outValue = new DoubleWritable();

		@Override
		protected void reduce(Text key, Iterable<DoubleWritable> values, Context context)
				throws IOException, InterruptedException {

			int count = 0;
			double sum = 0;
			for (DoubleWritable value : values) {
				count++;
				sum += value.get();
			}

			double attention_mean = sum / count;
			outValue.set(attention_mean);
			context.write(key, outValue);
		}
	}
}

结果:

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hbase</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>demo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
  <dependencies>
    <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.6.1</version>
        </dependency>
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
        <!--HBase MapReduce API-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.6.1</version>
        </dependency>
  </dependencies>
</project>

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MapReduce 是一个用于处理大规模数据的分布式编程框架。在 HBase 中,MapReduce 可以用来统计成绩表中的单科排名和总分排名。 下面是一个简单的 MapReduce 示例,用于统计 HBase 中成绩表中的单科排名: 1. 定义 Mapper 类,其中实现 map() 方法,接收输入的 key-value 对(即 HBase 中的行键和列族),并处理成绩数据。 2. 定义 Reducer 类,其中实现 reduce() 方法,接收 Mapper 输出的 key-value 对,并对成绩数据进行排序。 3. 使用 HBase API 读取成绩表中的数据,并将其传递给 Mapper 类。 4. 运行 MapReduce 程序,并输出统计结果。 统计总分排名的方法类似,只需要在 Mapper 和 Reducer 中对所有科目的成绩进行求和,然后在 Reducer 中对总分进行排序即可。 ### 回答2: MapReduce是一种用于处理大规模数据集的分布式计算框架。HBase是一种分布式、面向列的开源数据库,可以在Hadoop集群上进行横向扩展和高可靠性存储。提供了对实时读写的支持。 在MapReduce项目中,我们可以使用HBase作为数据源,通过MapReduce作业来统计HBase成绩表中的单科排名和总分排名。 首先,我们需要定义输入格式,即将HBase的数据转化为适合MapReduce处理的键值对格式。可以使用HBase提供的TableInputFormat类来读取HBase表中的数据,并将其转化为key-value对。 接下来,我们需要实现Mapper类。Mapper负责将输入的键值对进行处理,提取出需要的数据,并以键值对的形式输出给Reducer。在本例中,我们可以将学生的姓名作为键,将成绩作为值进行输出。 Reducer类负责对Mapper输出的键值对进行处理,计算每个学生的总分,并将结果进行排序。在本例中,我们可以使用TreeMap来对学生的总分进行排序。 最后,我们还需要定义输出格式,将Reducer的输出写入到HBase表中。可以使用HBase提供的TableOutputFormat类来将结果写入到HBase表中,以更新学生的排名信息。 综上所述,通过以上步骤,我们可以实现MapReduce项目来统计HBase成绩表中的单科排名和总分排名。 ### 回答3: MapReduce是一种用于处理大规模数据集的编程模型和算法。在Hadoop生态系统中,MapReduce被广泛用于并行处理和分析大数据。 对于统计HBase成绩表中的单科排名和总分排名,我们可以使用MapReduce来完成。 首先,我们需要编写Mapper来读取HBase表中的数据,并按照学生ID作为键,成绩数据作为值进行映射。这样可以保证每个Mapper处理一行数据。然后,我们可以在Mapper中计算单科分数或总分,作为中间结果。 接下来,我们需要编写Reducer来合并和处理Mapper的输出。在Reducer中,我们可以根据需要对中间结果进行排序和聚合操作。对于单科排名,我们可以根据每个学生的成绩进行排序,并分配排名。对于总分排名,我们可以按照学生总分进行排序,并为每个学生分配排名。 最后,我们将Reducer的输出写回到HBase表中,以便我们可以在需要时查询排名结果。 整个过程中,MapReduce能够充分利用分布式计算的优势来高效地处理大数据集。通过适当的数据处理和运算,我们可以得到HBase成绩表中的单科排名和总分排名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值