功能:把hdfs上的数据写入到hbase表。

hadoop的mapreduce输出要导入到hbase表,最好先输出HFile格式,再导入hbase,因为HFile是hbase的内部存储格式,所以导入效率很高,下面我们来看一下具体怎么做。

1、我们在hdfs上有一个文本文件:

  

2、在hbase表里我们创建一个t1表

  创建语句:create 't1','cf'

3、写MR作业

  

 package cn.tendency.wenzhouhbase.hadoop;

 import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; public class Hadoop2Hbase { @SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.1.124,192.168.1.125,192.168.1.126");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master.port", "60000");
conf.set("hbase.rootdir", "hdfs://192.168.1.122:9000/hbase");
conf.set(TableOutputFormat.OUTPUT_TABLE, "t1"); Job job = new Job(conf, Hadoop2Hbase.class.getSimpleName());
TableMapReduceUtil.addDependencyJars(job);
job.setJarByClass(Hadoop2Hbase.class); job.setMapperClass(HbaseMapper.class);
job.setReducerClass(HbaseReducer.class); job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TableOutputFormat.class); FileInputFormat.setInputPaths(job, "hdfs://192.168.1.123:9000/mytest/*");
job.waitForCompletion(true);
} static class HbaseMapper extends
Mapper<LongWritable, Text, LongWritable, Text> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, LongWritable, Text>.Context context)
throws IOException, InterruptedException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String[] split = value.toString().split("\t");
context.write(
key,
new Text(split[0]+sdf.format(Calendar.getInstance().getTime())
+ "\t" + value.toString()));
}
} static class HbaseReducer extends
TableReducer<LongWritable, Text, NullWritable> {
@Override
protected void reduce(
LongWritable key,
Iterable<Text> values,
Reducer<LongWritable, Text, NullWritable, Mutation>.Context context)
throws IOException, InterruptedException {
for (Text text : values) {
String[] split = text.toString().split("\t");
Put put = new Put(split[0].getBytes());
put.addColumn("cf".getBytes(), "oneColumn".getBytes(), text
.toString().getBytes());
put.addColumn("cf".getBytes(), "id".getBytes(),
split[1].getBytes());
put.addColumn("cf".getBytes(), "name".getBytes(),
split[2].getBytes());
put.addColumn("cf".getBytes(), "age".getBytes(),
split[3].getBytes());
// put.addColumn("cf".getBytes(), "addr".getBytes(),
// split[4].getBytes());
context.write(NullWritable.get(), put);
}
}
}
}

把hdfs数据写入到hbase表的更多相关文章

  1. hbase使用MapReduce操作4(实现将 HDFS 中的数据写入到 HBase 表中)

    实现将 HDFS 中的数据写入到 HBase 表中 Runner类 package com.yjsj.hbase_mr2; import com.yjsj.hbase_mr2.ReadFruitFro ...

  2. Flink 使用(一)——从kafka中读取数据写入到HBASE中

    1.前言 本文是在<如何计算实时热门商品>[1]一文上做的扩展,仅在功能上验证了利用Flink消费Kafka数据,把处理后的数据写入到HBase的流程,其具体性能未做调优.此外,文中并未就 ...

  3. 使用spark将内存中的数据写入到hive表中

    使用spark将内存中的数据写入到hive表中 hive-site.xml <?xml version="1.0" encoding="UTF-8" st ...

  4. 将从数据库中获取的数据写入到Excel表中

    pom.xml文件写入代码,maven自动加载poi-3.1-beta2.jar <!-- https://mvnrepository.com/artifact/poi/poi --> & ...

  5. 使用MapReduce将HDFS数据导入到HBase(三)

    使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中 package com.mengyao.bigdata.hbase; import j ...

  6. Mapreduce读取Hbase表,写数据到一个Hbase表中

    public class LabelJob { public static void main(String[] args) throws Exception { Job job = Job.getI ...

  7. 使用MapReduce将HDFS数据导入到HBase(二)

    package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...

  8. 使用MapReduce将HDFS数据导入到HBase(一)

    package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...

  9. Mysql把一个表的数据写入另一个表中

    一.表结构一样 insert into 表1 select * from 表2 二. 表结构不一样或者取部分列 insert into 表1 (列名1,列名2,列名3) select 列1,列2,列3 ...

随机推荐

  1. 【HC89S003F4开发板】 6crc校验

    HC89S003F4开发板crc校验 前言 第一次用有带crc的mcu 使用资料自带的demo @实现效果 通过PC向MCU发送5个8位数据,MCU返回CRC校验值 void main() { /** ...

  2. Echarts设置y轴值间隔 以及设置 barWidth : 30,//柱图宽度

    需求:如图,y轴之间的距离太小,这样就太过于拥挤了,现在要修改echarts里面的属性,设置y轴值间隔让图表看上去舒服一些.     其实很多问题,真的只是因为自己没有好好的看文档,很多文档上面都写的 ...

  3. 设计模式--装饰者模式(io流中使用的模式)

    重点: 1.动态扩展对象,替换之前需要继承才能实现的功能. 2.具体工作的,仍然是被包装的对象,(有点锦上添花的意思,顾名思义仅仅起到装饰的作用,主体不变). 对比继承: 1.减少类的数量. 如果使用 ...

  4. DEFAULT CURRENT_TIMESTAMP

    alter table t_user_channel_info change update_dttm update_dttm timestamp NOT NULL DEFAULT CURRENT_TI ...

  5. Yii2 redis 使用方法

    /** * 基于 yii2.0 redis使用方法 *///项目根目录命令行执行 composer require --prefer-dist yiisoft/yii2-redis; //在配置文件中 ...

  6. Consul微服务的配置中心体验篇

    Spring Cloud Consul 项目是针对Consul的服务治理实现.Consul是一个分布式高可用的系统,具有分布式.高可用.高扩展性 Consul Consul 是 HashiCorp 公 ...

  7. CCF 2017-03-2 学生排队

    CCF 2017-03-2 学生排队 题目 问题描述 体育老师小明要将自己班上的学生按顺序排队.他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面,然后进行多次调整.一次调整小明可能让一位同学 ...

  8. H5 - 简学

    什么是HTML? HTML 是用来描述网页的一种语言. 0.HTML 指的是超文本标记语言 1.HTML 不是一种编程语言,而是一种标记语言 2.标记语言是一套标记标签 3.HTML 使用标记标签来描 ...

  9. pc端vue 滚动到底部翻页

    html: <div class="list" ref="scrollTopList"> <div class="listsmall ...

  10. H5之拖拽

    步骤: 1.为将要拖拽的元素设置允许拖拽,并赋予dragstart事件将其id转换成数据保存: 2.为容器添加dragover属性添加事件阻止浏览器默认事件,允许元素放置,并赋予drop事件进行元素的 ...