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

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

一、hdfs中的数据是这样的

hbase创建好表

create 'NNTB','info'

 二、自定义mr

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
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);
}
}
}

参考自:HBase从hdfs导入数据

参考文献中的hbase导入工具介绍

(my_python_env)[root@hadoop26 ~]# hbase org.apache.hadoop.hbase.mapreduce.Import
ERROR: Wrong number of arguments: 0
Usage: Import [options] <tablename> <inputdir>
By default Import will load data directly into HBase. To instead generate
HFiles of data to prepare for a bulk data load, pass the option:
-Dimport.bulk.output=/path/for/output

在命令中中使用命令进行导入:

hbase org.apache.hadoop.hbase.mapreduce.Import table2 /t2

hdfs数据到hbase过程的更多相关文章

  1. 用mapreduce读取hdfs数据到hbase上

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

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

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

  3. phoenix将hdfs数据导入hbase

    http://phoenix.apache.org/bulk_dataload.html

  4. HBase伪分布式安装(HDFS)+ZooKeeper安装+HBase数据操作+HBase架构体系

    HBase1.2.2伪分布式安装(HDFS)+ZooKeeper-3.4.8安装配置+HBase表和数据操作+HBase的架构体系+单例安装,记录了在Ubuntu下对HBase1.2.2的实践操作,H ...

  5. Hadoop基础-HDFS数据清理过程之校验过程代码分析

    Hadoop基础-HDFS数据清理过程之校验过程代码分析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想称为一名高级大数据开发工程师,不但需要了解hadoop内部的运行机制,还需 ...

  6. MapReduce将HDFS文本数据导入HBase中

    HBase本身提供了很多种数据导入的方式,通常有两种常用方式: 使用HBase提供的TableOutputFormat,原理是通过一个Mapreduce作业将数据导入HBase 另一种方式就是使用HB ...

  7. 【HBase】HBase与MapReduce集成——从HDFS的文件读取数据到HBase

    目录 需求 步骤 一.创建maven工程,导入jar包 二.开发MapReduce程序 三.结果 需求 将HDFS路径 /hbase/input/user.txt 文件的内容读取并写入到HBase 表 ...

  8. 大数据查询——HBase读写设计与实践

    导语:本文介绍的项目主要解决 check 和 opinion2 张历史数据表(历史数据是指当业务发生过程中的完整中间流程和结果数据)的在线查询.原实现基于 Oracle 提供存储查询服务,随着数据量的 ...

  9. 一条数据的HBase之旅,简明HBase入门教程-Write全流程

    如果将上篇内容理解为一个冗长的"铺垫",那么,从本文开始,剧情才开始正式展开.本文基于提供的样例数据,介绍了写数据的接口,RowKey定义,数据在客户端的组装,数据路由,打包分发, ...

随机推荐

  1. 20165221学习基础和C语言基础调查

    1.你有什么技能比大多人(超过90%以上)更好? - 我觉得自己应该改算资质平平的那种人,如果说有什么技能比大多数人更好,我觉得应该是看过自己喜欢的书后,那种记忆能力.就比如自己从小对历史很感兴趣,小 ...

  2. 查看oracle 用户执行的sql语句历史记录

      select * from v$sqlarea t order by t.LAST_ACTIVE_TIME desc

  3. linux 安装matlab

    转自: https://www.cnblogs.com/Amedeo/archive/2018/06/03/9129925.html 一.下载“Crack”和”ISO”文件 下载地址如下所示:http ...

  4. Python-查找两个文件中相同的ip地址

    with open("testt","r") as f1: list1 = f1.readlines() print(list1) list1 = set(li ...

  5. 10分钟搭建Kubernetes容器集群平台【转】

    官方提供3种方式部署Kubernetes minikube Minikube是一个工具,可以在本地快速运行一个单点的Kubernetes,尝试Kubernetes或日常开发的用户使用.不能用于生产环境 ...

  6. vue后台项目记录

    1.当我们用axios进行接口访问时,必须同时使用Qs,否则后端接收不到所传的数据! npm 安装qs,然后引用 import Qs from 'qs' // 创建axios实例 const serv ...

  7. C# 对话框使用整理

    1.保存文件对话框 SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Title = "save file"; sa ...

  8. mysql查询sending data占用大量时间的问题处理

    问题描述:某条sql语句在测试环境执行只需要1秒不到,到了生产环境执行需要8秒以上 在phpmyadmin里面执行性能分析,发现sending data占用了差不多90%以上的时间 查询一下“Send ...

  9. oracle加密传输

    参考文章: http://blog.itpub.net/24052272/viewspace-2129175/ oracle在传输过程中,正常是明文传输的,例如SQL以及执行的结果. 看看做的测试: ...

  10. [转]PhpStorm中如何使用Xdebug工具,入门级操作方法(亲测有效)

    0 前言 网上试过很多方案,有的根本无效,有的是有效一段时间后失效,然而这个方法是一直有效果,所以留底记录一下 1 简介 PhpStorm是一个轻量级且便捷的PHP IDE,其提供的智能代码补全,快速 ...