【hadoop】案例:MapReduce批量写入HBase

1.需求分析

我们仍然以美国各个气象站每年的气温数据集为例,现在要求使用MapReduce读取该数据集,然后批量写入HBase数据库,最后利用HBase shell根据行键即席查询气温数据。

2.数据集准备

数据集的文件名为temperature.log,里面包含美国各个气象站每年的气温数据,数据的第一列为气象站ID,第二列为年份,第三列为气温值。具体样本数据如下所示: 03103,1980,41 03103,1981,98 03103,1982,70 03103,1983,74 03103,1984,77

3.代码实现

Mapper:

复制代码
public static class MyMapper extends Mapper<LongWritable, Text,LongWritable,Text>{
	private Text word = new Text();
	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		//解析每条气温记录
		String[] records = value.toString().split(",");
		int length = records.length;
		if(length==3){
			//设置HBase行键rowKey
			String rowKey = records[0]+":"+records[1];
			word.set(rowKey+","+value.toString());
			context.write(key,word);
		}
	}}

Reducer:

复制代码
public static class MyReducer extends TableReducer<LongWritable,Text, ImmutableBytesWritable>{
@Override
protected void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for(Text value:values){
String[] splits = value.toString().split(",");
String rowKey=splits[0];
//获取第一列作为RowKey
Put put =new Put(Bytes.toBytes(splits[0]));
//获取其他列作为HBase列簇中的字段
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("id"), Bytes.toBytes(splits[0]));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("year"), Bytes.toBytes(splits[1]));
put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("temperature"), Bytes.toBytes(splits[2]));
ImmutableBytesWritable keys = new ImmutableBytesWritable(rowKey.getBytes());
context.write(keys,put);
}
}}

Main:

复制代码
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
//设置HBase配置连接
Configuration conf= HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2,hadoop3");
conf.set("hbase.zookeeper.property.clientPort", "2181");
Job job = new Job(conf, "BatchImportHBase");
job.setJobName("BatchImportHBase");
job.setJarByClass(BatchImportHBase.class);
job.setMapperClass(MyMapper.class);
//执行reducer类写入HBase
TableMapReduceUtil.initTableReducerJob("temperature", MyReducer.class, job, null, null, null, null, false);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(Put.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
job.waitForCompletion(true) ;
}

4.测试运行

  1. 创建HBase表 hbase(main):002:0> create 'temperature','cf'

  2. 提交MapReduce作业

  3. 根据Rowkey查询气温数据

相关推荐
顧棟1 天前
HDFS元数据与auditlog结合Hive元数据统计分析
hive·hadoop·hdfs
z***89711 天前
【分布式】Hadoop完全分布式的搭建(零基础)
大数据·hadoop·分布式
Kevinyu_1 天前
责任链模式
java·hadoop·责任链模式
yumgpkpm1 天前
腾讯云TBDS与CDH迁移常见问题有哪些?建议由CDH迁移到CMP 7.13 平台(类Cloudera CDP,如华为鲲鹏 ARM 版)
hive·hadoop·zookeeper·flink·spark·kafka·hbase
yumgpkpm2 天前
数据可视化AI、BI工具,开源适配 Cloudera CMP 7.3(或类 CDP 的 CMP 7.13 平台,如华为鲲鹏 ARM 版)值得推荐?
人工智能·hive·hadoop·信息可视化·kafka·开源·hbase
干就完事了3 天前
Hive视图
数据仓库·hive·hadoop
Linux Huang3 天前
【Dinky】IDEA运行出现HistoryServer异常
java·hadoop·flink·intellij idea
A尘埃3 天前
Hive基于Hadoop的数据仓库工具
数据仓库·hive·hadoop
Macbethad3 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
1***s6323 天前
MySQLGraphQL案例
django·hbase·图形洹染