楼主: jieforest

Couchbase API数据访问Guide

[复制链接]
论坛徽章:
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#
 楼主| 发表于 2013-3-11 10:57 | 只看该作者
WRITING AND UPDATING DATA

Add Operations

The add method adds a value to the database with the specified key, but will fail (Couchbase::Error::KeyExists) if the key already exists in the database.
  1. client.add(key, value, options = {})
  2. client.add[key, options = {}] = value
复制代码
Replace Operations

The replace method will replace an existing key with a new value but will fail (Couchbase::Error::NotFound) if the key does not exist in the database.
  1. client.replace(key, value, options = {})
  2. client.replace[key, options = {}] = value
复制代码

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-11 10:59 | 只看该作者
Set Operations

The set method stores a value to the database with a specified key, overwriting existing content if it exists (see add/replace above).
  1. client.set(key, value, options = {})
  2. client.set[key, options = {}] = value
复制代码
options:
  1. :ttl     [Fixnum, Integer] Time to Live in seconds [optional]
  2. :flags   [Fixnum] Flags that you want to store/retrieve [optional]
  3. :cas     [Fixnum] Optimistic locking for update control, must match the document cas in Couchbase to succeed
  4. :format  [:document, :plain, :marshal] Explicit format setting, :plain for strings, :document for Hash(JSON), :marshal to use Marshal. load and Marshal.dump; format is :document by default
复制代码
Asynchronous Store Example (Block)
  1. client.run do
  2.   client.add(“foo” => “val1”, “bar” => “val2”) do |ret|
  3.     ret.operation #=> :add
  4.     ret.success? #=> true
  5.     ret.keys #=> “foo”, “bar” in separate calls
  6.     ret.cas
  7.     ret.flags
  8.   end
  9. end
复制代码

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-11 11:00 | 只看该作者
READING DATA

The Get method is versatile, allowing for both optimistic and pessimistic locking, multiple gets, and hash-like syntax.

Simple Get Operations
  1. val = client.get(key, options = {})
  2. val = client[“key”]
复制代码
Extended Tuple Get Operations
  1. val, flags, cas = client.get(key, :extended => true)
  2. val, flags, cas = client[“key”, :extended => true]
复制代码

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-11 11:01 | 只看该作者
Multi-Get Operations
  1. val_array = client.get(keys)
  2. val_hash = client.get(keys, :assemble_hash => true)
复制代码
options:
  1. :extended     [String, Symbol] Key used to reference the value
  2. :quiet        [true, false] Return nil for missing keys default, or false to Return Couchbase::Error::KeyNotFound for missing key
  3. :ttl          [Fixnum, Integer] Get and Touch, :ttl will also reset the TTL at the same time as the get (seconds)
  4. :lock         Lock the document, true to use default lock timeout, or integer for number of seconds to lock
  5. :format       [:document, :plain, :marshal] Explicit format setting, :plain for strings, :document for Hash(JSON), :marshal to use Marshal.load and Marshal.dump
  6. :assemble_hash [true, false] For Multi-Get, will return a Hash keyed on the document keys instead of default of array
复制代码

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-11 11:02 | 只看该作者
GETTING STATS FROM COUCHBASE

Obtain stats from all servers for the connection (bucket)
  1. client.stats
  2. client.stats[“stats”]
复制代码
To fetch memory stats
  1. client.stats(:memory)
复制代码
Parameters:
  1. stat        Individual stat string (i.e. "curr_items")
复制代码

使用道具 举报

回复
论坛徽章:
58
马上加薪
日期:2014-05-22 17:23:14兰博基尼
日期:2013-10-28 16:48:53比亚迪
日期:2013-10-24 14:53:08法拉利
日期:2013-10-23 14:22:02三菱
日期:2013-10-23 12:33:08大众
日期:2013-10-23 10:23:25本田
日期:2013-10-16 10:24:23法拉利
日期:2013-10-15 09:01:46宝马
日期:2013-10-10 12:39:51ITPUB社区12周年站庆徽章
日期:2013-10-08 17:44:42
16#
发表于 2013-3-11 13:54 | 只看该作者
mark

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-12 15:02 | 只看该作者
DELETING AND UPDATING DATA

To delete keys/values
  1. client.delete(key)
复制代码
Atomic Counter Operations

Atomic Counters are only atomic per cluster, but are useful for many different patterns and numeric data. They are unsigned positive integers.

You cannot use atomic operations on floats or objects or arrays.
  1. client.incr(key, delta, options = {})
  2. client.decr(key, delta, options = {})
复制代码

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-12 15:04 | 只看该作者
options:
  1. delta            [Fixnum] Amount to incr/decr, default is 1, can be up to 64 bits
  2. :create         [true, false] If set to true, if key doesn’t exist, initializes to zero but doesn’t increment, can be combined with :initial
  3. :initial           [Fixnum, Unsigned Integer] Sets initial value if key doesn’t exist, doesn’t increment the initial value!
  4. :extended    [true, false] Returns [value, cas] tuple instead of just value
  5. :ttl                [Fixnum, Integer] Sets TTL, doesn’t alter existing TTL, will only be applied to a new item created via :create and/or :initial
复制代码
Touch Operations

Reset the TTL expiration on one or more keys explicitly with the touch command, can also be done with a get command.
  1. client.touch(key, ttl)
  2. client.touch({“key1”=> ttl1, “key2” => ttl2})
复制代码

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-12 15:05 | 只看该作者
Durability Requirements

Observe the state of the keys on all the nodes or using :replicated and ersisted it allows to set up the waiting rule.
  1. client.observe(*keys, options = {})
  2. client.observe_and_wait(*keys, options = {})
复制代码
Non-JSON Operations

For non-json values, prepend and append can concatenate to existing values accordingly. Defaults tolain format for encoder/decoder.
  1. client.prepend(key, value)
  2. client.append(key, value)
复制代码

使用道具 举报

回复
论坛徽章:
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#
 楼主| 发表于 2013-3-12 15:06 | 只看该作者
QUERYING DATA

With Couchbase Server 2.0, you can create Indexes through Map/Reduce functions. http://www.couchbase.com/docs/co ... ouchbase-views.html

Create Design Doc Object

Refer to the design_doc by name, the Views are an array of view names, as well as functions defined by name.
  1. ddoc = client.design_docs[‘design_doc_name’]
  2. ddoc.views #=> [‘view_name’, ‘view_name_2’, …]
复制代码
Use dot notation for accessing the view by your defined view name and pass in options for query parameters:
  1. ddoc.view_name(params = {}).each do |doc|
  2. # do stuff with docs
  3. end
复制代码

使用道具 举报

回复

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

本版积分规则 发表回复

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