读取Nutch抓取文件中的Segments中Parse_Text的内容
2013-04-09 20:58
423 查看
有时侯非常需要想知道nutch抓取网页后对html解析情况,最常用的是在终端使用dump或者readseg命令,但是这样做的话也很是不方便。在nutch中org.apache.nutch.searcher包中NutchBean类中main找到了一点头绪,main方法中的代码如下:
说一下这里的HitDetails类,里面主要有 private String[] fields; private String[] values;两个成员变量,两个数组的长度相同,fields中存储变量,values中存储的是变量对应的值,功能类似hashmap的键值对,这里的details存储的有segmnet名称(以时间命名的文件夹),以及想找的那个文档的url,用来定位网页位置。将上面的代码稍加 修改就勉强的可以遍历segment中的parse_text
public static void main(String[] args) throws IOException{
FileOutputStream fos=null;
final NutchBean bean = new NutchBean(conf);
FSDirectory dir=FSDirectory.open(new File("/home/hadoop/output/index"));
IndexSearcher searcher=new IndexSearcher(dir);
int num=searcher.maxDoc();
int count=1;
for(int i=0;i<num;i++){
Hit h=new Hit(0,i+"");
HitDetails details=bean.getDetails(h);
ParseText text=bean.getParseText(details);
String temp=details.toString();
temp=temp.substring(temp.indexOf("/")+1);
File file=new File("/home/hadoop/temp/output/"+count++);//只是一个输出文件
if(!file.exists()) file.createNewFile();
fos=new FileOutputStream(file);
fos.write((temp+"\r\n"+text.getText()+"\r\n").getBytes());
}
fos.close();
System.out.println("Done"+count+" "+num);
return;
}上面的Hit类的构造方法中的两个参数为int 类型的indexNo和String UniqeKey,indexNo这个值目前还不知道有什么用,可能我建的索引的量还没有达到使它的值变为1,第二个参数是文档号,一般从0开始往上数。通过构造Hit来得到HitDetails,这样就可以通过NutchBean取得segment中的值;
public static void main(String[] args) throws Exception { final String usage = "NutchBean query [<searcher.dir>]"; if (args.length == 0) { System.err.println(usage); System.exit(-1); } // String[] str={"中国","/home/hadoop/output"}; // args=str; final Configuration conf = NutchConfiguration.create(); if (args.length > 1) { conf.set("searcher.dir", args[1]); } final NutchBean bean = new NutchBean(conf); try { final Query query = Query.parse(args[0], conf); query.getParams().setMaxHitsPerDup(0); final Hits hits = bean.search(query); System.out.println("Total hits: " + hits.getTotal()); final int length = (int)Math.min(hits.getLength(), 10); final Hit[] show = hits.getHits(0, length); final HitDetails[] details = bean.getDetails(show); final Summary[] summaries = bean.getSummary(details, query); for (int i = 0; i < hits.getLength(); i++) { System.out.println(" " + i + " " + details[i] + "\n" + summaries[i]); } } catch (Throwable t) { LOG.error("Exception occured while executing search: " + t, t); System.exit(1); } System.exit(0); }这段代代码需要查传入两个参数:查询项和索引的路径,输出结果是显示查询结果的摘要。
说一下这里的HitDetails类,里面主要有 private String[] fields; private String[] values;两个成员变量,两个数组的长度相同,fields中存储变量,values中存储的是变量对应的值,功能类似hashmap的键值对,这里的details存储的有segmnet名称(以时间命名的文件夹),以及想找的那个文档的url,用来定位网页位置。将上面的代码稍加 修改就勉强的可以遍历segment中的parse_text
public static void main(String[] args) throws IOException{
FileOutputStream fos=null;
final NutchBean bean = new NutchBean(conf);
FSDirectory dir=FSDirectory.open(new File("/home/hadoop/output/index"));
IndexSearcher searcher=new IndexSearcher(dir);
int num=searcher.maxDoc();
int count=1;
for(int i=0;i<num;i++){
Hit h=new Hit(0,i+"");
HitDetails details=bean.getDetails(h);
ParseText text=bean.getParseText(details);
String temp=details.toString();
temp=temp.substring(temp.indexOf("/")+1);
File file=new File("/home/hadoop/temp/output/"+count++);//只是一个输出文件
if(!file.exists()) file.createNewFile();
fos=new FileOutputStream(file);
fos.write((temp+"\r\n"+text.getText()+"\r\n").getBytes());
}
fos.close();
System.out.println("Done"+count+" "+num);
return;
}上面的Hit类的构造方法中的两个参数为int 类型的indexNo和String UniqeKey,indexNo这个值目前还不知道有什么用,可能我建的索引的量还没有达到使它的值变为1,第二个参数是文档号,一般从0开始往上数。通过构造Hit来得到HitDetails,这样就可以通过NutchBean取得segment中的值;
相关文章推荐
- QTextStream读取文件内容
- 任何的File.ReadAllText()和使用StreamReader读取文件内容之间的差异?
- android 读取sd卡文件的内容用TextView显示
- QTextStream 读取文件内容
- Android 读取SD卡中的text文件内容
- 利用FileReader.readAsText()读取文件内容并保存到服务器
- Hadoop读取sequencefile和textfile文件内容
- QTextStream 读取文件内容
- 编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
- mahout读取nutch抓取数据后的文件
- 如何从文件系统中读取文件内容
- php读取文件内容至字符串中,同时去除换行、空行、行首行尾空格(原创自Zjmainstay)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- node.js抓取并分析网页内容有无特殊内容的js文件
- 读取并显示 CSV 文件的整个内容
- shell读取文件中的内容
- C# StreamWriter写入一个ini文件到本地,StreamReader读取保存在本地的ini文件内容
- 关于如何使用properties读取配置文件内容问题
- file操作(7)--------------- BufferedReader读取文件内容
- c++ 一次读取文件全部内容