已创建的数据库会显示在数据库仪表板选项卡上。列表会显示名称信息、每个数据库的大小、每个数据库包含的文档数量以及数据库是否为分区数据库。
📄 Cloudant文档的特性
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_24.png
许多其他数据库系统将数据存储在表中,数据包含在行中,每行都有相同的固定列,并且每个表的模式是预定义的。需要仔细定义列名、数据类型、值约束以及与其他表的关系的列表。
但在Cloudant中,情况则大不相同。我们已经讨论过,Cloudant是一种使用JSON文档存储的文档数据库。因此,您的Cloudant实例拥有称为“数据库”的集合,而不是“表”,每个数据库将包含许多文档。
一个Cloudant文档必须是一个JSON对象,它必须以花括号{}开始和结束,并包含许多键值对属性。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_26.png
JSON对象必须小于1MB,并且可以包含任意数量的字符串、数字、布尔值、数组和嵌套对象。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_27.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_28.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_29.png
📝 文档的基本操作
上一节我们介绍了Cloudant文档的结构,本节中我们来看看如何对文档进行增删改查。
插入文档
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_31.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_32.png
您可以在Cloudant仪表板中选定数据库的详细信息页面上,将文档插入数据库。
插入文档时,需要使用起始和结束花括号{},并且需要使用系统提供的_id或提供您自己的自定义_id。Cloudant使用_id键来唯一标识一个文档,它相当于关系数据库中的主键。
然后,您可以通过在花括号之间手动输入或从其他来源粘贴来填充文档详细信息,最后创建文档以将其插入数据库。
查看文档
当您选择一个数据库时,可以通过三种不同的视图查看其中包含的文档。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_34.png
元数据视图:这是将文档插入数据库后的默认视图,此视图显示每个文档的ID、键和值信息。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_35.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_36.png
表格视图:显示数据库中每个文档的数据表信息。例如,在此示例中,我们可以看到ID、卧室数量、价格和平方英尺的表格数据。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_38.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_39.png
JSON视图:以JSON格式显示文档的内容。您也可以从这里编辑JSON文档。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_41.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_42.png
查询文档
您可以对数据库中的文档运行查询。在查询窗口中输入查询语句,然后运行查询即可。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_44.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_45.png
更新文档
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_47.png
您可以更新数据库中文档的数据。只需选择一个文档,进行所需的更改,然后保存更改。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_49.png
删除文档
您也可以使用Cloudant仪表板从数据库中删除文档。只需选择文档,将其删除,并确认删除操作即可。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_51.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_52.png
🎯 课程总结
本节课中,我们一起学习了在IBM Cloudant中使用数据库的核心知识。
我们了解到,在IBM Cloudant中可以创建和使用两种类型的数据库:非分区数据库和分区数据库。非分区数据库无需定义分区方案,但只提供全局查询。分区数据库则同时提供分区查询和全局查询,并能提供显著的性能和成本优势。数据库的分区类型在创建时设定。
Cloudant是一个使用JSON文档存储的文档数据库系统。您可以使用Cloudant仪表板创建数据库,并利用它来对数据库中的文档进行插入、查看、查询、更新和删除操作。
035:HTTP API基础知识 🌐
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_0.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_1.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_3.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_4.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_5.png
在本节课中,我们将学习HTTP API的基础知识,了解什么是cURL工具,并学习如何使用简单的cURL命令与Cloudant数据库进行交互。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_7.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_8.png
概述
在前面的视频和实验中,你学习了如何使用Cloudant仪表板执行基本的CRUD操作,即如何在Cloudant数据库中创建、读取、更新和删除数据。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_10.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_11.png
Cloudant仪表板本身是一个基于Web的应用程序,它通过超文本传输协议(HTTP)调用Cloudant的应用编程接口(API)。由于Cloudant数据库拥有HTTP API,它们具备一个显著优势:任何想要从中读取或写入数据的互联网设备都可以通过使用标准HTTP库的请求来访问它们。
什么是cURL? 💻
cURL是一个免费的开源命令行工具,可用于向你的Cloudant API发出HTTP请求。它是客户端URL(cURL)计算机软件项目的一部分,该项目最初于1997年发布。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_13.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_14.png
cURL可以从命令行直接使用标准URL语法获取和发送数据(包括文件)。对于更重复的任务,你也可以在脚本中使用它。它还通过使用安全套接字层(SSL)证书验证来支持安全的HTTP(HTTPS)请求。
你可以在github.com/curl/curl访问最新的cURL源代码。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_16.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_17.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_18.png
基本cURL命令
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_20.png
如果你只想从cURL命令行提示符检索一个网页,只需输入单词curl,然后输入网页的URL。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_22.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_23.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_24.png
以下示例将检索IBM Cloud主页:
curlhttps://cloud.ibm.comhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_25.png
但为了避免每次连接时都输入你的Cloudant服务URL,你可以创建一个变量。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_27.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_28.png
设置变量和别名
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_29.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_30.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_31.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_32.png
以下示例使用export命令将我们的Cloudant URL保存为一个名为URL的变量。你需要将用户名、密码和主机部分替换为你自己的Cloudant服务凭据信息。你将在本课结束时的动手实验中进行此操作。
exportURL="https://username:password@hostname.cloudant.com"https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_34.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_35.png
你也可以创建一个别名,作为cURL的快捷方式。以下示例将创建一个名为acurl的别名,并指定了JSON内容类型头。它还使用了一些有用的命令行开关:
-s使你的请求静默(即,你不会看到任何进度或错误消息返回)。-g禁用URL全局解析器。-H允许你指定内容类型头。
aliasacurl='curl -sg -H "Content-Type: application/json"'https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_36.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_37.png
测试连接与操作数据库
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_39.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_40.png
要测试与Cloudant中数据库的连接,你可以使用acurl别名和URL变量(变量前必须加美元符号$)。如果成功,你将收到一些包含欢迎消息、版本号、供应商名称和关于你的Cloudant实例功能信息的JSON代码。
acurl$URLhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_42.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_43.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_44.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_45.png
要检索所有数据库的列表,再次使用acurl别名和美元符号URL变量,但要加上_all_dbs端点。
acurl$URL/_all_dbshttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_47.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_48.png
作为一个附加选项,如果你的操作系统上安装了Python,你可以在这行命令的末尾添加管道命令| python -m json.tool。这将把你的命令输出发送给Python,以便在终端中获得改进的JSON格式,将JSON数据转换为更易读的格式。
acurl$URL/_all_dbs|python-mjson.toolhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_50.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_52.png
还有另一个名为jq的JSON格式化程序,你可以免费下载和安装,它也能将JSON数据格式化成更易读的形式。指定此格式化程序的管道命令是| jq。
acurl$URL/_all_dbs|jqhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_54.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_55.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_56.png
如果你想查看单个数据库的详细信息,只需在斜杠后指定数据库的名称。以下示例将检索名为training的数据库的详细信息。同样,你可以使用两个可选的管道命令之一,将命令输出发送到Python或jq,以提高cURL终端的可读性。
acurl$URL/training acurl$URL/training|python-mjson.tool acurl$URL/training|jq要查看数据库中包含的文档,只需在斜杠后指定数据库名称,然后是_all_docs端点。这将仅检索所有文档的_id和_rev值。
acurl$URL/training/_all_docshttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_58.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_59.png
要同时检索文档的正文,你需要在命令末尾指定?include_docs=true,如以下屏幕示例所示。
acurl$URL/training/_all_docs?include_docs=true要从数据库中检索单个文档,你需要在包含它的数据库名称后指定文档的文档ID。
acurl$URL/training/document_id总结
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_61.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_62.png
本节课中,我们一起学习了以下内容:
Cloudant数据库拥有HTTP API,因此可以通过使用标准HTTP库的请求来访问。
HTTP被Web浏览器、移动设备、常见编程语言以及像cURL这样的命令行脚本工具使用。
cURL是一个免费的开源命令行工具,可以向Cloudant API发出HTTP请求。
cURL可以从命令行或脚本中使用标准URL语法获取和发送数据(包括文件)。
你应该创建一个变量来访问你的Cloudant服务。
你可以使用别名作为cURL的快捷方式。
你可以将cURL命令的输出通过管道传递给Python或jq,以在终端中获得改进的JSON格式。
036:使用HTTP API 🚀
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_0.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_1.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_0.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_1.png
概述
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_3.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_4.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_5.png
在本节课中,我们将学习如何在Cloudant中使用HTTP API。你将掌握使用cURL工具执行不同HTTP方法,从而对数据库、文档进行创建、读取、更新、删除(CRUD)操作,并运行查询。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_6.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_7.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_8.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_9.png
HTTP方法与cURL
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_11.png
上一节我们介绍了使用cURL执行一些基本命令。你可能注意到,在运行最后几个cURL命令时,我们并未指定HTTP方法。这是因为当cURL命令未明确指定方法时,默认使用GET方法。
Cloudant的API通常使用动词作为其HTTP方法。例如,GET用于检索数据。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_13.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_14.png
但是,如果我们想创建数据库、添加文档或删除文档,这些任务就需要使用其他HTTP方法。例如:
PUT:用于创建数据库、创建文档或修改现有文档。
POST:用于运行查询、创建索引或创建文档(需配合
-d参数)。DELETE:用于删除数据库、文档和索引。
要使用GET以外的HTTP方法,你需要使用-X命令行开关,然后指定你想在cURL中使用的具体方法。例如:curl -X PUT。
数据库操作
创建数据库
要在Cloudant中创建数据库,需要使用-X开关配合PUTHTTP请求,然后指定服务连接变量和要创建的数据库名称。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_16.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_17.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_18.png
命令格式:
curl-XPUT$SERVICE/coursedbhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_19.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_20.png
删除数据库
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_22.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_23.png
要删除(或称为“删除”)Cloudant中的数据库,同样使用-X开关,但这次配合DELETEHTTP请求,然后指定服务连接变量和要删除的数据库名称。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_25.png
命令格式:
curl-XDELETE$SERVICE/coursedbhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_26.png
要验证这些HTTP请求是否成功,请查看响应中是否包含{"ok": true}。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_28.png
文档操作
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_29.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_30.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_31.png
插入文档
要在Cloudant中插入文档,如前所述,使用-X开关配合PUTHTTP请求,然后指定服务连接变量和要插入文档的数据库名称。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_33.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_34.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_35.png
接着,你需要提供文档的ID(本例中手动指定为212),然后使用-d命令行开关来输入构成文档主体的JSON数据。
命令示例:
curl-XPUT$SERVICE/coursedb/212-d'{"course_name": "Introduction to NoSQL", "level": "Beginner"}'https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_37.png
要验证此PUT请求是否成功,请查看响应是否包含{"ok": true},后面跟着你提供的文档ID和一个修订令牌值(rev)。修订令牌是数据库在写入文档时生成的一个标识值。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_38.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_39.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_40.png
更新文档
为了更新数据库中的文档,你需要知道该文档的修订令牌值。这个值可以通过在创建文档时记录响应来获得,或者使用_all_docs端点(如上一视频所示)来检索数据库中的所有文档列表。
在Cloudant中更新文档时,同样使用-X开关配合PUTHTTP请求,然后指定服务连接变量以及要更新文档的路径(即数据库名后跟文档ID)。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_42.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_43.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_44.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_45.png
接着,使用-d命令行开关,并提供更新后文档的新主体。在本例中,我们为文档添加了一个额外的值。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_46.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_47.png
重要提示:你必须提供整个更新后的文档主体。不能只更新文档的一部分,必须用更新后的版本完全替换它。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_49.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_50.png
最后,你必须提供文档的修订令牌值。如果不提供此修订令牌值,将会看到类似示例所示的错误信息。
要验证此更新请求是否成功,请查看响应是否包含{"ok": true},后面跟着文档ID和新的修订令牌值。
删除文档
要从数据库中删除文档,使用-X开关配合DELETEHTTP请求,然后指定服务连接变量、要删除文档的路径,最后是文档当前的修订令牌值。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_52.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_53.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_54.png
命令格式:
curl-XDELETE$SERVICE/coursedb/212?rev=2-9c652960f5f5ec7c0e6f5e8d5a6b7c8dhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_55.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_56.png
如果不提供正确的修订令牌值,将会看到错误信息。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_58.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_60.png
要验证此删除请求是否成功,请查看响应中是否包含{"ok": true}。
查询数据
到目前为止,你看到的所有操作都是CRUD操作,即创建、读取、更新和删除。但如果你想查找数据库中文档包含的信息,例如哪些课程适合初学者,哪些课程涉及特定技术或软件应用程序,或者哪些课程列出Jane Doe为讲师,这时就需要进行查询。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_62.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_63.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_64.png
Cloudant查询语言基于MongoDB查询语言,使用JSON格式的对象来表达查询。这些查询使用selector属性来定义查询要返回的数据子集,这相当于SQL中的WHERE子句。
JSON查询对象通过POSTHTTP请求发送到数据库的_find端点来执行查询。这些JSON查询可以从cURL命令行或Cloudant仪表板触发。
以下是一个使用cURL查找文档ID为212的文档的示例。注意selector属性的使用。-H开关允许你指定JSON内容类型头。
命令示例:
curl-XPOST$SERVICE/coursedb/_find-H"Content-Type: application/json"-d'{"selector": {"_id": "212"}}'以下是查找所有级别为“Beginner”的课程,以及所有列出“Jane Doe”为讲师的课程的示例。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_66.png
你还可以在查询中使用逻辑运算符,例如大于($gt)和小于($lt)。因此,本幻灯片上的示例是查找包含超过4个模块的课程。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_68.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_69.png
命令示例:
curl-XPOST$SERVICE/coursedb/_find-H"Content-Type: application/json"-d'{"selector": {"modules": {"$gt": 4}}}'https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_70.png
使用JSON文件进行查询
如果你有一个需要定期重用的查询,或者JSON查询本身很大,与其像我们在这里和动手实验室中那样直接在cURL命令行中输入JSON查询,你很可能希望将JSON查询存储在一个.json文件中。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_72.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_73.png
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_74.png
如果你这样做,可以使用-d @后跟JSON文件名来指定JSON文件,如本屏幕示例所示。
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_76.png
命令示例:
curl-XPOST$SERVICE/coursedb/_find-H"Content-Type: application/json"-d@query.jsonhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_78.png
总结
本节课中,我们一起学习了以下内容:
https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_80.png
cURL默认使用GETHTTP方法,除非你使用
-X命令行开关指定其他方法。可以在cURL中使用的其他HTTP方法包括:PUT、POST和DELETE。
使用PUT请求来创建数据库、创建文档和更新文档。
使用POST请求来运行查询、创建文档和创建索引。
使用DELETE请求来删除(或删除)数据库、文档和索引。
Cloudant Query是一种基于MongoDB查询语言的查询语言,用于运行以JSON对象形式表达的查询。