MapReduce-从HBase读取处理后再写入HBase

代码如下

package com.hbase.mapreduce;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
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.TableInputFormat;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
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.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @author:FengZhen
* @create:2018年9月17日
* 从HBase读写入HBase
* zip -d HBaseToHBase.jar 'META-INF/.SF' 'META-INF/.RSA' 'META-INF/*SF'
*/
public class HBaseToHBase extends Configured implements Tool{ private static String addr="HDP233,HDP232,HDP231";
private static String port="2181"; public enum Counters { ROWS, COLS, VALID, ERROR, EMPTY, NOT_EMPTY} static class ParseMapper extends TableMapper<ImmutableBytesWritable, Put>{
private byte[] columnFamily = null;
@Override
protected void setup(Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put>.Context context)
throws IOException, InterruptedException {
columnFamily = Bytes.toBytes(context.getConfiguration().get("conf.columnfamily"));
}
@Override
protected void map(ImmutableBytesWritable key, Result value,
Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put>.Context context)
throws IOException, InterruptedException {
context.getCounter(Counters.ROWS).increment(1);
String hbaseValue = null; Put put = new Put(key.get());
for (Cell cell : value.listCells()) {
context.getCounter(Counters.COLS).increment(1);
hbaseValue = Bytes.toString(CellUtil.cloneValue(cell));
if (hbaseValue.length() > 0) {
String top = hbaseValue.substring(0, hbaseValue.length()/2);
String detail = hbaseValue.substring(hbaseValue.length()/2, hbaseValue.length() - 1);
put.addColumn(columnFamily, Bytes.toBytes("top"), Bytes.toBytes(top));
put.addColumn(columnFamily, Bytes.toBytes("detail"), Bytes.toBytes(detail));
context.getCounter(Counters.NOT_EMPTY).increment(1);
}else {
put.addColumn(columnFamily, Bytes.toBytes("empty"), Bytes.toBytes(hbaseValue));
context.getCounter(Counters.EMPTY).increment(1);
}
}
try {
context.write(key, put);
context.getCounter(Counters.VALID).increment(1);
} catch (Exception e) {
e.printStackTrace();
context.getCounter(Counters.ERROR).increment(1);
}
}
} static class ParseTableReducer extends TableReducer<ImmutableBytesWritable, Put, ImmutableBytesWritable>{
@Override
protected void reduce(ImmutableBytesWritable key, Iterable<Put> values,
Reducer<ImmutableBytesWritable, Put, ImmutableBytesWritable, Mutation>.Context context)
throws IOException, InterruptedException {
for (Put put : values) {
context.write(key, put);
}
}
} public int run(String[] arg0) throws Exception {
String table = arg0[0];
String column = arg0[1];
String destTable = arg0[2]; Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",addr);
configuration.set("hbase.zookeeper.property.clientPort", port); Scan scan = new Scan();
if (null != column) {
byte[][] colkey = KeyValue.parseColumn(Bytes.toBytes(column));
if (colkey.length > 1) {
scan.addColumn(colkey[0], colkey[1]);
configuration.set("conf.columnfamily", Bytes.toString(colkey[0]));
configuration.set("conf.columnqualifier", Bytes.toString(colkey[1]));
}else {
scan.addFamily(colkey[0]);
configuration.set("conf.columnfamily", Bytes.toString(colkey[0]));
}
} Job job = Job.getInstance(configuration);
job.setJobName("HBaseToHBase2");
job.setJarByClass(HBaseToHBase2.class); job.getConfiguration().set(TableInputFormat.INPUT_TABLE, table);
job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, destTable); job.setMapperClass(ParseMapper.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class); // job.setReducerClass(ParseTableReducer.class);
job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(Put.class); job.setInputFormatClass(TableInputFormat.class);
TableInputFormat.addColumns(scan, KeyValue.parseColumn(Bytes.toBytes(column)));
job.setOutputFormatClass(TableOutputFormat.class); job.setNumReduceTasks(0); //使用TableMapReduceUtil会报类找不到错误
//Caused by: java.lang.ClassNotFoundException: com.yammer.metrics.core.MetricsRegistry
// TableMapReduceUtil.initTableMapperJob(table, scan, ParseMapper.class, ImmutableBytesWritable.class, Put.class, job);
// TableMapReduceUtil.initTableReducerJob(table, IdentityTableReducer.class, job); return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
String[] params = new String[] {"test_table_mr", "data:info", "test_table_dest"};
int exitCode = ToolRunner.run(new HBaseToHBase2(), params);
System.exit(exitCode);
}
}

 打包测试

zip -d HBaseToHBase.jar 'META-INF/.SF' 'META-INF/.RSA' 'META-INF/*SF'
hadoop jar HBaseToHBase.jar com.hbase.mapreduce.HBaseToHBase

出现的问题

一开始使用额TableMapReduceUtil,但是报下面这个错

Exception in thread "main" java.lang.NoClassDefFoundError: com/yammer/metrics/core/MetricsRegistry
at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.addHBaseDependencyJars(TableMapReduceUtil.java:732)
at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.addDependencyJars(TableMapReduceUtil.java:777)
at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.initTableMapperJob(TableMapReduceUtil.java:212)
at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.initTableMapperJob(TableMapReduceUtil.java:168)
at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.initTableMapperJob(TableMapReduceUtil.java:291)
at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.initTableMapperJob(TableMapReduceUtil.java:92)
at com.hbase.mapreduce.HBaseToHBase.run(HBaseToHBase.java:108)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:76)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:90)
at com.hbase.mapreduce.HBaseToHBase.main(HBaseToHBase.java:115)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.hadoop.util.RunJar.run(RunJar.java:233)
at org.apache.hadoop.util.RunJar.main(RunJar.java:148)
Caused by: java.lang.ClassNotFoundException: com.yammer.metrics.core.MetricsRegistry
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more

 解决,不使用TableMapReduceUtil,分布设置便可解决此问题

