我是Hadoop和Java的新手,我觉得有一些明显的东西我只是缺席了。如果这意味着什么,我正在使用Hadoop 1.0.3。
我使用hadoop的目的是获取一堆文件并一次解析一个文件(而不是逐行)。每个文件将生成多个键值,但其他行的上下文很重要。键和值是多值/复合的,所以我已经为键实现了WritableCompare,为值实现了可写。因为每个文件的处理占用了一点CPU,我想保存映射器的输出,然后再运行多个reducer。
对于复合键,我跟着[http://stackoverflow.com/questions/12427090/hadoop-composite-key] [1]
问题是,输出只是Java对象引用而不是复合键和值。例:
LinkKeyWritable@bd2f9730 LinkValueWritable@8752408c
我不确定问题是否与根本不减少数据或
有关这是我的主要课程:
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课程:
公共类RawMap扩展了MapReduceBase实现 Mapper {
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;
}
}
LinkValueWritable:
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;
}
}
答案 0 :(得分:7)
我认为答案在于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。
答案 1 :(得分:2)
看起来你有一个像你想要的对象列表。如果你想要打印出一个人类可读的版本而不是一个丑陋的java引用,你需要在你的可写上实现toString()。