- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在尝试运行 hadoop 0.20.205.0 MapReduce 作业(单线程,本地),它表现出各种奇怪和意外的行为。我终于明白为什么了。在我看来,这就像是 hadoop 中的一个错误,但也许有一些我不明白的地方。有人可以给我一些建议吗?我的 setMapOutputKeyClass 类实现了 Configurable。除非先调用 setConf,否则无法正确读取 readFields 方法(我相信这就是 Configurable 接口(interface)的重点)但是查看 WritableComparator 的代码,我发现当框架对它们进行排序时,它会实例化其内部关键对象:
70 key1 = newKey();
71 key2 = newKey();
并且 newKey() 使用空 Configuration 来构造键:
83 public WritableComparable newKey() {
84 return ReflectionUtils.newInstance(keyClass, null);
85 }
确实,当我在调试器中运行时,我发现在
91 key1.readFields(buffer);
key1里面的conf为null,所以没有调用setConf
这是 hadoop 中的错误,还是我应该使用 Configurable 之外的其他东西来配置 key ?如果这是一个错误,有人知道任何解决方法吗?
编辑:这是一个简短的(有点人为的)工作示例,由于这个原因而失败:
// example/WrapperKey.java
package example;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.ByteWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.ReflectionUtils;
/**
* This class wraps a WritableComparable class to add one extra possible value
* (namely null) to the range of values available for that class.
*/
public class WrapperKey<T extends WritableComparable> implements
WritableComparable<WrapperKey<T>>, Configurable {
private T myInstance;
private boolean isNull;
private Configuration conf;
@Override
public void setConf(Configuration conf) {
this.conf = conf;
Class<T> heldClass = (Class<T>) conf.getClass("example.held.class",
null, WritableComparable.class);
myInstance = ReflectionUtils.newInstance(heldClass, conf);
}
@Override
public Configuration getConf() {
return conf;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeBoolean(isNull);
if (!isNull)
myInstance.write(out);
}
@Override
public void readFields(DataInput in) throws IOException {
isNull = in.readBoolean();
if (!isNull)
myInstance.readFields(in);
}
@Override
public int compareTo(WrapperKey<T> o) {
if (isNull) {
if (o.isNull)
return 0;
else
return -1;
} else if (o.isNull)
return 1;
else
return myInstance.compareTo(o.myInstance);
}
public void clear() {
isNull = true;
}
public T get() {
return myInstance;
}
/**
* Should sort the KV pairs (5,0), (3,0), and (null,0) to [(null,0), (3,0), (5,0)], but instead fails
* with a NullPointerException because WritableComparator's internal keys
* are not properly configured
*/
public static void main(String[] args) throws IOException,
InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
conf.setClass("example.held.class", ByteWritable.class,
WritableComparable.class);
Path p = new Path("input");
Path startFile = new Path(p, "inputFile");
SequenceFile.Writer writer = new SequenceFile.Writer(
p.getFileSystem(conf), conf, startFile, WrapperKey.class,
ByteWritable.class);
WrapperKey<ByteWritable> key = new WrapperKey<ByteWritable>();
key.setConf(conf);
ByteWritable value = new ByteWritable((byte) 0);
key.get().set((byte) 5);
writer.append(key, value);
key.get().set((byte) 3);
writer.append(key, value);
key.clear();
writer.append(key, value);
writer.close();
Job j = new Job(conf, "Example job");
j.setInputFormatClass(SequenceFileInputFormat.class);
j.setOutputKeyClass(WrapperKey.class);
j.setOutputValueClass(ByteWritable.class);
j.setOutputFormatClass(SequenceFileOutputFormat.class);
FileInputFormat.setInputPaths(j, p);
FileOutputFormat.setOutputPath(j, new Path("output"));
boolean completed = j.waitForCompletion(true);
if (completed) {
System.out
.println("Successfully sorted byte-pairs by key (putting all null pairs first)");
} else {
throw new RuntimeException("Failed to sort");
}
}
}
最佳答案
WrapperKey 正在实现 Configurable 并实现 setConf。仅仅实现一个接口(interface)并不意味着其他一些类将调用它。 Hadoop 框架可能不会在键上调用 setConf 方法。
我不认为这是一个错误。我见过的所有类型都只实现了 WritableComparable 而不是 Configurable。不确定是否有解决方法,您可能必须在 key 中定义具体类型。
关于null - Hadoop 0.20.205.0 WritableComparator 不遵守可配置键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8584052/
刚开始探索mapreduce,遇到了有compareTo()方法的Writablecomparable接口(interface)和有compare()方法的Writablecomparator,当我们
compare() 和 compareTo() 如果我们谈论排序键,它们是同义的但我只想知道在高度配置机器的时代是否需要考虑关于何时使用 compare() 以及何时使用 compareTo()? 如
下面是我使用 WritableComparator 的代码片段,但它不起作用 import org.apache.hadoop.io.WritableComparable;import org.apa
我正在尝试实现自定义 WritableComparable,如图所示 In this link 当我尝试在我的映射器方法中初始化自定义可写比较类时出现错误。我已经在我的代码中显示了我的错误。textp
我是 Hadoop 和 Java 的新手,我觉得我缺少一些明显的东西。如果这意味着什么,我正在使用 Hadoop 1.0.3。 我使用 hadoop 的目标是获取一堆文件并一次解析一个文件(而不是逐行
我需要在 Job 中使用 setGroupingComparatorClass 方法,它需要一个类型为 WritableComparable 的参数。 我无法实现 WritableComparable
尝试使用 Maven(或使用我的 IDE IntelliJ)编译我的 Pig UDF 时出现以下错误: cannot access org.apache.hadoop.io.WritableCompa
我已经为我的 map 作业实现了 WritableComparable 并向它传递了三个值。 public class KeyCustom implementsw WritableComparable
谁能给我解释一下: 什么是Hadoop中的Writable和Writable Comparable接口(interface)? 这两者有什么区别? 请举例说明。 提前致谢 最佳答案 Writable
您能否解释一下Java中的RAW Type是什么,我可以在Internet上找到更有意义的东西。 例如: Hadoop WritableComparable RawType , 什么是 RawType
我一直在尝试运行 hadoop 0.20.205.0 MapReduce 作业(单线程,本地),它表现出各种奇怪和意外的行为。我终于明白为什么了。在我看来,这就像是 hadoop 中的一个错误,但也许
在输出从 mapper 传递到 reducer 之前,mapreduce 中如何进行排序。如果我的映射器输出键是 IntWritable 类型,它是否使用 IntWritable 类中定义的比较器或类
我是一名优秀的程序员,十分优秀!