将HDFS中的信息读入到Hbase
时间: 2024-02-25 09:51:32 浏览: 71
要将HDFS中的信息读入到HBase,可以使用HBase提供的工具类,如TableMapReduceUtil和HFileOutputFormat2等。下面是一个简单的示例:
1. 创建一个MapReduce程序,并在mapper中读取HDFS中的文件,将数据写入到HBase中:
```java
public class HDFS2HBaseMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
private final static byte[] CF = "cf".getBytes();
private final static byte[] ATTR1 = "attr1".getBytes();
private final static byte[] ATTR2 = "attr2".getBytes();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] fields = value.toString().split(",");
String rowKey = fields[0];
Put put = new Put(rowKey.getBytes());
put.addColumn(CF, ATTR1, fields[1].getBytes());
put.addColumn(CF, ATTR2, fields[2].getBytes());
context.write(new ImmutableBytesWritable(rowKey.getBytes()), put);
}
}
```
2. 在Driver中配置HBase表和HDFS输入路径,并设置HFileOutputFormat2的输出路径:
```java
public class HDFS2HBaseDriver {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set(TableOutputFormat.OUTPUT_TABLE, "mytable");
Job job = Job.getInstance(conf, "HDFS2HBase");
job.setJarByClass(HDFS2HBaseDriver.class);
job.setMapperClass(HDFS2HBaseMapper.class);
job.setOutputFormatClass(HFileOutputFormat2.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
FileInputFormat.setInputPaths(job, new Path("/input"));
FileOutputFormat.setOutputPath(job, new Path("/output"));
HFileOutputFormat2.configureIncrementalLoad(job, new HTable(conf, "mytable"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
```
3. 运行MapReduce程序,并将输出的HFile文件导入到HBase中:
```shell
$ hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /hbase/output mytable
```
以上就是将HDFS中的信息读入到HBase的基本步骤,具体可根据实际需求进行调整。
阅读全文
相关推荐


















