1、业务上在hdfs上生成了批量的数据,现在我们需要将这些数据导入到hbase中
首先我们需要了解buload的步骤:
a、生成HFile文件
b、将HFile文件load到hbase中
2、生成HFile的代码
package com.xiaofei.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
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.TextInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
import java.util.UUID;
/**
* @author xxxx
* @date 2021.07.13
*/
public class Hdfs2Hbase extends Configured implements Tool {
static Path pathHFile = new Path("hdfs://hadoop01:9000/hbase/userClickFile");
static TableName tableName=TableName.valueOf("userClick");
@Override
public int run(String[] args) throws Exception {
Job job=Job.getInstance(super.getConf(), Hdfs2Hbase.class.getSimpleName());
job.setJarByClass(Hdfs2Hbase.class);
job.setInputFormatClass(TextInputFormat.class);
TextInputFormat.addInputPath(job,new Path("hdfs://hadoop01:9000/dataclean/input/"));
job.setMapperClass(Hdfs2HbaseMapper.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
Connection conn = ConnectionFactory.createConnection(super.getConf());
Table table = conn.getTable(tableName);
HFileOutputFormat2.configureIncrementalLoad(
job,
table,
conn.getRegionLocator(tableName)
);
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://hadoop01:9000/dataclean/userClickFile");
FileSystem fileSystem = FileSystem.get(configuration);
if(fileSystem.exists(pathHFile)){
fileSystem.delete(pathHFile,true);
}
HFileOutputFormat2.setOutputPath(job,pathHFile);
return (job.waitForCompletion(true)?0:1);
}
public static void main(String[] args) throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
System.setProperty("HADOOP_USER_NAME","root");
int run = ToolRunner.run(conf, new Hdfs2Hbase(), args);
System.exit(run);
}
public static class Hdfs2HbaseMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] splits = value.toString().split("\t");
String uuid = UUID.randomUUID().toString();
if(splits.length==6) {
//不能使用userId做为rowkey,很多userid的操作时重复的
//String userId = splits[1];
byte[] rk = Bytes.toBytes(uuid);
ImmutableBytesWritable immutableBytesWritable = new ImmutableBytesWritable();
immutableBytesWritable.set(rk);
Put put = new Put(rk);
put.addColumn("info".getBytes(),"datatime".getBytes(),splits[0].getBytes());
put.addColumn("info".getBytes(),"userid".getBytes(),splits[1].getBytes());
put.addColumn("info".getBytes(),"searchkwd".getBytes(),splits[2].getBytes());
put.addColumn("info".getBytes(),"retorder".getBytes(),splits[3].getBytes());
put.addColumn("info".getBytes(),"cliorder".getBytes(),splits[4].getBytes());
put.addColumn("info".getBytes(),"cliurl".getBytes(),splits[5].getBytes());
context.write(immutableBytesWritable,put);
}
}
}
}
3、将HFile文件load到HBase中
package com.xiaofei.hbase;
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.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.tool.LoadIncrementalHFiles;
/**
* @author xxxx
* @date 2021.-7.13
*/
public class LoadData {
public static void main(String[] args) throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
System.setProperty("HADOOP_USER_NAME","root");
Connection conn = ConnectionFactory.createConnection(conf);
Table table=conn.getTable(TableName.valueOf("userClick"));
Admin admin = conn.getAdmin();
LoadIncrementalHFiles loadIncrementalHFiles = new LoadIncrementalHFiles(conf);
loadIncrementalHFiles.doBulkLoad(
new Path("hdfs://hadoop01:9000/hbase/userClickFile/"),
admin,
table,
conn.getRegionLocator(TableName.valueOf("userClick"))
);
}
}