2016-11-23 8 views
2

Nutchを使用して一部のWebサイトをクロールしていますが、正確には私はthis siteをクロールしています。readsegコマンドなしでNutchセグメントを読み取る方法

私はthese five segmentsを見つけました(見つかったすべての文書(約10,000文書))。今度は、の内容をなしでreadsegコマンドを使用して処理します。これは、セグメントをプレーンテキストにダンプしないことです。

このため、各セグメントのサブディレクトリcontentのみが私にとって役に立ちます(タグとドキュメントの内容)。

私はcontentディレクトリの中にさらに2つのコンテナがあることを認識しました:dataindex。しかし、私はそれらの説明を見つけられておらず、内部でコンテンツを処理するためにそれらをどのように読むことができますか?私もこの質問にpointersを見つけましたが、私はまだアルゴリズムの考え方を理解していません。

コンテンツはNutchセグメントにどのように格納されていますか?どのように読むことができますか?私は、短い例が与えられたい場合(しかし、必要ではない)、コレクションのウェブサイトとセグメントを与えました。

答えて

1

コンテンツには何が必要ですか?たとえば、カスタムのIndexWriterを記述することができます。インデックス作成の段階で呼び出され、コンテンツへのアクセス権が与えられます。あるいは、 'dump'コマンド(org.apache.nutch.tools.FileDumper)を見て、コードを修正してください。

BTW 'Hadoop the Definitive Guide' by Tom Whiteは、Nutchのデータ構造について素敵な章を持っています。

NLPや分類などのページの処理をさらに進めたい場合は、NutchセグメントをHDFS上の「ニュートラル」データ構造に変換し、さまざまなツールで処理することができます。

+0

FileDumperの使用と修正が機能しました! –

0

@JulienNiocheの回答によると、これは私の実装です。

// file is the root directory of the segments. 
private static void indexSegments(File file) 
     throws IOException, IllegalAccessException, InstantiationException { 
    // Do not try to index files that cannot be read. 
    if (file.canRead() & file.isDirectory()) { 
     // List with all the segments. 
     File[] segmentDirs = file.listFiles(); 
     if (segmentDirs == null) { 
      System.err.println("No segment directories found in '" + 
           file.getAbsolutePath() + "'"); 
      return; 
     } 
     Configuration conf = NutchConfiguration.create(); 
     FileSystem fs = FileSystem.get(conf); 
     // Index all the segments. 
     for (File segment : segmentDirs) { 
      /* Only the content of the documents managed in 
      * the segment is useful for the system. */ 
      String segmentData = segment.getAbsolutePath() + "/" + 
        Content.DIR_NAME + "/part-00000/data"; 
      if (!new File(segmentData).exists()) { 
       System.out.println("Skipping segment: '" + segment.getName() + 
            "': no data directory present."); 
       continue; 
      } 
      SequenceFile.Reader reader = 
        new SequenceFile.Reader(fs, new Path(segmentData), conf); 
      Writable key = (Writable) reader.getKeyClass().newInstance(); 
      // Index all the documents managed in the current segment. 
      while (reader.next(key)) { 
       Content content = new Content(); 
       reader.getCurrentValue(content); 
       String url = key.toString(); 
       String baseName = FilenameUtils.getBaseName(url); 
       String extension = FilenameUtils.getExtension(url); 
       // Skips the document if it's not a XML file. 
       String mimeType = new Tika().detect(content.getContent()); 
       if (mimeType == null | !mimeType.equals(MediaType.APPLICATION_XML.toString())) { 
        System.out.println("Skipping document: '" + baseName + 
             "': not a XML file."); 
        continue; 
       } 
       /* Content of the document. */ 
       ByteArrayInputStream bas = new ByteArrayInputStream(content.getContent()); 
       int n = bas.available(); 
       byte[] bytes = new byte[n]; 
       bas.read(bytes, 0, n); 
       bas.close(); 
       String docContent = new String(bytes, StandardCharsets.UTF_8); 
       // TODO: Do what you want with the content. 
      } 
     } 
    } 
} 
関連する問題