1.数据写入方式
hbase数据导入方式可分成两种,一种在在线通过put实时写入、一种为批量写入。批量写入可分成批量put写入、和mapreduce程序生成hfile并将hfile导入hbase两种方式。批量生成hfile的方式效率更高,批量导入时一般使用批量生成hfile的方式导入hbase。
2.mapreduce示例
使用mapreduce生成hfile的时候需要注意数据rowkey分布的均匀,否则会导致数据热点,从而导致程序运行缓慢。在程序中用户只需要定义根据输入文件生成hbase表的keyValue值的map程序即可。reduce程序可使用hbase中自带reduce排序、分区程序。示例如下:
import java.io.IOException;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.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.lib.input.TextInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class HFileGenerate extends Configured implements Tool {
static class Mapper extends
org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
private ImmutableBytesWritable outKey = new ImmutableBytesWritable();
@Override
public void setup(Context context) {
}
@Override
protected void map(LongWritable key, Text value,
org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context)
throws IOException, InterruptedException {
try {
String[] line = StringUtils.splitPreserveAllTokens(value.toString(),SEPATOR);
if(line == null || line.length != 2){
context.getCounter("Error", "LengthError").increment(1);
return;
}
String hKey = line[0];
// 需注意rowkey分布是否均匀
outKey.set(Bytes.toBytes(hKey));
KeyValue kv1 = new KeyValue(Bytes.toBytes(hKey), Bytes.toBytes("cf"), Bytes.toBytes("f1"),
System.currentTimeMillis(), Bytes.toBytes(line[1]));
context.write(outKey, kv1);
context.getCounter("Success", "LineCount").increment(1);
} catch (Exception ex) {
ex.printStackTrace();
context.getCounter("Error", "ErrorCount").increment(1);
}
}
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(HBaseConfiguration.create(),new HFileGenerate(), args);
System.exit(res);
}
public int run(String[] args) throws Exception {
if (args.length < 2) {
return -1;
}
String input = args[0];
String output = args[1];
Configuration conf = getConf();
Job job = new Job(conf, "HFileGenerate");
job.setJarByClass(HFileGenerate.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(KeyValue.class);
// job.setOutputKeyClass(ImmutableBytesWritable.class);
// job.setOutputValueClass(KeyValue.class);
job.setMapperClass(HFileGenerate.Mapper.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(HFileOutputFormat2.class);
TextInputFormat.setInputPaths(job, input);
HFileOutputFormat2.setOutputPath(job, new Path(output));
Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set("hbase.zookeeper.quorum", "127.0.0.1");
HBASE_CONFIG.set("zookeeper.znode.parent", "/hbase");
Configuration cfg = HBaseConfiguration.create(HBASE_CONFIG);
HTable htable = new HTable(cfg, "test");
// 设置hfile reduce task、reduce 数量、key partition分布
// 生成hfile的排序、合并去重在reduce中执行
HFileOutputFormat2.configureIncrementalLoad(job, htable);
System.exit(job.waitForCompletion(true) ? 0 : 1);
return 0;
}
}
3. 数据导入
当hfile生成之后,可以bulkload将hfile文件导入到hbase中。
hadoop jar hbase-server-1.1.3.jar completebulkload {hfile路径} {hbase表}
4.注意事项
- 导入数据的主要防止数据热点
- 使用mapreduce程序生成hfile的时候,尽量使用hbase自带的reduce程序。否则可能导致因为hfile中数据rowkey排序不正确,导致数据读取错误。
- 如果hbase在线服务集群与hadoop离线任务集群在同一个集群上时,当hadoop运行大任务时,会影响hbase的性能甚至导致hbase死亡(遇见过由于大任务导致hbase连接zookeeper超时,从而导致master死亡)。所以建议将hbase在线服务的集群与hadoop离线任务集群分开部署。导入时可以先通过在hadoop离线任务集群跑mapreduce生成hfile,再将hfile同步到hbase在线服务集群的hdfs文件上去,再进行bulkload导入。