2013年1月27日日曜日

NutchとMySQL

HBase, Cassandraと連携させたので、MySQLにも。

  • さくらVPS
  • CentOS 6.2
  • OpenJDK 1.6.0_24
  • Apache Nutch 2.1
  • MySQL 5.1.61

  1. MySQLのインストール
  2. ここは他サイトを参照してください。インストールされているものとして続けます。
  3. MySQLユーザ作成
  4. ここではユーザ「nutch」で作成。GRANT文のパスワード「****」は任意のものを
    # mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 138
    Server version: 5.1.61 Source distribution
    
    Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> 
    mysql> GRANT ALL PRIVILEGES ON nutch.* TO nutch@localhost IDENTIFIED BY '****';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> 
    mysql> quit
    Bye
    
  5. Nutchのソースパッケージ取得
  6. ※現時点で公式リンクにはソースのみ。
    apache-nutch-2.1-src.tar.gz

  7. 展開(同様に/opt)
  8. # cd /opt
    # ls
    apache-nutch-2.1-src.tar.gz
    # tar xvf apache-nutch-2.1-src.tar.gz
    # cd apache-nutch-2.1

  9. 設定(Gora関連)

  10. 以下それぞれ追記。

    conf/nutch-site.xml(<configuration>タグ内)
      <property>
        <name>storage.data.store.class</name>
        <value>org.apache.gora.sql.store.SqlStore</value>
      </property>

    conf/gora.properties
    gora.datastore.default=org.apache.gora.sql.store.SqlStore
    gora.sqlstore.jdbc.driver=com.mysql.jdbc.Driver
    gora.sqlstore.jdbc.url=jdbc:mysql://localhost:3306/nutch?createDatabaseIfNotExist=true
    gora.sqlstore.jdbc.user=<作成したユーザ名(ここではnutch)>
    gora.sqlstore.jdbc.password=<設定したパスワード>

    ivy/ivy.xml
    <dependency org="mysql" name="mysql-connector-java" rev="5.1.18" conf="*->default"/>

  11. コンパイル

  12. antでコンパイル。

    # ant
    <省略>

  13. 設定(Nutch関連)

  14. confディレクトリにあるnutch-default.xmlの「http.agent.name」プロパティの値に適当な文字列を記述する。
    conf/nutch-default.xml
    <property>
      <name>http.agent.name</name>
      <value>My Nutch Spider</value>
    </property>
    

    クローリングの始点となるサイトを記述したテキストファイルを任意のディレクトリに作成。
    以下の例では、インストールディレクトリ配下に作成した「urls」ディレクトリに作成。
    例)
    # mkdir urls
    urls/seed.txt
    http://nutch.apache.org/
    正規表現でクローリング対象サイトを指定する。
    ファイル「conf/regex-urlfilter.txt」を以下の通り、最下行コメントアウト+追記する。
    conf/regex-urlfilter.txt
    #+.
    +^http://([a-z0-9]*\.)*nutch.apache.org

  15. クローリングのためのInject
  16. # ./runtime/local/bin/nutch inject urls/
    InjectorJob: starting
    InjectorJob: urlDir: urls
    2013/01/27 12:56:49 org.apache.gora.sql.store.SqlStore createSchema
    情報: creating schema: webpage
    InjectorJob: org.apache.gora.util.GoraException: java.io.IOException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column length too big for column 'text' (max = 21845); use BLOB or TEXT instead
     at org.apache.gora.store.DataStoreFactory.createDataStore(DataStoreFactory.java:167)
     at org.apache.gora.store.DataStoreFactory.createDataStore(DataStoreFactory.java:135)
     at org.apache.nutch.storage.StorageUtils.createWebStore(StorageUtils.java:75)
     at org.apache.nutch.crawl.InjectorJob.run(InjectorJob.java:214)
     at org.apache.nutch.crawl.InjectorJob.inject(InjectorJob.java:228)
     at org.apache.nutch.crawl.InjectorJob.run(InjectorJob.java:248)
     at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:65)
     at org.apache.nutch.crawl.InjectorJob.main(InjectorJob.java:258)
    Caused by: java.io.IOException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column length too big for column 'text' (max = 21845); use BLOB or TEXT instead
     at org.apache.gora.sql.store.SqlStore.createSchema(SqlStore.java:226)
     at org.apache.gora.sql.store.SqlStore.initialize(SqlStore.java:172)
     at org.apache.gora.store.DataStoreFactory.initializeDataStore(DataStoreFactory.java:102)
     at org.apache.gora.store.DataStoreFactory.createDataStore(DataStoreFactory.java:161)
     ... 7 more
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column length too big for column 'text' (max = 21845); use BLOB or TEXT instead
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
     at com.mysql.jdbc.Util.getInstance(Util.java:386)
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
     at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
     at org.apache.gora.sql.store.SqlStore.createSchema(SqlStore.java:224)
     ... 10 more
    
    
    エラーがでました。textカラム(Webの本文が格納される)のサイズが少ないと、型をBLOBかTEXTにせよと。
    試行錯誤しましたが、やり方がわからずコミュニティのチケットなどを調べていると、
    https://issues.apache.org/jira/browse/NUTCH-1497
    https://issues.apache.org/jira/browse/NUTCH-1473
    ここにたどり着きました。
    http://nlp.solutions.asia/?p=180
    ちなみに、2.2で修正される予定のようですね。
  17. 再設定
  18. データベース/スキーマ作成
    mysql> drop database nutch;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> CREATE DATABASE nutch DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use nutch;
    Database changed
    
    mysql> CREATE TABLE `webpage` (
        -> `id` varchar(255) 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) CHARACTER SET latin1 DEFAULT NULL,
        -> `baseUrl` varchar(767) DEFAULT NULL,
        -> `content` longblob,
        -> `title` varchar(2048) DEFAULT NULL,
        -> `reprUrl` varchar(767) 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`)
        -> ) ENGINE=InnoDB
        -> ROW_FORMAT=COMPRESSED
        -> DEFAULT CHARSET=utf8;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show tables;
    +-----------------+
    | Tables_in_nutch |
    +-----------------+
    | webpage         |
    +-----------------+
    1 row in set (0.00 sec)
    
    
    gora-sql-mapping.xmlのidカラムの長さを512から767に変更する。
    gora-sql-mapping.xml
      
    
  19. クローリング
  20. とりあえず1巡だけやってみます。
    ./runtime/local/bin/nutch crawl urls/ -depth 1 -topN 1
    
    MySQLで確認。(ずれていてみにくいですが。)
    mysql> select id, score, fetchTime  from webpage;
    +----------------------------------------------+-----------+---------------+
    | id                                           | score     | fetchTime     |
    +----------------------------------------------+-----------+---------------+
    | org.apache.nutch:http/                       |   1.05882 | 1361854115396 |
    | org.apache.nutch:http/about.html             | 0.0588235 | 1359262135084 |
    | org.apache.nutch:http/apidocs-1.6/index.html | 0.0588235 | 1359262135086 |
    | org.apache.nutch:http/apidocs-2.1/index.html | 0.0588235 | 1359262135086 |
    | org.apache.nutch:http/bot.html               | 0.0588235 | 1359262135087 |
    | org.apache.nutch:http/credits.html           | 0.0588235 | 1359262135088 |
    | org.apache.nutch:http/faq.html               | 0.0588235 | 1359262135089 |
    | org.apache.nutch:http/index.html             | 0.0588235 | 1359262135090 |
    | org.apache.nutch:http/index.pdf              | 0.0588235 | 1359262135092 |
    | org.apache.nutch:http/issue_tracking.html    | 0.0588235 | 1359262135093 |
    | org.apache.nutch:http/mailing_lists.html     | 0.0588235 | 1359262135094 |
    | org.apache.nutch:http/nightly.html           | 0.0588235 | 1359262135095 |
    | org.apache.nutch:http/old_downloads.html     | 0.0588235 | 1359262135095 |
    | org.apache.nutch:http/sonar.html             | 0.0588235 | 1359262135096 |
    | org.apache.nutch:http/tutorial.html          | 0.0588235 | 1359262135097 |
    | org.apache.nutch:http/version_control.html   | 0.0588235 | 1359262135099 |
    | org.apache.nutch:http/wiki.html              | 0.0588235 | 1359262135100 |
    +----------------------------------------------+-----------+---------------+
    17 rows in set (0.00 sec)
    

おわり

0 件のコメント:

コメントを投稿