gpt4 book ai didi

hadoop - 自定义 WritableCompare 将对象引用显示为输出

转载 作者:可可西里 更新时间:2023-11-01 15:00:31 26 4
gpt4 key购买 nike

我是 Hadoop 和 Java 的新手,我觉得我缺少一些明显的东西。如果这意味着什么,我正在使用 Hadoop 1.0.3。

我使用 hadoop 的目标是获取一堆文件并一次解析一个文件(而不是逐行解析)。每个文件都会产生多个键值,但其他行的上下文很重要。键和值是多值/复合的,所以我为键实现了 WritableCompare,为值实现了 Writable。因为每个文件的处理都需要一点 CPU,所以我想保存映射器的输出,然后再运行多个 reducer。

对于复合键,我遵循了[http://stackoverflow.com/questions/12427090/hadoop-composite-key][1]

问题是,输出只是 Java 对象引用,而不是复合键和值。例子:LinkKeyWritable@bd2f9730 LinkValueWritable@8752408c

我不确定问题是否与根本不减少数据有关,或者

这是我的主要类(class):

public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(Parser.class);
conf.setJobName("raw_parser");

conf.setOutputKeyClass(LinkKeyWritable.class);
conf.setOutputValueClass(LinkValueWritable.class);

conf.setMapperClass(RawMap.class);
conf.setNumMapTasks(0);

conf.setInputFormat(PerFileInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

PerFileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf);
}

还有我的 Mapper 类:

公共(public)类 RawMap 扩展 MapReduceBase 实现 映射器 {

    public void map(NullWritable key, Text value,
OutputCollector<LinkKeyWritable, LinkValueWritable> output,
Reporter reporter) throws IOException {
String json = value.toString();
SerpyReader reader = new SerpyReader(json);
GoogleParser parser = new GoogleParser(reader);
for (String page : reader.getPages()) {
String content = reader.readPageContent(page);
parser.addPage(content);
}
for (Link link : parser.getLinks()) {
LinkKeyWritable linkKey = new LinkKeyWritable(link);
LinkValueWritable linkValue = new LinkValueWritable(link);
output.collect(linkKey, linkValue);
}
}
}

Link 基本上是在 LinkKeyWritable 和 LinkValueWritable 之间拆分的各种信息的结构

LinkKeyWritable:

public class LinkKeyWritable implements WritableComparable<LinkKeyWritable>{
protected Link link;

public LinkKeyWritable() {
super();
link = new Link();
}

public LinkKeyWritable(Link link) {
super();
this.link = link;
}

@Override
public void readFields(DataInput in) throws IOException {
link.batchDay = in.readLong();
link.source = in.readUTF();
link.domain = in.readUTF();
link.path = in.readUTF();
}

@Override
public void write(DataOutput out) throws IOException {
out.writeLong(link.batchDay);
out.writeUTF(link.source);
out.writeUTF(link.domain);
out.writeUTF(link.path);
}

@Override
public int compareTo(LinkKeyWritable o) {
return ComparisonChain.start().
compare(link.batchDay, o.link.batchDay).
compare(link.domain, o.link.domain).
compare(link.path, o.link.path).
result();
}

@Override
public int hashCode() {
return Objects.hashCode(link.batchDay, link.source, link.domain, link.path);
}

@Override
public boolean equals(final Object obj){
if(obj instanceof LinkKeyWritable) {
final LinkKeyWritable o = (LinkKeyWritable)obj;
return Objects.equal(link.batchDay, o.link.batchDay)
&& Objects.equal(link.source, o.link.source)
&& Objects.equal(link.domain, o.link.domain)
&& Objects.equal(link.path, o.link.path);
}
return false;
}
}

链接值可写:

public class LinkValueWritable implements Writable{
protected Link link;

public LinkValueWritable() {
link = new Link();
}

public LinkValueWritable(Link link) {
this.link = new Link();
this.link.type = link.type;
this.link.description = link.description;
}

@Override
public void readFields(DataInput in) throws IOException {
link.type = in.readUTF();
link.description = in.readUTF();
}

@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(link.type);
out.writeUTF(link.description);
}

@Override
public int hashCode() {
return Objects.hashCode(link.type, link.description);
}

@Override
public boolean equals(final Object obj){
if(obj instanceof LinkKeyWritable) {
final LinkKeyWritable o = (LinkKeyWritable)obj;
return Objects.equal(link.type, o.link.type)
&& Objects.equal(link.description, o.link.description);
}
return false;
}
}

最佳答案

我认为答案在于 TextOutputFormat 的实现中.具体来说,LineRecordWriter 的 writeObject 方法:

/**
* Write the object to the byte stream, handling Text as a special
* case.
* @param o the object to print
* @throws IOException if the write throws, we pass it on
*/
private void writeObject(Object o) throws IOException {
if (o instanceof Text) {
Text to = (Text) o;
out.write(to.getBytes(), 0, to.getLength());
} else {
out.write(o.toString().getBytes(utf8));
}
}

如您所见,如果您的键或值不是 Text 对象,它会调用其上的 toString 方法并将其写出。由于您在键和值中未实现 toString,因此它使用的是 Object 类的实现,它正在写出引用。

我会说您应该尝试编写适当的 toString 函数或使用不同的 OutputFormat。

关于hadoop - 自定义 WritableCompare 将对象引用显示为输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12937581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com