hbase的bulk loading代码和执行方法

本文详细介绍了HBase的bulk loading过程,包括数据生成和加载的步骤。首先,创建分区表并设置classpath环境。接着,通过MapReduce生成HFile,要求输出的key-value类型为<ImmutableBytesWritable, KeyValue>或<ImmutableBytesWritable, Put>,使用HFileOutputFormat类,并配置incremental load。最后,执行数据加载并删除临时HFile。" 81192340,1098046,方正微run card中英文术语解析,"['嵌入式开发', '硬件系统', '微处理器', '系统诊断']
摘要由CSDN通过智能技术生成

bulk loading的优缺点这里就不再赘述,在本博客的其他文章已经进行过详细的分析:

 Hbase Bulk Loading与HBase API方式分析和对比


bulk loading的过程主要分为两部分,一部分为数据生成,一部分为数据加载。

我们先来看看执行bulk loading的执行过程,然后再分析代码。下面是一个执行bulk loading的完整shell脚本:

CLASSPATH=./bulkload.jar:/etc/yarn1/conf:/etc/hdfs1/conf:/etc/hbase/conf:/usr/lib/hadoop/lib/*:/usr/lib/hadoop/.//*:/usr/lib/hadoop-hdfs/./:/usr/lib/hadoop-hdfs/lib/*:/usr/lib/hadoop-hdfs/.//*:/usr/lib/hadoop-yarn/lib/*:/usr/lib/hadoop-yarn/.//*:/usr/lib/hadoop-mapreduce/lib/*:/usr/lib/hadoop-mapreduce/.//*:/contrib/capacity-scheduler/*.jar
TABLE_NAME=CZSJ_hbase
RECORD_COUNT_FILE=/tmp/data1
RECORDS_PER_REGION=/tmp/output

echo "Start bulk load at `date`"
start=`date +%s`

echo "create hbase table=========="
hbase shell << EOF
    disable 'CZSJ_hbase'
    drop 'CZSJ_hbase'
    create 'CZSJ_hbase', 'cf', {SPLITS_FILE => 'region_start_row_info'}
EOF

jars=`ls ./lib`
for jar in $jars
do
    CLASSPATH="$CLASSPATH:./lib/$jar"
done

CLASSPATH=/usr/lib/hadoop/conf:/usr/lib/hbase/conf:$CLASSPATH
java -cp $CLASSPATH com.learn.hbasebulkload.HFileGenerator $TABLE_NAME $RECORD_COUNT_FILE $RECORDS_PER_REGION
java -cp $CLASSPATH com.learn.hbasebulkload.HFileLoader $TABLE_NAME $RECORDS_PER_REGION
hdfs dfs -rmr $RECORDS_PER_REGI
以下是一个简单的HBase BulkLoad批量写入数据代码示例: ```java Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); // HBase ZooKeeper Quorum config.set("hbase.zookeeper.property.clientPort", "2181"); // HBase ZooKeeper Client端口 String tableName = "mytable"; Path inputPath = new Path("/path/to/input/file"); Path outputPath = new Path("/path/to/output/file"); // 创建HBaseHBaseAdmin admin = new HBaseAdmin(config); HTableDescriptor tableDesc = new HTableDescriptor(tableName); tableDesc.addFamily(new HColumnDescriptor("cf")); admin.createTable(tableDesc); // 配置BulkLoad BulkLoadHelper helper = new BulkLoadHelper(config); helper.setHTable(new HTable(config, tableName)); helper.setInputPath(inputPath); helper.setOutputPath(outputPath); helper.setBulkLoadDir(new Path("/path/to/bulkload/directory")); helper.setMapperClass(MyMapper.class); // 运行BulkLoad int result = helper.run(); // 关闭Admin和HTable admin.close(); helper.getHTable().close(); ``` 上述代码中,`MyMapper`是自定义的Mapper类,用于将输入文件中的数据转换为HBase表中的Put操作。以下是一个简单的`MyMapper`类示例: ```java public static class MyMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> { public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split(","); // 将第一个字段作为HBase行键 byte[] rowKey = Bytes.toBytes(fields[0]); ImmutableBytesWritable key = new ImmutableBytesWritable(rowKey); // 创建HBase Put操作 Put put = new Put(rowKey); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes(fields[1])); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col2"), Bytes.toBytes(fields[2])); context.write(key, put); } } ``` `MyMapper`类的作用是将输入文件中的每行数据转换为一个HBase表中的Put操作。在这个例子中,我们假设输入文件中的每行数据都包含三个字段,第一个字段作为HBase表中的行键,第二个和第三个字段作为HBase表中的列。因此,我们将第一个字段作为HBase Put操作的行键,将第二个和第三个字段作为HBase Put操作的列。最后,我们将每个Put操作的行键和操作本身作为输出传递给Hadoop MapReduce框架。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值