Couchbase II( View And Index)

Couchbase II( View And Index)html

 

Views

view的做用是從沒有結構和半結構的數據對象中抽取過濾須要的信息,並生成相關的index信息,一般生成json數據。 view經過迭代bucket全部文檔而提取過濾信息,進而生成index。node

一個bucket能夠有多個設計文檔,一個設計文檔(Design Document)能夠有多個views。git

開發view和產品view開發view 以dev——前綴開始,只對部分數據作index和view,以幫助不用對全部的數據作view, 而調試獲得正確的view,以後能夠切換到產品模式(去掉dev_前綴)github

view名必定要是一個或多個uft編碼字符,首尾中間不含空白(空格, tab等)數據庫

View的做用

  1. 索引和查詢存儲的對象json

  2. 構建輸出指定的對象類型數組

  3. 從數據庫抽取過濾信息緩存

  4. 計算,統計,彙集數據集合的信息app

View contents

view的輸出對應每一個emit有三個重要屬性函數

  • Document ID

    每次調用emit()都會包含 document id,幫助加載全部的文檔經過get()
  • View key

    這是emit()的第一個參數,你能夠指定key任何值,包括數組(一組值)來查詢複雜的選擇和報告。
    key的值也決定如何插敘
  • View value

    這是emit()的第二個參數,只能被自定義的reduce方法使用。

查詢和選擇

Couchbase 支持三種選擇方式:
  • Speific Key(指定的鍵)

    $result = $cb->view("recipes", "bytitle", array('key' => 'Apple Pie'));
  • One or more keys

    $result = $cb->view("dev_recipes", "bytitle", array('keys' => array('Shepherds
    pie', 'Mariners pie')));
  • Key range

    #會找出tittle在Meat loaf到Mexican tacos間全部的數據
    $result = $cb->view("dev_recipes", "bytitle", array('startkey' => 'Meat
    loaf','endkey' => 'Mexican tacos'));
可選的附加功能

key能夠幫助過濾查詢,但view也有排序和其餘需求:

  • descending

    Return the documents in descending by key order
  • endkey

    Stop returning records when the specified key is reached. Key must be specified as a JSON value.
  • endkey_docid

    Stop returning records when the specified document ID is reached
    full_set Use the full cluster data set (development views only).
  • group

    Group the results using the reduce function to a group or single row
  • group_level

    Specify the group level to be used
  • inclusive_end

    Specifies whether the specified end key should be included in the result
  • key

    Return only documents that match the specified key. Key must be specified as a JSON value.
  • keys

    Return only documents that match each of keys specified within the given array. 
    Key must be specified as a JSON value. Sorting is not applied when using this option.
  • limit

    Limit the number of the returned documents to the specified number
  • on_error

    Sets the response in the event of an error. stop will stop returning rows; 
    continue will notify you of the error, but continue returning rows from other nodes.
  • reduce

    Use the reduction function.
  • skip

    Skip this number of records before starting to return the results
  • stale

    Allow the results from a stale view to be used. ok uses a stale index; 
    false forces an index update; 
    up date_after updates the index after it has been accessed (default)
  • startkey

    Return records with a value equal to or greater than the specified key. 
    Key must be specified as a JSON value.
  • startkey_docid

    Return records starting with the specified document ID

處理不一樣的數據格式

有時可能由於應用的版本,而形成輸出不一樣等,而輸入的數據格式也不一樣,emit 能夠出現屢次,經過選擇控制輸出不一樣格式(JS):

function (doc, meta){
    if (doc.preptime && doc.cooktime){
        emit(parseInt(doc.preptime, 10) + parseInt(doc.cooktime, 10), null);
    }
    else{
        emit(parseInt(doc.totalcooktime, 10), null);
    }
}

當須要使用到reduce時,這時emit的第二個參數,就須要傳入value,若是不要用到,就像上面例子傳入null就好

index 更新

index更新會在各個節點同步時,觸發更新。Couchbase在讀寫時(get, set)時,先從緩存層開始,只用序列化更新到磁盤,纔會更新index:

  1. index會根據服務配置設定的更新頻率自動更新。

  2. 在query時,能夠指定是否更新index

  3. 刪除文檔時,只用硬盤上的數據被刪除,index纔會被刪除

  4. 文檔有TTL過時時限,相關index會自動更新當文檔過時

stale 參數

在客戶端獲取數據時,設置 -stale- 參數能夠設定三種index 更新狀態:

  • update_after

    在獲取數據後更新index,也就是下次查詢時使用的是更新的view
  • ok

    使用當前版本的index,不觸發index更新
  • false

    強制更新全部的索引後返回查詢結果,也許會很費時間,由於要更新全部的view和index。

Reductions

_count
_count內建函數用來統計來自map的輸入行
_sum
_sum 會把map輸出的值或多個值加起來。
_stats
用於彙集統計,最大,最小等值。

文檔的元數據

meta 含以下信息和字段:

  • id

    數據對象的ID或鍵(key),和set方法用來寫數據的key是同樣的
  • rev

    內建版本號,用來追蹤當前數據的版本。rev字段含的信息是不具備一致性或可跟蹤性,不可用於客戶端應用中。
  • type

    保存文檔的類型, JSON文檔是json類型, 二進制文檔是base64類型
  • flags

    flags是一個32爲的整數,用來保存數據保存建立的時間, 可用狀態由客戶端是否支持決定
  • expiration

    數據對象過時時間 ,和TTL的表示一致
相關文章
相關標籤/搜索