Amazon Elasticsearch Service
开发人员指南 (API 版本 2015-01-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

步骤 2:将数据上传到 Amazon ES 域以便编制索引

重要

此过程是一个简明教程,用于上传少量测试数据。有关更多信息,请参阅在 Amazon Elasticsearch Service 中为数据建立索引简介

您可以通过命令行使用 Elasticsearch 索引和批量处理 API 将数据上传到 Amazon Elasticsearch Service 域以便编制索引。

  • 使用索引 API 添加或更新单个 Elasticsearch 文档。

  • 使用批量 API 添加或更新同一 JSON 文件中所述的多个 Elasticsearch 文档。

为简化和方便起见,以下示例请求使用了 curl(常见的 HTTP 客户端)。像 curl 这样的客户端无法执行您的访问策略指定 IAM 用户或角色时所需的请求签名。要成功执行本步骤的说明,您必须使用基于 IP 地址的访问策略以允许未经身份验证的访问,与您在步骤 1 中进行的配置相似。

您可在 Windows 上安装 curl 并通过命令提示符使用它,但 Windows 用户可能会发现,使用 CygwinWindows Subsystem for Linux 之类的工具更方便。macOS 和大多数 Linux 发行版都预安装有 curl。

上传单个文档到 Amazon ES 域

  • 运行以下命令将单个文档添加到 movies 域:

    curl -XPUT elasticsearch_domain_endpoint/movies/_doc/1 -d '{"director": "Burton, Tim", "genre": ["Comedy","Sci-Fi"], "year": 1996, "actor": ["Jack Nicholson","Pierce Brosnan","Sarah Jessica Parker"], "title": "Mars Attacks!"}' -H 'Content-Type: application/json'

有关此命令的详细说明,以及如何向 Amazon ES 发出已签名的请求,请参阅在 Amazon Elasticsearch Service 中为数据建立索引简介

上传包含多个文档的 JSON 文件到 Amazon ES 域

  1. 创建名为 bulk_movies.json 的文件。将以下内容复制并粘贴到其中,并添加一个尾部换行:

    { "index" : { "_index": "movies", "_type" : "_doc", "_id" : "2" } } {"director": "Frankenheimer, John", "genre": ["Drama", "Mystery", "Thriller"], "year": 1962, "actor": ["Lansbury, Angela", "Sinatra, Frank", "Leigh, Janet", "Harvey, Laurence", "Silva, Henry", "Frees, Paul", "Gregory, James", "Bissell, Whit", "McGiver, John", "Parrish, Leslie", "Edwards, James", "Flowers, Bess", "Dhiegh, Khigh", "Payne, Julie", "Kleeb, Helen", "Gray, Joe", "Nalder, Reggie", "Stevens, Bert", "Masters, Michael", "Lowell, Tom"], "title": "The Manchurian Candidate"} { "index" : { "_index": "movies", "_type" : "_doc", "_id" : "3" } } {"director": "Baird, Stuart", "genre": ["Action", "Crime", "Thriller"], "year": 1998, "actor": ["Downey Jr., Robert", "Jones, Tommy Lee", "Snipes, Wesley", "Pantoliano, Joe", "Jacob, Ir\u00e8ne", "Nelligan, Kate", "Roebuck, Daniel", "Malahide, Patrick", "Richardson, LaTanya", "Wood, Tom", "Kosik, Thomas", "Stellate, Nick", "Minkoff, Robert", "Brown, Spitfire", "Foster, Reese", "Spielbauer, Bruce", "Mukherji, Kevin", "Cray, Ed", "Fordham, David", "Jett, Charlie"], "title": "U.S. Marshals"} { "index" : { "_index": "movies", "_type" : "_doc", "_id" : "4" } } {"director": "Ray, Nicholas", "genre": ["Drama", "Romance"], "year": 1955, "actor": ["Hopper, Dennis", "Wood, Natalie", "Dean, James", "Mineo, Sal", "Backus, Jim", "Platt, Edward", "Ray, Nicholas", "Hopper, William", "Allen, Corey", "Birch, Paul", "Hudson, Rochelle", "Doran, Ann", "Hicks, Chuck", "Leigh, Nelson", "Williams, Robert", "Wessel, Dick", "Bryar, Paul", "Sessions, Almira", "McMahon, David", "Peters Jr., House"], "title": "Rebel Without a Cause"}
  2. 运行以下命令以将该文件上传到 movies 域:

    curl -XPOST elasticsearch_domain_endpoint/_bulk --data-binary @bulk_movies.json -H 'Content-Type: application/json'

有关批量文件格式的更多信息,请参阅在 Amazon Elasticsearch Service 中为数据建立索引简介

注意

Amazon ES 支持从在 Amazon ES 和自管理 Elasticsearch 集群上创建的手动快照迁移数据。从自管理 Elasticsearch 集群还原快照是将数据迁移到 Amazon ES 的常见方法。有关更多信息,请参阅还原快照