Posts Tagged ‘couchdb’

23rd May
2009
written by cashplk

以下是一个简单的使用Ruby操作CouchDB数据库的代码:

新建fromRuby2CouchDB.rb文件,具体内容如下:

require 'net/http'
 
module Couch
  class Server
    def initialize(host, port, option = nil)
      @host = host;
      @port = port;
      @option = option;
    end
 
    def delete(uri)
      request(Net::HTTP::Delete.new(uri))
    end
 
    def get(uri)
      request(Net::HTTP::Get.new(uri))
    end
 
    # put json to request
    def put(uri, json)
      req = Net::HTTP::Put.new(uri)
      req["content.type"] = "application/json"
      req.body = json
      request(req)
    end
 
    def post(uri, json)
      req = Net::HTTP::Post.new(uri)
      req["content-type"] = "application/json"
      req.body = json
      request(req)
    end
 
    def request(req)
      res = Net::HTTP.start(@host, @port) {
        |http|http.request(req)
      }
      if (not res.kind_of?(Net::HTTPSuccess))
        handle_error(req, res)
      end
      res
    end
 
    private
 
    def handle_error(req, res)
      e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:" +
 
          "#{req.method}\nURI:#{req.path}\n#{res.body}")
      raise e
    end
  end
 
end

在文件路径下,以命令行模式运行:irb,启动ruby编译器:
irb(main):001:0> require “fromRuby2CouchDB.rb”
=> true
irb(main):002:0> server = Couch::Server.new(“127.0.0.1″,”5984″)
=> #<Couch::Server:0xb7bdd258 @option=nil, @port=”5984″, @host=”127.0.0.1″>
irb(main):003:0> server.get(“/blog/”)
会插入一个新的数据库,名为blog。如果不对,会打印出相应的错误。成功后,可以在页面查看是否存在该数据库。

Tags: , , ,
23rd May
2009
written by cashplk

看到InfoQ上关于CouchDB的报道,正好最近也在研究Erlang,就看看。

CouchDB是一个文档形数据库。本身的程序已经实现了基本的管理。和数据库的交互采用的是JS,具体传输的数据是JSON格式的。

下面是Eralng的架构图:

erlang-arch

可以看到,中间层完全使用Erlang编写, 底层搜索则是Lucene。

ubuntu下面的安装:

sudo apt-get install couchdb

OK了之后,直接运行: sudo couchdb 即可运行。

/etc/couchdb/couch.ini,可以对运行的程序参数进行配置。

访问:http://127.0.0.101:5984/_utils/browse/index.html 就可以看到数据库的情况了。

此后的所有操作都可以在界面完成。

假如新建了一个blog的数据库,字段为text.

需要查询数据的时候,直接访问:http://127.0.0.1:5984/blog/1 就可以了。

显示的数据和这个类似:

{"_id":"1","_rev":"2166111176","text":"hello,world"}

初步使用了一下,觉得还不错,但是还是有几个疑问:
1,对于大规模的操作,性能如何?
2,直接页面编辑,必须按照JSON格式, 输入字符之类的,要“”补全。不能自动实现此功能,不爽。
3,中文字符似乎处理还不是很好。

Erlang的官方网站:http://couchdb.apache.org/index.html