已创建的数据库会显示在数据库仪表板选项卡上。列表会显示名称信息、每个数据库的大小、每个数据库包含的文档数量以及数据库是否为分区数据库。 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.pngJSON对象必须小于1MB并且可以包含任意数量的字符串、数字、布尔值、数组和嵌套对象。https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_27.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_28.pnghttps://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.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_39.pngJSON视图以JSON格式显示文档的内容。您也可以从这里编辑JSON文档。https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_41.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/e1e88bd06515f8901968b8234dfdd447_52.png 课程总结本节课中我们一起学习了在IBM Cloudant中使用数据库的核心知识。我们了解到在IBM Cloudant中可以创建和使用两种类型的数据库非分区数据库和分区数据库。非分区数据库无需定义分区方案但只提供全局查询。分区数据库则同时提供分区查询和全局查询并能提供显著的性能和成本优势。数据库的分区类型在创建时设定。Cloudant是一个使用JSON文档存储的文档数据库系统。您可以使用Cloudant仪表板创建数据库并利用它来对数据库中的文档进行插入、查看、查询、更新和删除操作。035HTTP API基础知识 https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_0.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_1.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_3.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_4.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_11.pngCloudant仪表板本身是一个基于Web的应用程序它通过超文本传输协议HTTP调用Cloudant的应用编程接口API。由于Cloudant数据库拥有HTTP API它们具备一个显著优势任何想要从中读取或写入数据的互联网设备都可以通过使用标准HTTP库的请求来访问它们。什么是cURL cURL是一个免费的开源命令行工具可用于向你的Cloudant API发出HTTP请求。它是客户端URLcURL计算机软件项目的一部分该项目最初于1997年发布。https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_13.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_14.pngcURL可以从命令行直接使用标准URL语法获取和发送数据包括文件。对于更重复的任务你也可以在脚本中使用它。它还通过使用安全套接字层SSL证书验证来支持安全的HTTPHTTPS请求。你可以在github.com/curl/curl访问最新的cURL源代码。https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_16.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_17.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_23.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_30.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_31.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_32.png以下示例使用export命令将我们的Cloudant URL保存为一个名为URL的变量。你需要将用户名、密码和主机部分替换为你自己的Cloudant服务凭据信息。你将在本课结束时的动手实验中进行此操作。exportURLhttps://username:passwordhostname.cloudant.comhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_34.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_35.png你也可以创建一个别名作为cURL的快捷方式。以下示例将创建一个名为acurl的别名并指定了JSON内容类型头。它还使用了一些有用的命令行开关-s使你的请求静默即你不会看到任何进度或错误消息返回。-g禁用URL全局解析器。-H允许你指定内容类型头。aliasacurlcurl -sg -H Content-Type: application/jsonhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_36.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_43.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_44.pnghttps://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.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_55.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_59.png要同时检索文档的正文你需要在命令末尾指定?include_docstrue如以下屏幕示例所示。acurl$URL/training/_all_docs?include_docstrue要从数据库中检索单个文档你需要在包含它的数据库名称后指定文档的文档ID。acurl$URL/training/document_id总结https://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/39dbf84a95cbc58453635a4921613d46_61.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_1.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_0.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_4.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_7.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_8.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_9.pngHTTP方法与cURLhttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_17.pnghttps://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.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_30.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_34.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_39.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_43.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_44.pnghttps://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.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_53.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_54.png命令格式curl-XDELETE$SERVICE/coursedb/212?rev2-9c652960f5f5ec7c0e6f5e8d5a6b7c8dhttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_55.pnghttps://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.pnghttps://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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_63.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_64.pngCloudant查询语言基于MongoDB查询语言使用JSON格式的对象来表达查询。这些查询使用selector属性来定义查询要返回的数据子集这相当于SQL中的WHERE子句。JSON查询对象通过POSTHTTP请求发送到数据库的_find端点来执行查询。这些JSON查询可以从cURL命令行或Cloudant仪表板触发。以下是一个使用cURL查找文档ID为212的文档的示例。注意selector属性的使用。-H开关允许你指定JSON内容类型头。命令示例curl-XPOST$SERVICE/coursedb/_find-HContent-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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_69.png命令示例curl-XPOST$SERVICE/coursedb/_find-HContent-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.pnghttps://github.com/OpenDocCN/dsai-notes-pt1-zh/raw/master/docs/ibm-dtengi-10/img/da807cf3168382c724b630d899546446_73.pnghttps://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-HContent-Type: application/json-dquery.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.pngcURL默认使用GETHTTP方法除非你使用-X命令行开关指定其他方法。可以在cURL中使用的其他HTTP方法包括PUT、POST和DELETE。使用PUT请求来创建数据库、创建文档和更新文档。使用POST请求来运行查询、创建文档和创建索引。使用DELETE请求来删除或删除数据库、文档和索引。Cloudant Query是一种基于MongoDB查询语言的查询语言用于运行以JSON对象形式表达的查询。