12
返回列表 发新帖
楼主: jieforest

把数据导入CouchDB-java、Ruby、Erlang三种方法

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
11#
 楼主| 发表于 2012-10-19 14:05 | 只看该作者
Saving the records

Once we have the erlang structure ready to be saved into our CouchDB instance things get much easier. For this purpose I have used some code snippets from the ‘Hovercraft‘ library which looks like its not developed since 2010 but the code is easy to follow and I was able to reuse some functionality I needed.

An easy direct Erlang CouchDB library.

Use this to abstract CouchDB behind a simple Erlang function call. Currently supports the database and document APIs, with views on the way.

The ‘lightning’ test works with the latest CouchDB version and thats more than enough for my needs, if it wouldn’t work, it was a good starting point to play with the CouchDB from Erlang anyway.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
12#
 楼主| 发表于 2012-10-19 14:07 | 只看该作者
Saving our erlang structure is as easy as a single method call:
  1. %
  2. %% Closing tags and resetting counters..
  3. %
  4. processEndTag(<<"artist">>,Acc) ->
  5.   [_,_,_,_,ArtistData] = Acc,
  6.   save_doc(<<"erlang_music">>,ArtistData),
  7.   [];
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
13#
 楼主| 发表于 2012-10-19 14:07 | 只看该作者
Whole part for interacting with the CouchDB from erlang looks like the following: (taken mostly from Hovercraft sources):
  1. open_db(DbName) ->
  2.   couch_db:open(DbName, [?ADMIN_USER_CTX]).

  3. create_db(DbName) ->
  4.   create_db(DbName, []).

  5. create_db(DbName, Options) ->
  6.   case couch_server:create(DbName, Options) of
  7.     {ok, Db} ->
  8.       couch_db:close(Db),
  9.       {ok, created};
  10.     Error ->
  11.       {error, Error}
  12.   end.

  13. delete_db(DbName) ->
  14.   delete_db(DbName,  [?ADMIN_USER_CTX]).

  15. delete_db(DbName, Options) ->
  16.   case couch_server:delete(DbName, Options) of
  17.     ok ->
  18.       {ok, deleted};
  19.     Error ->
  20.       {error, Error}
  21.   end.

  22. save_doc(#db{}=Db, Doc) ->
  23.   CouchDoc = ejson_to_couch_doc(Doc),
  24.   {ok, Rev} = couch_db:update_doc(Db, CouchDoc, []),
  25.   {ok, {[{id, CouchDoc#doc.id}, {rev, couch_doc:rev_to_str(Rev)}]}};

  26. save_doc(DbName, Docs) ->
  27.   {ok, Db} = open_db(DbName),
  28.   save_doc(Db, Docs).

  29. ejson_to_couch_doc({DocProps}) ->
  30.   Doc = case proplists:get_value(<<"_id">>, DocProps) of
  31.     undefined ->
  32.       DocId = couch_uuids:new(),
  33.       {[{<<"_id">>, DocId}|DocProps]};
  34.     _DocId ->
  35.       {DocProps}
  36.   end,
  37.   couch_doc:from_json_obj(Doc).
复制代码
Full source code for the erlang script as well as ruby is available from GitHub, for Java version you can get it from ‘woodstoxex‘ example under the ‘couchdb’ branch.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
14#
 楼主| 发表于 2012-10-20 09:31 | 只看该作者
The Test

Assuming you have downloaded the test data from Jamendo website, and modified the sources appropriately you do the following:

Java – this is really straightforward, just execute the main method from the ‘Main’ class to get the job done.

Ruby –  run the script feeding it with the xml stream using cat command:
  1. cat /path_to_your_download_folder/dbdump_artistalbumtrack.0.290905586176.xml | ruby jamendo.rb
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
15#
 楼主| 发表于 2012-10-20 09:31 | 只看该作者
Our erlang script will be installed and used the same way a Hovercraft library was used, inside the CouchDB trunk, create a folder called ‘xmlimporter’ and place our erlang script in there (modifying the xml file location earlier).

Once you have a CouchDB build you can start it up in interactive mode with the following command:
  1. erlc xmlimporter/*erl && ERL_LIBS="xmlimporter" utils/run -i
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
16#
 楼主| 发表于 2012-10-20 09:32 | 只看该作者
Hit Enter when the CouchDB finishes launching and you should be inside the erlang console.

To start our script execute the following command:
  1. 1> xmlimporter:start().
复制代码
where (1>) is the erlang shell prompt.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
17#
 楼主| 发表于 2012-10-20 09:34 | 只看该作者
The Results

Putting the database inserts on the side, it has to be highlighted that every script described here takes a different amount of time to parase xml (without doing anything usefull with the records themselves). To compare the results between those 3 scripts I have to take that into account and first find out the time of parsing and then the time of parsing and inserting the records.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
18#
 楼主| 发表于 2012-10-21 09:58 | 只看该作者
The results of parsing and inserting for each language/library of choice used is as follows:

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
19#
 楼主| 发表于 2012-10-21 09:58 | 只看该作者
Final Notes

Process of importing data could be done quicker if we would insert that data in batches instead of each record one by one. This would eat a bit more memory but will increase the speed significantly I believe. Another thing to note is that the scripts described here are by no means perfect (especially the erlang one) and could be improved for speed and efficiency. It was obvious from the beginning that the Java and Ruby versions would be most probably slower than the erlang version (no matter how bad the code is) as they are communicating with the database through the HTTP protocol where the Erlang script is using the CouchDB code directly.

Surprisingly the results differs just a bit. Where parsing in Java using Woodstox was the fastest one it had the slowest insertion times, probably because of the Ektorp performance. Ruby and Erlang parsing times look similar but Erlang was quicker while inserting the data as it didn’t had to go through the loopback interface.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
20#
 楼主| 发表于 2012-10-21 09:58 | 只看该作者
over.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表