TagCloud

Monday, October 22, 2012

Nutch 2.1 with MySQL


Update : 2013.08.19
- 최신 Gora 에서는 Backend Storage로서 mysql 및 HSQLDB 을 더이상 지원하지 않습니다. 따라서, Nutch 2.2.x 이상부터는 패키징된 Gora 에서 더이상 Mysql 등을 지원하지 않으므로 참고 바랍니다. ( MySQL 사용이 필요할 경우 Nutch 2.1 로 다운그레이드 해야 합니다. )

Nutch 2.X 부터  Storage Abstraction (Apache Gora 를 이용한) 이 적용됨에 따라, 1.x 대에서 사용하던 webdb만이 아닌, MySQL, HBase, Cassandra, Accumulo 등이 사용 가능합니다.

따라서 스토리지 추상화가 이루어짐으로서 원하는 스토리지의 선택이 가능하고 따라서 Pre/Post Handling  개발에 있어서의 유용해졌다 라고 판단됩니다.


이하 내용은  http://nlp.solutions.asia/?p=180 를 참고하여 Nutch 2.1 에서 MySQL 를 이용하여 크롤링을 하는 예제를 진행합니다.


1. MySQL 에서 nutch db 생성 후 아래와 같이 테이블 생성


CREATE DATABASE nutch DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

use nutch;



CREATE TABLE `webpage` (
`id` varchar(767) NOT NULL,
`headers` blob,
`text` mediumtext DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`markers` blob,
`parseStatus` blob,
`modifiedTime` bigint(20) DEFAULT NULL,
`score` float DEFAULT NULL,
`typ` varchar(32) DEFAULT NULL,
`baseUrl` varchar(512) DEFAULT NULL,
`content` mediumblob,
`title` varchar(2048) DEFAULT NULL,
`reprUrl` varchar(512) DEFAULT NULL,
`fetchInterval` int(11) DEFAULT NULL,
`prevFetchTime` bigint(20) DEFAULT NULL,
`inlinks` mediumblob,
`prevSignature` blob,
`outlinks` mediumblob,
`fetchTime` bigint(20) DEFAULT NULL,
`retriesSinceFetch` int(11) DEFAULT NULL,
`protocolStatus` blob,
`signature` blob,
`metadata` blob,
PRIMARY KEY (`id`)
) ROW_FORMAT = Dynamic ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `host` (
  `id` varchar(767) NOT NULL DEFAULT '',
  `inlinks` longblob,
  `outlinks` longblob,
  `metadata` longblob,
  PRIMARY KEY (`id`)
) ROW_FORMAT = Dynamic ENGINE=InnoDB DEFAULT CHARSET=utf8


참고 : Unicode 관련 오류 발생시, 아래와 같이 서버의 설정을 변경 ( /etc/my.cnf )
- show variables like 'c%';















참고 : 원문에는 latin 으로 정의하였지만 테스트 결과 유니코드 관련 INSERT 시 오류가 발견됨.  http://myallforyou.blogspot.kr/2012/10/mysql-innodblargeprefix.html 를 참고하여 innodb 의 설정(키 컬럼 길이제한)을 변경하여 utf8 타입으로 생성함

2. Nutch 설치 경로/ivy/ivy.xml 에서 아래 주석부분 해제


{APACHE_NUTCH_HOME}/ivy/ivy.xml

<!– Uncomment this to use MySQL as database with SQL as Gora store. –>
<dependency org=”mysql” name=”mysql-connector-java” rev=”5.1.18″ conf=”*->default”/>




3. ${APACHE_NUTCH_HOME}/conf/gora.properties 에서 아래와 같이 MySQL properties 를 설정

###############################
# MySQL properties            #
###############################
gora.sqlstore.jdbc.driver=com.mysql.jdbc.Driver
gora.sqlstore.jdbc.url=jdbc:mysql://localhost:3306/nutch?createDatabaseIfNotExist=true
gora.sqlstore.jdbc.user=xxxxx
gora.sqlstore.jdbc.password=xxxxx

4. ${APACHE_NUTCH_HOME}/conf/gora-sql-mapping.xml 에서 아래와 같이 primarykey 의 길이를 table 의 값과 동일하게 설정
<primarykey column=”id” length=”768″/>



5. ${APACHE_NUTCH_HOME}/conf/nutch-site.xml 에서 아래와 같이 설정 수정

<property>
<name>http.agent.name</name>
<value>Your Nutch Spider</value>
</property>

<property>
<name>http.accept.language</name>
<value>ko-kr,ja-jp,en-us,en-gb,en;q=0.7,*;q=0.3</value>
<description>Value of the “Accept-Language” request header field.
This allows selecting non-English language as default one to retrieve.
It is a useful setting for search engines build for certain national group.
</description>
</property>

<property>
<name>parser.character.encoding.default</name>
<value>utf-8</value>
<description>The character encoding to fall back to when no other information
is available</description>
</property>

<property>
<name>storage.data.store.class</name>
<value>org.apache.gora.sql.store.SqlStore</value>
<description>The Gora DataStore class for storing and retrieving data.
Currently the following stores are available: ….
</description>
</property>


6. ant runtime 실행

7. 빌드가 끝나면 ${APACHE_NUTCH_HOME}\runtime 경로에 빌드가 완료됨

8. ${APACHE_NUTCH_HOME}\runtime\local\ 경로로 이동 후,

- cd ${APACHE_NUTCH_HOME}/runtime/local
- mkdir -p urls
- echo 'http://nutch.apache.org/' > urls/seed.txt
- bin/nutch crawl urls -depth 3 -topN 5


9. MySQL 접속 후 크롤링 데이터 확인









주) 컬럼 인코딩 문제로 [ java.io.IOException: java.sql.BatchUpdateException: Incorrect string value: '\xEC\x95\x84\xEC\x9D\xB4...' for column 'id' at row 1 ] 와 같이 오류가 발생할 수 있다.
mysql 의 my.conf 에서 [mysqld] 하위에 아래 내용 추가

character-set-server = utf8
collation-server=utf8_unicode_ci

10. solr 4.0 설치 후, conf 경로에 다음 url 의 schema.xml 복사
wget http://nlp.solutions.asia/wp-content/uploads/2012/08/schema.xml

11. java -jar start.jar 명령으로 solr 실행

12. nutch 에서 다음명령으로 인덱싱 실행
- nutch -solrindex http://127.0.0.1:8983/solr/ -reindex

13. solr 접속 후 확인
- http://vm:8983/solr/#/collection1/query















참고 : http://nlp.solutions.asia/?p=180