使用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的内部存储格式,所以导入效 ...
随机推荐
- JAVA集合面面观
List的常用实现:vector,ArrayList,linkedList. 总体关系如下(java8): vector和arraylist 两者底层都是采用数组的形式.但是有些许不同 // Arra ...
- [OpenCV]DMatch类和KeyPoints类:特征点匹配
DMatch struct CV_EXPORTS_W_SIMPLE DMatch { CV_WRAP DMatch() : queryIdx(-), trainIdx(-), imgIdx(-), d ...
- LINQ学习笔记——(2)Lambda表达式
最基本的 Lambda 表达式语法: (参数列表)=>{方法体} 说明: 参数列表中的参数类型可以是明确类型或者是推断类型 如果是推断类型,则参数的数据类型将由编译器根据上下文自动推断出 ...
- Visual Studio 2012安装包
点击下载
- LeetCode 82 ——删除排序链表中的重复元素 II
1. 题目 2. 解答 新建一个链表,并添加一个哨兵结点,从前向后开始遍历链表. 如果下一个结点的值和当前结点的值相等,则循环向后遍历直到找到一个和当前结点值不相等的结点: 反之,如果下一个结点的值和 ...
- Spring Cloud 自定义ConfigServer 解决敏感信息存储问题
公司需要将系统配置信息中的敏感信息独立存放. 现有系统采用Spring Cloud Config提供配置信息,其中敏感信息主要是Db配置,分解本次需求: (1)数据库配置信息分离(主要是Db信息). ...
- xampp开户,apache打开出现端口被占用提示
刚装上去的时候,可以打开xampp,但是重启的时候出现以后以下问题 13:49:02 [Apache] Error: Apache shutdown unexpectedly.13:49:0 ...
- MySQL初识3
随着对MySQL的熟识,今次总结一下MySQL数据库的删除.备份和还原操作 1.数据库的删除: a.删除数据库的命令:drop database dbname; b.删除数据库中的表: 单个表:dro ...
- Python不同进制之间的转换
不同的进制 二进制 0b101 以数字0和字母b打头的表示二进制数 如果出现大于等于2的数 会抛出SyntaxError异常 八进制 0711 以数字0打头的数字表示八进制数 如果出现大于 ...
- vue-cli项目里npm安装font-awesome
第一步:进入到项目目录里,运行 cnpm i font-awesome -s 第二步:在main.js里面 import 'font-awesome/css/font-awesome.min.css'