Java hadoop map使用HDFS输入和HBASE输出减少作业
我是hadoop新手。 我有一个MapReduce作业,它应该从Hdfs获取一个输入,并将reducer的输出写入Hbase。我没有找到任何好的例子 下面是代码,运行此示例时出现的错误是map中的类型不匹配,应为ImmutableBytesWriteable Received IntWriteable。 映射器类Java hadoop map使用HDFS输入和HBASE输出减少作业,java,hadoop,mapreduce,hbase,hdfs,Java,Hadoop,Mapreduce,Hbase,Hdfs,我是hadoop新手。 我有一个MapReduce作业,它应该从Hdfs获取一个输入,并将reducer的输出写入Hbase。我没有找到任何好的例子 下面是代码,运行此示例时出现的错误是map中的类型不匹配,应为ImmutableBytesWriteable Received IntWriteable。 映射器类 public static class AddValueMapper extends Mapper < LongWritable, Text, ImmutableBytesW
public static class AddValueMapper extends Mapper < LongWritable,
Text, ImmutableBytesWritable, IntWritable > {
/* input <key, line number : value, full line>
* output <key, log key : value >*/
public void map(LongWritable key, Text value,
Context context)throws IOException,
InterruptedException {
byte[] key;
int value, pos = 0;
String line = value.toString();
String p1 , p2 = null;
pos = line.indexOf("=");
//Key part
p1 = line.substring(0, pos);
p1 = p1.trim();
key = Bytes.toBytes(p1);
//Value part
p2 = line.substring(pos +1);
p2 = p2.trim();
value = Integer.parseInt(p2);
context.write(new ImmutableBytesWritable(key),new IntWritable(value));
}
}
public static class AddValuesReducer extends TableReducer<
ImmutableBytesWritable, IntWritable, ImmutableBytesWritable> {
public void reduce(ImmutableBytesWritable key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
long total =0;
// Loop values
while(values.iterator().hasNext()){
total += values.iterator().next().get();
}
// Put to HBase
Put put = new Put(key.get());
put.add(Bytes.toBytes("data"), Bytes.toBytes("total"),
Bytes.toBytes(total));
Bytes.toInt(key.get()), total));
context.write(key, put);
}
}
公共静态类AddValueMapper扩展了Mapper{
/*输入
*输出*/
公共无效映射(可长写键、文本值、,
上下文)抛出IOException,
中断异常{
字节[]键;
int值,pos=0;
字符串行=value.toString();
字符串p1,p2=null;
pos=行索引(“=”);
//关键部分
p1=行子串(0,位置);
p1=p1.trim();
key=Bytes.toBytes(p1);
//价值部分
p2=行子串(位置+1);
p2=p2.trim();
value=Integer.parseInt(p2);
write(新的ImmutableBytesWritable(键),新的IntWritable(值));
}
}
减速器类
public static class AddValueMapper extends Mapper < LongWritable,
Text, ImmutableBytesWritable, IntWritable > {
/* input <key, line number : value, full line>
* output <key, log key : value >*/
public void map(LongWritable key, Text value,
Context context)throws IOException,
InterruptedException {
byte[] key;
int value, pos = 0;
String line = value.toString();
String p1 , p2 = null;
pos = line.indexOf("=");
//Key part
p1 = line.substring(0, pos);
p1 = p1.trim();
key = Bytes.toBytes(p1);
//Value part
p2 = line.substring(pos +1);
p2 = p2.trim();
value = Integer.parseInt(p2);
context.write(new ImmutableBytesWritable(key),new IntWritable(value));
}
}
public static class AddValuesReducer extends TableReducer<
ImmutableBytesWritable, IntWritable, ImmutableBytesWritable> {
public void reduce(ImmutableBytesWritable key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
long total =0;
// Loop values
while(values.iterator().hasNext()){
total += values.iterator().next().get();
}
// Put to HBase
Put put = new Put(key.get());
put.add(Bytes.toBytes("data"), Bytes.toBytes("total"),
Bytes.toBytes(total));
Bytes.toInt(key.get()), total));
context.write(key, put);
}
}
公共静态类AddValuesReducer扩展了TableReducer<
ImmutableBytesWritable、IntWritable、ImmutableBytesWritable>{
public void reduce(ImmutableBytesWritable键、Iterable值、,
上下文)抛出IOException、InterruptedException{
长总计=0;
//循环值
while(values.iterator().hasNext()){
total+=values.iterator().next().get();
}
//使用HBase
Put Put=新Put(key.get());
put.add(Bytes.toBytes(“数据”)、Bytes.toBytes(“总计”),
字节。toBytes(总计);
toInt(key.get()),总计);
context.write(key,put);
}
}
我有一个类似的工作,只与HDFS和工作很好
于2013年6月18日编辑。这所大学的项目两年前成功地完成了。对于作业配置(驱动程序部分),请检查正确答案。不确定HDFS版本工作的原因:通常必须设置作业的输入格式,而FileInputFormat是一个抽象类。也许你漏掉了几行?比如
job.setInputFormatClass(TextInputFormat.class);
将此更改为immutableBytesWritable
,intwritable
我不确定。希望它能起作用。这是解决您问题的代码
司机
映射器和还原器
class yourMapper扩展了Mapper{
//@overidemap()
}
类减速器
延伸
压片机
{
//@重写reduce()
}
在HBase中批量加载数据的最佳且最快的方法是使用
HFileOutputFormat
和CompliteBulkLoad
实用程序
您将发现一个示例代码:
希望这将是有用的:)感谢您的回答驱动程序部分有几个错误,现在已解决。Ans示例代码[HDFS_HABSE map Reduce][1][1]:感谢您的回答,代码帖子是正确的,项目在两年前完成。感谢您的回答,您的作业配置与解决我的问题的配置相同。当我有时间清理这个烂摊子时,我会编辑并格式化我的问题。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。
class yourMapper extends Mapper<LongWritable, Text, Text,IntWritable> {
//@overide map()
}
class yourReducer
extends
TableReducer<Text, IntWritable,
ImmutableBytesWritable>
{
//@override reduce()
}