功能:把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. Python29之字符str与字节bytes

    详解见这位大神:https://www.cnblogs.com/xiaobingqianrui/p/9870480.html 实际上字符串和字节之间的转换过程,就是编码解码的过程,我们必须显示的指定编 ...

  2. 第二章 Python基础语法

    2.1 环境的安装 解释器:py2 / py3 (环境变量) 开发工具:pycharm 2.2 编码 编码基础 ascii ,英文.符号,8位为一个东西,2**8 unicode ,万国码,可以表示所 ...

  3. python数据库基础

    1.数据类型:(使用原则:够用就行,尽量使用范围小的) 整数:int,bit 小数:decimal 字符串:varchar(可变长度),char(固定长度字符串) 日期时间:date,time,dat ...

  4. python学习-43 装饰器 -- 函数闭包2

    函数闭包为函数加上认证功能 1.登陆账号 user_dic ={'username':None,'login':False} def auth_func(func): def wrapper(*arg ...

  5. PHP Math函数

    abs() 绝对值.   acos() 反余弦.   acosh() 反双曲余弦.   asin() 反正弦.   asinh() 反双曲正弦.   atan() 反正切.   atan2() 两个参 ...

  6. 使用dockers安装MySQL

    事前准备 关闭selinux setenforce 0 vim /etc/sysconfig/selinux SELINUX=disabled # 若不关闭,使用docker启动mysql5.7镜像容 ...

  7. sublime text3上设置 python 环境

    1. 打开Sublime text 3 安装package control 2. 安装 SublimeREPL Preferences -> package control 或者Ctrl+shi ...

  8. 一、openfeign的自动配置

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 openfeign是一种声明式的webservice客户端调用框架.你只需要声明接口和一 ...

  9. Html5知识精粹纪录

    1. HTML5文档的正文结构及新元素 正文结构: <header> <nav> <section> <aside> <footer> 深入 ...

  10. Lwip与底层的接口

    Lwip有三套api,分别是: raw api:使用方法为使用回调函数,即先注册一个函数,当接受到数据之后调用这个函数.缺点是对于数据连续处理不好. Lwip api:把接收与处理放在一个线程里面.因 ...