hdfs数据到hbase过程

将HDFS上的文件中的数据导入到hbase中

实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具

hbase先创建好表   create 'TB','info'

下面是实现代码:

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.lib.input.FileInputFormat; import java.io.IOException;
/**
* 用于HDFS的数据读取,写入到hbase中,
* hbase里预先创建好表:create 'NNTB','info'
* */
public class HdfsToHBase {
public static void main(String[] args) throws Exception{
System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.6");//这行我是本地运行所需指定的hadoop home
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "202.168.27.196:2181");//ip乱写的,端口默认2181
conf.set(TableOutputFormat.OUTPUT_TABLE, "NNTB");
Job job = Job.getInstance(conf, HdfsToHBase.class.getSimpleName());
TableMapReduceUtil.addDependencyJars(job);
job.setJarByClass(HdfsToHBase.class); job.setMapperClass(HdfsToHBaseMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setReducerClass(HdfsToHBaseReducer.class); FileInputFormat.addInputPath(job, new Path("hdfs://202.168.27.196:9000/user/hadoop/gznt/gznt_bmda/*"));
job.setOutputFormatClass(TableOutputFormat.class);
job.waitForCompletion(true);
} public static class HdfsToHBaseMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text outKey = new Text();
private Text outValue = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] splits = value.toString().split("\t");
outKey.set(splits[0]);
outValue.set(splits[1]+"\t"+splits[2]+"\t"+splits[3]+"\t"+splits[4]);
context.write(outKey, outValue);
}
}
//::: create 'NNTB','info'
public static class HdfsToHBaseReducer extends TableReducer<Text, Text, NullWritable> {
@Override
protected void reduce(Text k2, Iterable<Text> v2s, Context context) throws IOException, InterruptedException {
Put put = new Put(k2.getBytes());
for (Text v2 : v2s) {
String[] splis = v2.toString().split("\t");
//info,对应hbase列族名
if(splis[0]!=null && !"NULL".equals(splis[0])){
put.addColumn("info".getBytes(), "NodeCode".getBytes(),splis[0].getBytes());
}
if(splis[1]!=null && !"NULL".equals(splis[1])){
put.addColumn("info".getBytes(), "NodeType".getBytes(),splis[1].getBytes());
}
if(splis[2]!=null && !"NULL".equals(splis[2])){
put.addColumn("info".getBytes(), "NodeName".getBytes(),splis[2].getBytes());
}
if(splis[3]!=null && !"NULL".equals(splis[3])){
put.addColumn("info".getBytes(), "IsWarehouse".getBytes(),splis[3].getBytes());
}
}
context.write(NullWritable.get(),put);
}
}
}

  

用mapreduce读取hdfs数据到hbase上的更多相关文章

  1. bulk-load 装载HDFS数据到HBase

    bulk-load的作用是用mapreduce的方式将hdfs上的文件装载到hbase中,对于海量数据装载入hbase非常有用,参考http://hbase.apache.org/docs/r0.89 ...

  2. hdfs数据到hbase过程

    需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 hbase创建好表 cre ...

  3. 使用MapReduce将HDFS数据导入Mysql

    使用MapReduce将Mysql数据导入HDFS代码链接 将HDFS数据导入Mysql,代码示例 package com.zhen.mysqlToHDFS; import java.io.DataI ...

  4. MapReduce读取hdfs上文件,建立词频的倒排索引到Hbase

    Hdfs上的数据文件为T0,T1,T2(无后缀): T0: What has come into being in him was life, and the life was the light o ...

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

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

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

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

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

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

  8. spark读取hdfs数据本地性异常

    在分布式计算中,为了提高计算速度,数据本地性是其中重要的一环. 不过有时候它同样也会带来一些问题. 一.问题描述 在分布式计算中,大多数情况下要做到移动计算而非移动数据,所以数据本地性尤其重要,因此我 ...

  9. spark读取hdfs数据本地性异常【转】

    在分布式计算中,为了提高计算速度,数据本地性是其中重要的一环. 不过有时候它同样也会带来一些问题. 一.问题描述 在分布式计算中,大多数情况下要做到移动计算而非移动数据,所以数据本地性尤其重要,因此我 ...

随机推荐

  1. MySQL学习(一) 数据表基本操作

    创建数据库:create database db_name 查看数据库结构:show create database db_name 删除数据库:drop database db_name 查看数据库 ...

  2. Computer Hardware

    Computer Hardware Para 1 Computer hardware can be divides into four categories: input hardware, stor ...

  3. luogu P3787 冰精冻西瓜

    嘟嘟嘟 好题,好题…… 看这个修改和询问,就知道要么是求完dfs序后线段树维护,要么是树剖.又因为这道题都是子树的操作,没有链上的,所以线段树就够了. 然而重点不是这个.这道题最麻烦的是线段树push ...

  4. [19/03/21-星期四] 异常(Exception) (一)

    一.引言 在实际工作中,我们遇到的情况不可能是非常完美的.比如:你写的某个模块,用户输入不一定符合你的要求;你的程序要打开某个文件, 这个文件可能不存在或者文件格式不对 ,你要读取数据库的数据,数据可 ...

  5. [18/12/01]super 关键字和final 关键字

    一.super 关键字 1.super是直接父类对象的引用.可以通过super来访问父类中被子类覆盖的方法或属性. 使用super调用普通方法,语句没有位置限制,可以在子类中随便调用. 代码示例: c ...

  6. [luoguP4306][JSOI2010]连通数

    \[Yeasion\] \[Nein\] 其实我很奇怪为什么我的正解和输出\(N \times N\)的效果是一样的.....嗯,大概是\(RP\)问题吧.... 嗯首先来看一下题目: 题目描述: 度 ...

  7. java 注解annotation的使用,以及反射如何获取注解

     一.注解基本知识 1.元注解 元注解是指注解的注解.包括  @Retention @Target @Document @Inherited四种. 1. Annotation型定义为@interfac ...

  8. UVA - 136 Ugly Numbers(丑数,STL优先队列+set)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...

  9. 没有美工一样可以获取设计各种各样的UI图

    没有美工一样可以获取设计各种各样的UI图 http://www.iconfont.cn

  10. 对UIImageView+WebCache的封装

    UIImageView+SDWebImage.h #import <UIKit/UIKit.h> typedef void(^DownloadImageSuccessBlock)(UIIm ...