ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 리눅스(CentOS) 에서 nutch & solr 사용 예시
    프로그래밍/검색 2014. 5. 6. 23:01
    반응형

     

    이번 포스팅에서는 nutch 위키에 있는 예시를 따라가면서 간단하게 웹 크롤링을 해 보겠다.

     

     

    참조: http://wiki.apache.org/nutch/NutchTutorial#A3.3._Using_the_crawl_script

     

    1. apache-nutch-1.8/runtime/local/conf/nutch-site.xml을 아래와 같이 수정하여 crawling agent에 My Nutch Spider라는 이름을 설정했다.

     

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>


    <configuration>
     <property>
      <name>http.agent.name</name>
      <value>My Nutch Spider</value>
     </property>
    </configuration>


     

     

    2. apache-nutch-1.8/runtime/local 디렉토리에서 mkdir -p urls 로 urls 하위 디렉토리를 만든다.

     

    3. urls 폴더 아래에 seed.txt 파일을 생성하고 http://nutch.apache.org/ 을 seed.txt에 입력한다.

       seed.txt 는 앞으로 수집할 url을 입력하는 파일이다.

     

    4.  apache-nutch-1.8/runtime/local/conf/regex-urlfilter.txt 파일을 다음과 같이 변경한다.

         + : 수집할 domain 패턴을 regular expression으로 표현

         - : 수집 제외할 domain 패턴을 regular expression으로 표현 

        예제를 따라 nutch.apache.org 사이트의 모든 URL을 포함시켰다

     

    # accept anything else
    +.

     

    ->

     

    +^http://([a-z0-9]*\.)*nutch.apache.org/

     

     

    5. apache-nutch-1.8/runtime/local/bin/crawl urls/seed.txt TestCrawl -depth 3 -topN 5 명령어로 crawling 시작  

     

    참고로 crawl 사용법: crawl <seedDir> <crawlDir> <solrURL> <numberOfRounds>

     

    <crawlDir> 은 수집된 자료를 넣을 디렉토리이고

    solr 와 연동이 끝났으면 아래와 같이 sorlURL을 입력하여 인덱싱까지 한 번에 할 수 있다.

     

    bin/crawl urls/seed.txt TestCrawl http://localhost:8983/solr/ 2

    아직 solr 연동을 하지 않아서 일단 수집만 진행하였다.

     

     

     

    수집이 완료되면 crawl 디렉토리로 지정한 TestCrawl 디렉토리에 crawldb, linkdb, segements 디렉토리가 생성된다.

     

    이제 이 수집된 데이터들을 색인하고 검색하기 위해 solr 연동을 해보겠다.

     

    위 nutch 위키에서는 아래와 같이 solr 연동을 하라고 가이드 하고 있었다.

     

    • mv ${APACHE_SOLR_HOME}/example/solr/conf/schema.xml ${APACHE_SOLR_HOME}/example/solr/conf/schema.xml.org
    • cp ${NUTCH_RUNTIME_HOME}/conf/schema.xml ${APACHE_SOLR_HOME}/example/solr/conf/

    • vi ${APACHE_SOLR_HOME}/example/solr/conf/schema.xml
    • Copy exactly in 351 line: <field name="_version_" type="long" indexed="true" stored="true"/>

    • restart Solr with the command “java -jar start.jar” under ${APACHE_SOLR_HOME}/example

     

    그러나 내가 가진 버전에는 example/solr/conf/schema.xml 파일이 없었다 -_-?

    검색해보니 위는 solr 3버전을 위한 설명인 듯 하다. solr 4 버전은 아래와 같이 같이 수정하면 된다.

     

     

  • mv ${APACHE_SOLR_HOME}/example/solr/collection1/conf/schema.xml ${APACHE_SOLR_HOME}/example/solr/conf/schema.xml.org
  • cp ${NUTCH_RUNTIME_HOME}/conf/schema-solr4.xml ${APACHE_SOLR_HOME}/example/solr/collection1/conf/

  • mv ${APACHE_SOLR_HOME}/example/solr/collection1/conf/shema-solr4.xml ${APACHE_SOLR_HOME}/example/solr/collection1/conf/schema.xml

  • vi ${APACHE_SOLR_HOME}/example/collection1/solr/conf/schema.xml
  • Copy exactly in 351 line: <field name="_version_" type="long" indexed="true" stored="true"/>

  • restart Solr with the command “java -jar start.jar” under ${APACHE_SOLR_HOME}/example

 

정상적으로 solr가 재 시작되는지는 아래의 url에 접속해보면 알 수 있다.

 

http://localhost:8983/solr/#/ or http://domain or ip:8983/solr/#/

 

solr가 정상적으로 재시작되었으므 아까 저장해 둔 데이터를 색인해보자.

 

[root]# bin/nutch solrindex http://127.0.0.1:8983/solr/ TestCrawl/crawldb -linkdb TestCrawl/linkdb TestCrawl/segments/*
Indexer: starting at 2014-05-06 01:08:20
Indexer: deleting gone documents: false
Indexer: URL filtering: false
Indexer: URL normalizing: false
Active IndexWriters :
SOLRIndexWriter
        solr.server.url : URL of the SOLR instance (mandatory)
        solr.commit.size : buffer size when sending to SOLR (default 1000)
        solr.mapping.file : name of the mapping file for fields (default solrindex-mapping.xml)
        solr.auth : use authentication (default false)
        solr.auth.username : use authentication (default false)
        solr.auth : username for authentication
        solr.auth.password : password for authentication


Indexer: finished at 2014-05-06 01:09:10, elapsed: 00:00:50

 

정상적으로 색인이 끝났다. 참고로 색인된 데이터 확인은 solr 관리 페이지에서도 가능하다.

collection1의 query를 선택하여 전체 검색하면 nutch 사이트의 content, title, url등이 수집된 것을 확인할 수 있다.

 

 

 

 

728x90
반응형