【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查询气温数据

相关推荐
那就学有所成吧(˵¯͒¯͒˵)2 小时前
大数据项目(一):Hadoop 云网盘管理系统开发实践
大数据·hadoop·分布式
BYSJMG10 小时前
计算机毕业设计选题推荐:基于Hadoop的城市交通数据可视化系统
大数据·vue.js·hadoop·分布式·后端·信息可视化·课程设计
沃达德软件14 小时前
智慧警务技战法
大数据·数据仓库·hadoop·深度学习·机器学习·数据挖掘
TTBIGDATA15 小时前
【Hue】Ambari 页面启动 Hue 失败 user ‘hadoop‘ does not exist
java·hadoop·ambari
小园子的小菜1 天前
深入剖析HBase HFile原理:文件结构、Block协作与缓存机制
数据库·缓存·hbase
尘世壹俗人2 天前
Zookeeper、Hadoop、Hive配置Kerberos
hadoop
bigdata-rookie2 天前
Spark shuffle 和 MapReduce shuffle 的区别
大数据·spark·mapreduce
B站计算机毕业设计超人2 天前
计算机毕业设计hadoop+spark+hive在线教育可视化 课程推荐系统 大数据毕业设计(源码+LW文档+PPT+讲解)
大数据·人工智能·hive·hadoop·scrapy·spark·课程设计
普通网友2 天前
Hive ACID 事务表实战:插入 / 更新 / 删除操作的配置与使用限制
数据仓库·hive·hadoop
独自归家的兔2 天前
windows Hive使用全攻略:从入门到实战,轻松搞定大数据处理 - Hadoop windows安装
数据仓库·hive·hadoop