测试数据:

datas

1001    lilei   17  13800001111
1002 lily 16 13800001112
1003 lucy 16 13800001113
1004 meimei 16 13800001114

数据批量导入使用mr,先生成HFile文件然后在用completebulkload工具导入。

1、需要先在hbase 创建表名:

hbase> create 'student', {NAME => 'info'}

maven pom.xml配置文件如下:

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>

<!-- hbase -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.0.0</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.0.0</version>
</dependency>

编写MapReduce代码如下:

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
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 org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* @author 作者 E-mail:
* @version 创建时间:2016年3月2日 下午4:15:57
* 类说明
*/
public class CreateHfileByMapReduce { public static class MyBulkMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>{
@Override
protected void setup( Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context )
throws IOException, InterruptedException { super.setup( context );
}
@Override
protected void map( LongWritable key, Text value,
Context context )
throws IOException, InterruptedException {
String[] split = value.toString().split("\t"); // 根据实际情况修改
if (split.length == 4){
byte[] rowkey = split[0].getBytes();
ImmutableBytesWritable imrowkey = new ImmutableBytesWritable( rowkey );
context.write(imrowkey, new KeyValue(rowkey, Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(split[1])));
context.write(imrowkey, new KeyValue(rowkey, Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(split[2])));
context.write(imrowkey, new KeyValue(rowkey, Bytes.toBytes("info"), Bytes.toBytes("phone"), Bytes.toBytes(split[3])));
}
}
} @SuppressWarnings( "deprecation" )
public static void main( String[] args ) {
if (args.length != 4){
System.err.println("Usage: CreateHfileByMapReduce <table_name><data_input_path><hfile_output_path> ");
System.exit(2);
} String tableName = args[0];
String inputPath = args[1];
String outputPath = args[2]; /* String tableName = "student";
String inputPath = "hdfs://node2:9000/datas";
String outputPath = "hdfs://node2:9000/user/output";*/
HTable hTable = null;
Configuration conf = HBaseConfiguration.create();
try {
hTable = new HTable(conf, tableName);
Job job = Job.getInstance( conf, "CreateHfileByMapReduce");
job.setJarByClass( CreateHfileByMapReduce.class );
job.setMapperClass(MyBulkMapper.class);
job.setInputFormatClass(org.apache.hadoop.mapreduce.lib.input.TextInputFormat.class);
//
HFileOutputFormat.configureIncrementalLoad(job, hTable);
FileInputFormat.addInputPath( job, new Path(inputPath) );
FileOutputFormat.setOutputPath( job, new Path(outputPath) );
System.exit( job.waitForCompletion(true)? 0: 1 ); }
catch ( Exception e ) { e.printStackTrace();
} }
}

注: 借助maven的assembly插件, 生成胖jar包(就是把依赖的zookeeper和hbase jar包都打到该MapReduce包中), 否则的话, 就需要用户静态配置, 在Hadoop的class中添加zookeeper和hbase的配置文件和相关jar包.

最终的jar包为 bulk.jar, 主类名为cn.bd.batch.mr.CreateHfileByMapReduce, 生成HFile, 增量热载入hbase
sudo -u hdfs hadoop jar <xxoo>.jar <MainClass> <table_name> <data_input_path> <hfile_output_path>
hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles <hfile_output_path> <table_name>

hadoop jar bulk.jar cn.bd.batch.mr.CreateHfileByMapReduce student /datas /user/output

hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /user/output student

本文参考地址:http://www.cnblogs.com/mumuxinfei/p/3823367.html

