通過 HBase 的相關 JavaAPI,實現伴隨 HBase 操作的 MapReduce 過程,比如使用 MapReduce 將數據從本地文件系統導入到 HBase 的表中,比如我們從 HBase 中讀取一些原始數據後使用 MapReduce 做數據分析。
1.修改hadoop配置
hadoop版本2.9.2
hbase版本:2.0.3
配置Hadoop啓動的時候加載Hbase相關的jar包
修改hadoop的配置文件hadoop-env.sh
添加環境變量配置後重啓Hadoop
export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/usr/local/hbase-2.0.3/lib/*
2.Java工程開發
添加maven依賴
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-mapreduce</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
自定義MapReducer
package hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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.FileInputFormat;
import java.io.IOException;
/**
* @describe: 一自定義mr將Hadoop hdfs中的數據導入到 Hbase
*/
public class ReadHdfsToHbase {
//列族
public static final String CF = "info1";
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop1:2181");
conf.set("hbase.rootdir", "hdfs://hadoop1:9000/HBase");
conf.set(TableOutputFormat.OUTPUT_TABLE, args[1]);
Job job = Job.getInstance(conf, ReadHdfsToHbase.class.getSimpleName());
TableMapReduceUtil.addDependencyJars(job);
job.setJarByClass(ReadHdfsToHbase.class);
job.setMapperClass(ReadHdfsToHbaseMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setReducerClass(ReadHdfsToHbaseReducer.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
job.setOutputFormatClass(TableOutputFormat.class);
job.waitForCompletion(true);
}
public static class ReadHdfsToHbaseMapper extends Mapper<LongWritable, Text, Text, Text> {
private final Text outKey = new Text();
private final Text outValue = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] splits = value.toString().split("\t");
outKey.set(splits[0]);
outValue.set(splits[1]+"\t"+splits[2]+"\t"+splits[3]+"\t"+splits[4]);
context.write(outKey, outValue);
}
}
public static class ReadHdfsToHbaseReducer extends TableReducer<Text, Text, NullWritable> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
super.reduce(key, values, context);
Put put = new Put(key.getBytes());
for (Text text : values) {
String[] splis = text.toString().split("\t");
if (splis[0] != null && !"NULL".equals(splis[0])) {
put.addColumn(CF.getBytes(), "name".getBytes(), splis[0].getBytes());
}
if (splis[1] != null && !"NULL".equals(splis[1])) {
put.addColumn(CF.getBytes(), "age".getBytes(), splis[1].getBytes());
}
if (splis[2] != null && !"NULL".equals(splis[2])) {
put.addColumn(CF.getBytes(), "gender".getBytes(), splis[2].getBytes());
}
if (splis[3] != null && !"NULL".equals(splis[3])) {
put.addColumn(CF.getBytes(), "birthday".getBytes(), splis[3].getBytes());
}
}
context.write(NullWritable.get(), put);
}
}
}
3.測試
1.將自定義的MR打Jar包,並將jar包上傳到hadoop服務器上
2.在Hbase創建表 create 'stu','info1'
3運行MR
hadoop jar ReadHdfsToHbase.jar hadoop.ReadHdfsToHbase /stu.txt stu
4Hbase 查看數據
scan ‘stu’
數據文件:stu.txt
1 zhangsan 10 male NULL
2 lisi NULL NULL NULL
3 wangwu NULL NULL NULL
4 zhaoliu NULL NULL 1993