您的位置:首页 > 运维架构

通过BulkLoad的方式快速导入海量数据到Hbase

2018-03-05 14:12 519 查看

摘要加载数据到HBase的方式有多种,通过HBase API导入或命令行导入或使用第三方(如sqoop)来导入或使用MR来批量导入(耗费磁盘I/O,容易在导入的过程使节点宕机),但是这些方式不是慢就是在导入的过程的占用Region资料导致效率低下,今天要讲的就是利用HBase在HDFS存储原理及MapReduce的特性来快速导入海量的数据 HBase数据在HDFS下是如何存储的?HBase中每张Table在根目录(/HBase)下用一个文件夹存储,Table名为文件夹名,在Table文件夹下每个Region同样用一个文件夹存储,每个Region文件夹下的每个列族也用文件夹存储,而每个列族下存储的就是一些HFile文件,HFile就是HBase数据在HFDS下存储格式,其整体目录结构如下:/hbase/<tablename>/<encoded-regionname>/<column-family>/<filename> HBase数据写路径

                                                                              (图来自Cloudera)在put数据时会先将数据的更新操作信息和数据信息写入WAL,在写入到WAL后,数据就会被放到MemStore中,当MemStore满后数据就会被flush到磁盘(即形成HFile文件),在这过程涉及到的flush,split,compaction等操作都容易造成节点不稳定,数据导入慢,耗费资源等问题,在海量数据的导入过程极大的消耗了系统性能,避免这些问题最好的方法就是使用BlukLoad的方式来加载数据到HBase中。原理利用HBase数据按照HFile格式存储在HDFS的原理,使用Mapreduce直接生成HFile格式文件后,RegionServers再将HFile文件移动到相应的Region目录下其流程如下图:

                                                                      (图来自Cloudera)导入过程1.使用MapReduce生成HFile文件GenerateHFile类
public class GenerateHFile extends Mapper<LongWritable,
Text, ImmutableBytesWritable, Put>{
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] items = line.split("\t");

String ROWKEY = items[1] + items[2] + items[3];
ImmutableBytesWritable rowkey = new ImmutableBytesWritable(ROWKEY.getBytes());
Put put = new Put(ROWKEY.getBytes());   //ROWKEY
put.addColumn("INFO".getBytes(), "URL".getBytes(), items[0].getBytes());
put.addColumn("INFO".getBytes(), "SP".getBytes(), items[1].getBy
4000
tes());  //出发点
put.addColumn("INFO".getBytes(), "EP".getBytes(), items[2].getBytes());  //目的地
put.addColumn("INFO".getBytes(), "ST".getBytes(), items[3].getBytes());   //出发时间
put.addColumn("INFO".getBytes(), "PRICE".getBytes(), Bytes.toBytes(Integer.valueOf(items[4])));  //价格
put.addColumn("INFO".getBytes(), "TRAFFIC".getBytes(), items[5].getBytes());//交通方式
put.addColumn("INFO".getBytes(), "HOTEL".getBytes(), items[6].getBytes()); //酒店

context.write(rowkey, put);
}
}

GenerateHFileMain类public class GenerateHFileMain {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
final String INPUT_PATH= "hdfs://master:9000/INFO/Input";
final String OUTPUT_PATH= "hdfs://master:9000/HFILE/Output";
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("TRAVEL"));
Job job=Job.getInstance(conf);
job.getConfiguration().set("mapred.jar", "/home/hadoop/TravelProject/out/artifacts/Travel/Travel.jar"); //预先将程序打包再将jar分发到集群上
job.setJarByClass(GenerateHFileMain.class);
job.setMapperClass(GenerateHFile.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
job.setOutputFormatClass(HFileOutputFormat2.class);
HFileOutputFormat2.configureIncrementalLoad(job,table,connection.getRegionLocator(TableName.valueOf("TRAVEL")))
FileInputFormat.addInputPath(job,new Path(INPUT_PATH));
FileOutputFormat.setOutputPath(job,new Path(OUTPUT_PATH));
System.exit(job.waitForCompletion(true)?0:1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hadoop Hdfs Hbase BulkLoad