数据批量导入HBase的更多相关文章

  1. 用MR生成HFile文件格式后,数据批量导入HBase

    环境hadoop cdh5.4.7 hbase1.0.0 测试数据: topsid  uid roler_num typ 10 111111 255 0 在Hbase 创建t2数据库: create ...

  2. [Django]数据批量导入

    前言:历经一个月的复习,考试终于结束了.这期间上班的时候有研究了Django网页制作过程中,如何将数据批量导入到数据库中. 这个过程真的是惨不忍睹,犯了很多的低级错误,这会在正文中说到的.再者导入数据 ...

  3. 将Excle中的数据批量导入数据库

    namespace 将Excle中的数据批量导入数据库{    class Program    {        static void Main(string[] args)        { S ...

  4. 将execl里的数据批量导入数据库

    本文将采用NPOI插件来读取execl文件里的数据,将数据加载到内存中的DataTable中 /// <summary> /// 将Excel转换为DataTable /// </s ...

  5. mysql中把一个表的数据批量导入另一个表中

    mysql中把一个表的数据批量导入另一个表中   不管是在网站开发还是在应用程序开发中,我们经常会碰到需要将MySQL或MS SQLServer某个表的数据批量导入到另一个表的情况,甚至有时还需要指定 ...

  6. .net core利用MySqlBulkLoader大数据批量导入MySQL

    最近用core写了一个数据迁移小工具,从SQLServer读取数据,加工后导入MySQL,由于数据量太过庞大,数据表都过百万,常用的dapper已经无法满足.三大数据库都有自己的大数据批量导入数据的方 ...

  7. Java实现Excel数据批量导入数据库

    Java实现Excel数据批量导入数据库 概述: 这个小工具类是工作中的一个小插曲哦,因为提数的时候需要跨数据库导数... 有的是需要从oracle导入mysql ,有的是从mysql导入oracle ...

  8. Java实现数据批量导入mysql数据库

    本文完全照搬别人的. 原文标题:Java实现数据批量导入数据库(优化速度-2种方法) 原文地址:https://blog.csdn.net/qy20115549/article/details/526 ...

  9. SQL Server中bcp命令的用法以及数据批量导入导出

    原文:SQL Server中bcp命令的用法以及数据批量导入导出 1.bcp命令参数解析 bcp命令有许多参数,下面给出bcp命令参数的简要解析 用法: bcp {dbtable | query} { ...

随机推荐

  1. VB - FSO的常见对象和方法

    1. set fs=wscript.createobject(“scripting.filesystemobject”) 这样就建立了fso的模型.如果要释放的话也很简单, set fs=nothin ...

  2. 深度学习大规模MIMO中的功率分配

    摘要-本文使用深度学习的方法在大规模MIMO网络的下行链路中执行max-min和max-prod功率分配.更确切地说,与传统的面向优化的方法相比,训练深度神经网络来学习用户设备(UE)的位置和最优功率 ...

  3. BUUCTF 梅花香自苦寒来

    梅花香自苦寒来 打开图片可以看到,在jpg后面有大量的数据,将它保存出来,可以看出是十六进制,将它转为ascii,写脚本 with open('hex.txt','r') as h: h=h.read ...

  4. thinkphp5 自动注册Hook机制钩子扩展

    Hook.php 文件已更新1.修复在linux环境下类的 \ 在basename 下无法获取到类名的问题2.修复linux 环境下无法使用hook::call 调用失败问题 请先安装thinkphp ...

  5. ThinkPHP5.1x 中间件实现原理

    ThinkPHP5.1x的中间件,其核心还是闭包函数的应用,来实现“责任链”模式: 模拟代码: <?php //模拟的控制器 class Controller { public function ...

  6. showfont - 展示当前"显示屏-字体 映射"中的所有字符.

    总览 showfont 描述 showfont 利用8-bit控制台模式的 Application Charset Map(应用字符集映射) 中一些连续的开关, 以当前字体在屏幕上展示所有的256个或 ...

  7. sqlite3 C语言 API 函数

    int sqlite3_open(char *path, sqlite3 **db): 功能:打开sqlite数据库 参数:   path: 数据库文件路径 db: 指向sqlite句柄的指针 返回值 ...

  8. 前端学习(二十六)移动端s(笔记)

    ===================================================弹性布局rem布局---------------------------------------- ...

  9. TextView点击后背景颜色、文字颜色改变(转)

    转自:http://blog.csdn.net/u013278940/article/details/51152655 TextView本没有点击效果,故为实现点击后文字颜色改变和背景改变,需要写se ...

  10. QT问题解决

    1.pro文件下各个变量的含义 https://www.zybuluo.com/breakerthb/note/582395 2.如何在pro文件中导入其他的库 https://blog.csdn.n ...