MapReduce-从HBase读取数据处理后再写入HBase的更多相关文章

  1. Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例

    1. 把一个文件中的字符串排序后再写入另一个文件 已知s.txt文件中有这样的一个字符串:"hcexfgijkamdnoqrzstuvwybpl" 请编写程序读取数据内容,把数据排 ...

  2. MapReduce和Spark写入Hbase多表总结

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 大家都知道用mapreduce或者spark写入已知的hbase中的表时,直接在mapreduc ...

  3. 个人学习记录1:二维数组保存到cookie后再读取

    二维数组保存到cookie后再读取 var heartsArray = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0],[0,0, ...

  4. hadoop mapreduce 写入hbase报错 Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect

    现象:map任务构造数据正常,reduce任务,开始也正常,速度很快 ,在hbase 的管理界面,可以看到,5W以上的请求数 当reduce 执行到 70% 左右的时候,就堵住了,查看yarn的web ...

  5. 从hbase读取数据优化策略和实验对照结果

    起因:工作须要.我须要每5分钟从hbase中.导出一部分数据,然后导入到ES中.可是在開始阶段编写的python脚本,我发现从hbase读取数据的速度较慢,耗费大量的时间.影响整个导数过程,恐怕无法在 ...

  6. flink-----实时项目---day07-----1.Flink的checkpoint原理分析 2. 自定义两阶段提交sink(MySQL) 3 将数据写入Hbase(使用幂等性结合at least Once实现精确一次性语义) 4 ProtoBuf

    1.Flink中exactly once实现原理分析 生产者从kafka拉取数据以及消费者往kafka写数据都需要保证exactly once.目前flink中支持exactly once的sourc ...

  7. Spark DataFrame写入HBase的常用方式

    Spark是目前最流行的分布式计算框架,而HBase则是在HDFS之上的列式分布式存储引擎,基于Spark做离线或者实时计算,数据结果保存在HBase中是目前很流行的做法.例如用户画像.单品画像.推荐 ...

  8. Ambari部署HDP:HBase Master启动后自动消失

    这是第一次出勤部署产品.遇到不可控问题,解决,写个心得.记录一下吧^^ 在排查问题的过程中,学到不少知识. (1)centos系统盘和数据盘分开,装操作系统的人没有将IT的空间分配出来,所以分区,自动 ...

  9. 记一次OGG数据写入HBase的丢失数据原因分析

    一.现象二.原因排查2.1 SparkStreaming程序排查2.2 Kafka数据验证2.3 查看OGG源码2.3.1 生成Kafka消息类2.3.2 Kafka配置类2.3.3 Kafka 消息 ...

随机推荐

  1. CodeIgniter框架——源码分析之入口文件index.php

    CodeIgniter框架的入口文件主要是配置开发环境,定义目录常量,加载CI的核心类core/CodeIgniter.php.   在index.php中,CI首先做的事情就是设置PHP的错误报告, ...

  2. 【BZOJ1731】[Usaco2005 dec]Layout 排队布局 差分约束

    [BZOJ1731][Usaco2005 dec]Layout 排队布局 Description Like everyone else, cows like to stand close to the ...

  3. 使用RestTemplate post方式提交表单数据

    HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODE ...

  4. 巨蟒python全栈开发django9:一些知识点的汇总

    回顾上周内容: 题目: 1.人民出版社出版过的所有书籍的名字以及作者的姓名(三种写法,笔记中有两种写法) 2.手机以2开头的作者出版过的所有书籍名称以及出版社名称(三种写法,笔记中有1种写法) 1.聚 ...

  5. JavaScript-onerror事件:图片加载失败后不显示

    HTML: <img src="http://www.mazey.net/images/upload/image/20170518/1495122198180663.gif" ...

  6. 我的Java开发学习之旅------>Eclipse 项目有红感叹号解决之道

    今天一个读者问我关于Android通过调用Webservice实现天气预报这篇文章的源码下载后出现的错误 Could not find class 'org.ksoap2.transport.Http ...

  7. 给俺的 CSDN 博客加背景音乐 - 高大尚的《心经》背景音乐

    给俺的 CSDN 博客加背景音乐 - 高大尚的<心经>背景音乐 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途- ...

  8. 教你使用SQL数据库索引(1-15)

    原文地址:http://www.sqlservercentral.com/stairway/72399/ 中文地址:https://www.cnblogs.com/tjy9999/category/4 ...

  9. 001-centos7安装 笔记本 联想G510

    一.准备前提 1.联想G510AT 用winpe进入笔记本电脑,找到一个分区,删除即可 2.使用U盘安装 2.1.准备一个8G 的U盘,格式化ntfs. 2.2.在window下,下载UltraISO ...

  10. Armijo-Goldstein准则与Wolfe-Powell准则

    Armijo-Goldstein准则与Wolfe-Powell准则是不精确的一维搜索的两大准则. 之所以要遵循这些准则是为了能使算法收敛(求最优解).即要使我们的不精确的一维搜索的步长满足一定的规则, ...