数据批量导入HBase
测试数据:
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的更多相关文章
- 用MR生成HFile文件格式后,数据批量导入HBase
环境hadoop cdh5.4.7 hbase1.0.0 测试数据: topsid uid roler_num typ 10 111111 255 0 在Hbase 创建t2数据库: create ...
- [Django]数据批量导入
前言:历经一个月的复习,考试终于结束了.这期间上班的时候有研究了Django网页制作过程中,如何将数据批量导入到数据库中. 这个过程真的是惨不忍睹,犯了很多的低级错误,这会在正文中说到的.再者导入数据 ...
- 将Excle中的数据批量导入数据库
namespace 将Excle中的数据批量导入数据库{ class Program { static void Main(string[] args) { S ...
- 将execl里的数据批量导入数据库
本文将采用NPOI插件来读取execl文件里的数据,将数据加载到内存中的DataTable中 /// <summary> /// 将Excel转换为DataTable /// </s ...
- mysql中把一个表的数据批量导入另一个表中
mysql中把一个表的数据批量导入另一个表中 不管是在网站开发还是在应用程序开发中,我们经常会碰到需要将MySQL或MS SQLServer某个表的数据批量导入到另一个表的情况,甚至有时还需要指定 ...
- .net core利用MySqlBulkLoader大数据批量导入MySQL
最近用core写了一个数据迁移小工具,从SQLServer读取数据,加工后导入MySQL,由于数据量太过庞大,数据表都过百万,常用的dapper已经无法满足.三大数据库都有自己的大数据批量导入数据的方 ...
- Java实现Excel数据批量导入数据库
Java实现Excel数据批量导入数据库 概述: 这个小工具类是工作中的一个小插曲哦,因为提数的时候需要跨数据库导数... 有的是需要从oracle导入mysql ,有的是从mysql导入oracle ...
- Java实现数据批量导入mysql数据库
本文完全照搬别人的. 原文标题:Java实现数据批量导入数据库(优化速度-2种方法) 原文地址:https://blog.csdn.net/qy20115549/article/details/526 ...
- SQL Server中bcp命令的用法以及数据批量导入导出
原文:SQL Server中bcp命令的用法以及数据批量导入导出 1.bcp命令参数解析 bcp命令有许多参数,下面给出bcp命令参数的简要解析 用法: bcp {dbtable | query} { ...
随机推荐
- Debian 防火墙 打开 关闭
Debian原来用的是UFW防火墙,之前没接触过这种类型防火墙,懵逼了半天,这里记录一下简单的使用规则,后期在使用过程中慢慢完善UFW防火墙的使用操作方法: 查看防火墙现有规则: ufw status ...
- bash 中的特殊变量
bash 中的特殊变量 变量 说明 ------------------------------------ $0 当前脚本的文件名 $n 第n个位置参数 $* 传递给脚本或函数的所有参数,$*会将这 ...
- 服务器oracle数据库定时备份
首先要先建立一个.bat的文件 然后执行这个bat文件 测试是否能得到这个收据库的打包文件. bat文件内容: @echo off@color bdel /f /s /q D:\oracle\bac ...
- HDU - 2181 C - 哈密顿绕行世界问题(DFS
题目传送门 C - 哈密顿绕行世界问题 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...
- Ecplise无法启动“failed to create the JAVA Virtual Machine”
打开Ecplise时报错:“failed to create the JAVA Virtual Machine”,java配制也没有问题,然后尝试运行eclipsec.exe,报错了另一个信息:“Co ...
- GitHub托管代码-学习笔记
1.注册github账号 https://github.com/ 2.下载GitHub Desktop软件 https://desktop.github.com/ 在下载的软件上登陆GitHub账户 ...
- shell函数与变量
- Foobar 2000增加APE播放支持的方法
这里说明一下APE,它是一种常用的无损音乐的存储格式,通常会有将原始音乐光盘数字化后存储的APE文件搭配一个CUE文件使用.这个APE存储了音乐的原始数据,而CUE文件则是一个索引文件,用来标记音乐光 ...
- PHP7.1以上版本 count()报错
报错信息如下: count(): Parameter must be an array or an object that implements Countable (View: D:\fookusy ...
- sparksql 自定义用户函数(UDF)
自定义用户函数有两种方式,区别:是否使用强类型,参考demo:https://github.com/asker124143222/spark-demo 1.不使用强类型,继承UserDefinedAg ...