使用MapReduce将HDFS数据导入到HBase(三)
使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中
package com.mengyao.bigdata.hbase; import java.io.IOException; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;
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 mengyao
* HBase-1.0.1.1、Hadoop-2.6.0
*
*/
public class BulkLoadApp { private static Configuration conf = HBaseConfiguration.create();
private static String inPath;
private static String outPath;
private static String tableName;
static {
conf.set("hbase.zookeeper.quorum", "bdata200,bdata202,bdata203");
conf.set("hbase.zookeeper.property.clientPort", "2181");
} static class BulkLoadMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
private ImmutableBytesWritable row;
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
//id,username,email,birthday,mobile,phone,modified
String[] fields = line.split("\t");
String id = fields[0];
String username = fields[1];
String mail = fields[2];
String birthday = fields[3];
String mobile = fields[4];
String phone = fields[5];
String regtime = fields[6];
String rowKey = DigestUtils.md5Hex(id);
row = new ImmutableBytesWritable(Bytes.toBytes(rowKey));
Put put = new Put(Bytes.toBytes(rowKey), System.currentTimeMillis());
if (!StringUtils.isEmpty(id)) {
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("id"), Bytes.toBytes(id));
}
if (!StringUtils.isEmpty(username)) {
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("username"), Bytes.toBytes(username));
}
if (!StringUtils.isEmpty(mail)) {
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("mail"), Bytes.toBytes(mail));
}
if (!StringUtils.isEmpty(birthday) || !birthday.equals("0000-00-00")) {
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes(birthday));
}
if (!StringUtils.isEmpty(mobile)) {
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("mobile"), Bytes.toBytes(mobile));
}
if (!StringUtils.isEmpty(phone)) {
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("phone"), Bytes.toBytes(phone));
}
if (!StringUtils.isEmpty(regtime)) {
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("modified"), Bytes.toBytes(regtime));
}
context.write(row, put);
}
} static int createJob(String[] args) throws Exception {
inPath = args[0];
outPath = args[1];
tableName = args[2];
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Job job=Job.getInstance(conf);
job.setJarByClass(BulkLoadApp.class);
job.setMapperClass(BulkLoadMapper.class);
job.setNumReduceTasks(0);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
job.setOutputFormatClass(HFileOutputFormat2.class);
HFileOutputFormat2.configureIncrementalLoad(job, table, connection.getRegionLocator(TableName.valueOf(tableName)));
FileInputFormat.addInputPath(job,new Path(inPath));
FileOutputFormat.setOutputPath(job,new Path(outPath));
return job.waitForCompletion(true)?0:1;
} /**
* use commond:
* 1、hadoop jar MyJar INPUT_FILE OUTPUT_DIR TABLE_NAME
* hadoop jar bigdata.jar /tag/data/user/haier_user.csv /tag/data/user/haier_user_out tbl_shopuser
* 2、hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles OUTPUT_DIR TABLE_NAME
* hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /tag/data/user/haier_user_out tbl_shopuser
* @param args
* @throws Exception
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
if (args.length!=3) {
System.out.println("Usage: "+BulkLoadApp.class.getName()+" Input paramters <INPUT_PATH> <OUTPUT_PATH> <TABLE_NAME>");
} else {
int status = createJob(args);
if (status == 0) {
LoadIncrementalHFiles loadHFiles = new LoadIncrementalHFiles(conf);
loadHFiles.doBulkLoad(new Path(outPath), new HTable(conf, Bytes.toBytes(tableName)));
}
System.exit(status);
}
} }
使用MapReduce将HDFS数据导入到HBase(三)的更多相关文章
- 使用MapReduce将HDFS数据导入到HBase(二)
package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...
- 使用MapReduce将HDFS数据导入到HBase(一)
package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...
- 使用MapReduce将HDFS数据导入Mysql
使用MapReduce将Mysql数据导入HDFS代码链接 将HDFS数据导入Mysql,代码示例 package com.zhen.mysqlToHDFS; import java.io.DataI ...
- 使用MapReduce将mysql数据导入HDFS
package com.zhen.mysqlToHDFS; import java.io.DataInput; import java.io.DataOutput; import java.io.IO ...
- 用mapreduce读取hdfs数据到hbase上
hdfs数据到hbase过程 将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 hbase先创建好表 cre ...
- HBase(三): Azure HDInsigt HBase表数据导入本地HBase
目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见 HDP2.4安装(五):集群及组件安装 , ...
- 将Excel中数据导入数据库(三)
上篇文章将Excel中数据导入数据库时,将从Excel读入的数据均转换成了数据库相应字段的类型,其实这是没有必要的,因为对于数据库各种类型的插入,均可以字符串格式插入.比如表WQ_SWMSAR_A字段 ...
- HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)
HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...
- 把hdfs数据写入到hbase表
功能:把hdfs上的数据写入到hbase表. hadoop的mapreduce输出要导入到hbase表,最好先输出HFile格式,再导入hbase,因为HFile是hbase的内部存储格式,所以导入效 ...
随机推荐
- 【题解搬运】PAT_A1020 树的遍历
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- python 基础篇 04(列表 元组 常规操作)
本节主要内容:1. 列表2. 列表的增删改查3. 列表的嵌套4. 元组和元组嵌套5. range 一. 列表1.1 列表的介绍列表是python的基础数据类型之一 ,其他编程语言也有类似的数据类型. ...
- virsh查看迁移信息的两个命令
virsh qemu-monitor-command uuid --hmp info migrate, 能看到迁移所设置的相关参数,和进度相关信息: virsh domjobinfo uuid命令, ...
- static 关键字解析(转)
static关键字解析 Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面 ...
- 配置Android应用开发环境
详解JRE.JDK.SDK.ADT请点击辨析ADK&JVM&JRE&JDK&ADT 一.安装JDK 开发 Android应用程序的时候,仅有Java运行环境(Java ...
- 【iOS开发】iOS CGRectGetMaxX/Y 使用
在iOS的界面布局中我们可以使用CGRectGetMaxX 这个方法来方便的获取当前控件的x坐标值+宽度的数值,这样便可以方便布局. 同理CGRectGetMaxY是获取y坐标值+控件高度的值,当然这 ...
- 软工实践Beta冲刺(1/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...
- SIP初步
http://blog.sina.com.cn/s/blog_6b10255301012db7.html 1.什么是SIP SIP(会话发起协议)属于IP应用层协议,用于在IP网上为用户提供会话应用. ...
- homework for Java
public class Dog { private String name; private String color; private int age; public Dog(String nam ...
- 我的python计划
一直想学习一种脚本语言.现在主流的脚本语言,比较先接触的是python 刚开始了解了一下python,感觉挺适合自己的感觉,学习了一段时间,之中感觉,就好象C++一样,把面向对象和面向过程编程结合了起 ...