gpt4 book ai didi

java - 如何从 Java/Scala 读取 Nutch 内容?

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

我正在使用 Nutch 来抓取一些网站(作为一个独立于其他一切运行的进程),同时我想使用 Java (Scala) 程序来分析使用 Jsoup 的网站的 HTML 数据。

我通过遵循 tutorial 让 Nutch 开始工作(没有脚本,只有执行单独的指令有效),我认为它将网站的 HTML 保存在 crawl/segments/<time>/content/part-00000 中目录。

问题是我不知道如何在 Java/Scala 程序中实际读取网站数据(URL 和 HTML)。我读了这个document ,但由于我从未使用过 Hadoop,所以觉得有点不知所措。

我尝试使示例代码适应我的环境,这就是我的结果(主要是通过 guesswprk):

  val reader = new MapFile.Reader(FileSystem.getLocal(new Configuration()), ".../apache-nutch-1.8/crawl/segments/20140711115438/content/part-00000", new Configuration())
var key = null
var value = null
reader.next(key, value) // test for a single value
println(key)
println(value)

但是,当我运行它时出现了这个异常:

Exception in thread "main" java.lang.NullPointerException
at org.apache.hadoop.io.SequenceFile$Reader.next(SequenceFile.java:1873)
at org.apache.hadoop.io.MapFile$Reader.next(MapFile.java:517)

我不确定如何使用 MapFile.Reader ,特别是我应该传递给它的构造函数参数。我应该传递哪些配置对象?那是正确的文件系统吗?这是我感兴趣的数据文件吗?

最佳答案

斯卡拉:

val conf = NutchConfiguration.create()
val fs = FileSystem.get(conf)
val file = new Path(".../part-00000/data")
val reader = new SequenceFile.Reader(fs, file, conf)

val webdata = Stream.continually {
val key = new Text()
val content = new Content()
reader.next(key, content)
(key, content)
}

println(webdata.head)

Java:

public class ContentReader {
public static void main(String[] args) throws IOException {
Configuration conf = NutchConfiguration.create();
Options opts = new Options();
GenericOptionsParser parser = new GenericOptionsParser(conf, opts, args);
String[] remainingArgs = parser.getRemainingArgs();
FileSystem fs = FileSystem.get(conf);
String segment = remainingArgs[0];
Path file = new Path(segment, Content.DIR_NAME + "/part-00000/data");
SequenceFile.Reader reader = new SequenceFile.Reader(fs, file, conf);
Text key = new Text();
Content content = new Content();
// Loop through sequence files
while (reader.next(key, content)) {
try {
System.out.write(content.getContent(), 0,
content.getContent().length);
} catch (Exception e) {
}
}
}
}

或者,您可以使用 org.apache.nutch.segment.SegmentReader ( example )。

关于java - 如何从 Java/Scala 读取 Nutch 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24699305/

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