Couchbase II( View And Index)

 

Views

view的作用是从没有结构和半结构的数据对象中抽取过滤需要的信息,并生成相关的index信息,通常生成json数据。 view通过迭代bucket所有文档而提取过滤信息,进而生成index。

一个bucket可以有多个设计文档,一个设计文档(Design Document)可以有多个views。

开发view和产品view开发view 以dev——前缀开始,只对部分数据做index和view,以帮助不用对所有的数据做view, 而调试得到正确的view,之后可以切换到产品模式(去掉dev_前缀)

view名一定要是一个或多个uft编码字符,首尾中间不含空白(空格, tab等)

View的作用

  1. 索引和查询存储的对象

  2. 构建输出指定的对象类型

  3. 从数据库抽取过滤信息

  4. 计算,统计,聚集数据集合的信息

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的表示一致

Couchbase II( View And Index)的更多相关文章

  1. javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/pay/index] again. Check your ViewResolver setup!

    2019-08-08 17:12:03.544 ERROR 13748 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Se ...

  2. [Hive - LanguageManual] Create/Drop/Alter -View、 Index 、 Function

    Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version info ...

  3. SQL Server 索引(index) 和 视图(view) 的简单介绍和操作

    --索引(index)和视图(view)-- --索引(index)----概述: 数据库中的索引类似于书籍的目录,他以指针形式包含了表中一列或几列组合的新顺序,实现表中数据库的逻辑排序.索引创建在数 ...

  4. HBase、Redis、MongoDB、Couchbase、LevelDB主流 NoSQL 数据库的对比

    最近小组准备启动一个 node 开源项目,从前端亲和力.大数据下的IO性能.可扩展性几点入手挑选了 NoSql 数据库,但具体使用哪一款产品还需要做一次选型. 我们最终把选项范围缩窄在 HBase.R ...

  5. Couchbase学习和使用

    Couchbase介绍 couchbase的关键有两点:延后写入和松散存储.延后写入,顾名思义,couchbase在对数据进行增删时会先体现在内存中,而不会立刻体现在硬盘上,从内存的修改到硬盘的修改这 ...

  6. Couchbase I

    Couchbase第一印象(架构特性) 面向文档 保存的字节流总有一个 DOCUMENT ID(Object_ID) 高并发性,高灵活性,高拓展性,容错性好 面向文档的集群存储系统 每个文档用一个唯一 ...

  7. Couchbase第一印象(架构特性)

    Couchbase第一印象(架构特性) 面向文档 保存的字节流总有一个 DOCUMENT ID(Object_ID) 高并发性,高灵活性,高拓展性,容错性好 面向文档的集群存储系统 每个文档用一个唯一 ...

  8. ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)

    在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie ...

  9. IOS UIView 03- 自定义 Collection View 布局

    注:本人是翻译过来,并且加上本人的一点见解. 前言 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一颗新星.它和 UITableView 共享一套 API ...

随机推荐

  1. HDU1430 BFS + 打表 + 康托展开

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 , 一道比较好的题. 这道题要用到很多知识,康托展开.BFS.打表的预处理还要用到一一映射,做完 ...

  2. 如何在SAP云平台上使用MongoDB服务

    首先按照我这篇文章在SAP云平台上给您的账号分配MongboDB服务:如何在SAP云平台的Cloud Foundry环境下添加新的Service 然后从这个链接下载SAP提供的例子程序. 1. 使用命 ...

  3. codeforces 600D Area of Two Circles' Intersection

    分相离,内含,想交三种情况讨论一下. 主要是精度和数据范围的问题,首先数据用long double,能用整型判断就不要用浮点型. 题目中所给的坐标,半径是整型的,出现卡浮点判断的情况还是比较少的. 最 ...

  4. 【BZOJ2730】[HNOI2012] 矿场搭建(找割点)

    点此看题面 大致题意: 一张无向图,要求你在去掉任意一个节点之后,剩余的每个节点都能到达一个救援出口,问至少需要几个救援出口. 第一步:\(Tarjan\)求割点 首先,我们要跑一遍\(Tarjan\ ...

  5. [转] 防止js全局变量污染方法总结

    javaScript 可以随意定义保存所有应用资源的全局变量.但全局变量可以削弱程序灵活性,增大了模块之间的耦合性.在多人协作时,如果定义过多的全局变量 有可能造成全局变量冲突,也就是全局变量污染问题 ...

  6. 谷歌SwitchySharp && SwitchyOmega插件

    http://pan.baidu.com/s/1jOwgu 谷歌SwitchySharp插件 http://pan.baidu.com/s/1mgl7e2k SwitchySharp的升级版Switc ...

  7. Java代码工具箱之解析单行单列简单Excel

    1. 使用开源工具 jxl.jar 2. 功能:解析常规Excel.xls格式测试可行,xlsx未测试.Excel测试格式为常规类似table这种简单布局文件.第一行为标题,后面行为内容.代码 可正确 ...

  8. icon踩坑记录

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 深入理解new String()

    一. 引言 new String("hello")这样的创建方式,到底创建了几个String对象? 二. 分析 String s1 = "HelloWorld" ...

  10. Java第六次作业:RuPengGame setGameSize setGameTitle alert loadBgView playSound pause closeSound confirm input createText setTextPosition setTextColor setTextFontSize hideText showText CreateImage(number)

    package com.swift; import java.awt.Color; import com.rupeng.game.GameCore;//导入游戏引擎包 //实现Runnable接口 p ...