当前位置: 首页 > news >正文

Hadoop之——HBASE结合MapReduce批量导入数据

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46463889

废话不多说。直接上代码,你懂得

package hbase;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
/**
 * HBASE结合MapReduce批量导入
 * @author liuyazhuang
 */
public class BatchImport {
	static class BatchImportMapper extends Mapper<LongWritable, Text, LongWritable, Text>{
		SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddHHmmss");
		Text v2 = new Text();
		
		protected void map(LongWritable key, Text value, Context context) throws java.io.IOException ,InterruptedException {
			final String[] splited = value.toString().split("\t");
			try {
				final Date date = new Date(Long.parseLong(splited[0].trim()));
				final String dateFormat = dateformat1.format(date);
				String rowKey = splited[1]+":"+dateFormat;
				v2.set(rowKey+"\t"+value.toString());
				context.write(key, v2);
			} catch (NumberFormatException e) {
				final Counter counter = context.getCounter("BatchImport", "ErrorFormat");
				counter.increment(1L);
				System.out.println("出错了"+splited[0]+" "+e.getMessage());
			}
		};
	}
	
	static class BatchImportReducer extends TableReducer<LongWritable, Text, NullWritable>{
		protected void reduce(LongWritable key, java.lang.Iterable<Text> values, 	Context context) throws java.io.IOException ,InterruptedException {
			for (Text text : values) {
				final String[] splited = text.toString().split("\t");
				
				final Put put = new Put(Bytes.toBytes(splited[0]));
				put.add(Bytes.toBytes("cf"), Bytes.toBytes("date"), Bytes.toBytes(splited[1]));
				put.add(Bytes.toBytes("cf"), Bytes.toBytes("msisdn"), Bytes.toBytes(splited[2]));
				//省略其它字段,调用put.add(....)就可以
				context.write(NullWritable.get(), put);
			}
		};
	}
	
	public static void main(String[] args) throws Exception {
		final Configuration configuration = new Configuration();
		//设置zookeeper
		configuration.set("hbase.zookeeper.quorum", "hadoop0");
		//设置hbase表名称
		configuration.set(TableOutputFormat.OUTPUT_TABLE, "wlan_log");
		//将该值改大,防止hbase超时退出
		configuration.set("dfs.socket.timeout", "180000");
		
		final Job job = new Job(configuration, "HBaseBatchImport");
		
		job.setMapperClass(BatchImportMapper.class);
		job.setReducerClass(BatchImportReducer.class);
		//设置map的输出,不设置reduce的输出类型
		job.setMapOutputKeyClass(LongWritable.class);
		job.setMapOutputValueClass(Text.class);
		
		job.setInputFormatClass(TextInputFormat.class);
		//不再设置输出路径,而是设置输出格式类型
		job.setOutputFormatClass(TableOutputFormat.class);
		
		FileInputFormat.setInputPaths(job, "hdfs://hadoop0:9000/input");
		
		job.waitForCompletion(true);
	}
}


转载于:https://www.cnblogs.com/yutingliuyl/p/7152039.html

相关文章:

  • linux 修改服务器系统时间
  • checkbox 和 selected 混淆
  • extract-text-webpack-plugin
  • 转 docker 部署 kafka
  • RobotFrameWork Ride简单使用及快捷键
  • 3.saltstack-更换master端服务器
  • Maven生成可以直接运行的jar包的方式
  • 随时修改添加,thinkphp小知识
  • 学习ES6 变量的解构赋值
  • 怎样在网站中实现统计访问量的功能
  • cmd批处理常用符号详解
  • Vue 2.3、2.4 知识点小结
  • Servlet---请求封装器
  • powershell 远程重启/关闭服务器
  • 多线程剖析
  • php的引用
  • JavaScript-如何实现克隆(clone)函数
  • [译]Python中的类属性与实例属性的区别
  • 11111111
  • 4. 路由到控制器 - Laravel从零开始教程
  • CSS选择器——伪元素选择器之处理父元素高度及外边距溢出
  • HTML中设置input等文本框为不可操作
  • JavaScript 一些 DOM 的知识点
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • Python利用正则抓取网页内容保存到本地
  • python学习笔记-类对象的信息
  • react 代码优化(一) ——事件处理
  • react-native 安卓真机环境搭建
  • sublime配置文件
  • tensorflow学习笔记3——MNIST应用篇
  • 多线程 start 和 run 方法到底有什么区别?
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 选择阿里云数据库HBase版十大理由
  • ​MySQL主从复制一致性检测
  • # include “ “ 和 # include < >两者的区别
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (Java数据结构)ArrayList
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (二)WCF的Binding模型
  • (利用IDEA+Maven)定制属于自己的jar包
  • (四)图像的%2线性拉伸
  • (算法)求1到1亿间的质数或素数
  • (转)EOS中账户、钱包和密钥的关系
  • .gitattributes 文件
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • @property @synthesize @dynamic 及相关属性作用探究
  • @Transaction注解失效的几种场景(附有示例代码)
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce
  • [AAuto]给百宝箱增加娱乐功能
  • [AI]文心一言出圈的同时,NLP处理下的ChatGPT-4.5最新资讯
  • [bzoj1324]Exca王者之剑_最小割
  • [C++]指针与结构体