共计 3178 个字符,预计需要花费 8 分钟才能阅读完成。
将 HDFS 中的数据导入 HBase
package Hbase;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.Hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
public class BatchImport {
public static void main(String[] args) throws Exception {
final Configuration configuration = new Configuration();
// 设置 zookeeper
configuration.set(“hbase.zookeeper.quorum”, “hadoop1”);
// 设置 hbase 表名称
configuration.set(TableOutputFormat.OUTPUT_TABLE, “wlan_log”);
// 将该值改大,防止 hbase 超时退出
configuration.set(“dfs.socket.timeout”, “180000”);
final Job job = new Job(configuration, “HBaseBatchImport”);
job.setMapperClass(BatchImportMapper.class);
job.setReducerClass(BatchImportReducer.class);
// 设置 map 的输出,不设置 reduce 的输出类型
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
// 不再设置输出路径,而是设置输出格式类型
job.setOutputFormatClass(TableOutputFormat.class);
FileInputFormat.setInputPaths(job, “hdfs://hadoop1:9000/HTTP*”);
job.waitForCompletion(true);
}
static class BatchImportMapper extends Mapper<LongWritable, Text, LongWritable, Text>{
SimpleDateFormat dateformat1=new SimpleDateFormat(“yyyyMMddHHmmss”);
Text v2 = new Text();
protected void map(LongWritable key, Text value, Context context) throws java.io.IOException ,InterruptedException {
final String[] splited = value.toString().split(“\t”);
try {
final Date date = new Date(Long.parseLong(splited[0].trim()));
final String dateFormat = dateformat1.format(date);
String rowKey = splited[1]+”:”+dateFormat;
v2.set(rowKey+”\t”+value.toString());
context.write(key, v2);
} catch (NumberFormatException e) {
final Counter counter = context.getCounter(“BatchImport”, “ErrorFormat”);
counter.increment(1L);
System.out.println(“ 出错了 ”+splited[0]+” “+e.getMessage());
}
};
}
static class BatchImportReducer extends TableReducer<LongWritable, Text, NullWritable>{
protected void reduce(LongWritable key, java.lang.Iterable<Text> values, Context context) throws java.io.IOException ,InterruptedException {
for (Text text : values) {
final String[] splited = text.toString().split(“\t”);
final Put put = new Put(Bytes.toBytes(splited[0]));
put.add(Bytes.toBytes(“cf”), Bytes.toBytes(“date”), Bytes.toBytes(splited[1]));
// 省略其他字段,调用 put.add(….) 即可
context.write(NullWritable.get(), put);
}
};
}
}
HBase 的详细介绍 :请点这里
HBase 的下载地址 :请点这里
相关阅读 :
Hadoop+HBase 搭建云存储总结 PDF http://www.linuxidc.com/Linux/2013-05/83844.htm
HBase 结点之间时间不一致造成 regionserver 启动失败 http://www.linuxidc.com/Linux/2013-06/86655.htm
Hadoop+ZooKeeper+HBase 集群配置 http://www.linuxidc.com/Linux/2013-06/86347.htm
Hadoop 集群安装 &HBase 实验环境搭建 http://www.linuxidc.com/Linux/2013-04/83560.htm
基于 Hadoop 集群的 HBase 集群的配置 http://www.linuxidc.com/Linux/2013-03/80815.htm‘
Hadoop 安装部署笔记之 -HBase 完全分布模式安装 http://www.linuxidc.com/Linux/2012-12/76947.htm
单机版搭建 HBase 环境图文教程详解 http://www.linuxidc.com/Linux/2012-10/72959.htm