
# 使用开源Elasticsearch API导入数据
在应用开发调试或小规模数据迁移场景中，开发者经常需要快速将本地的JSON数据文件（如测试数据集、配置文件）写入Elasticsearch集群。相比于配置复杂的Logstash或CDM等迁移工具，直接调用开源Elasticsearch API（如 _bulk批量接口）是一种更轻量、灵活的解决方案。您既可以在Kibana的界面中进行交互式写入，也可以在ECS等服务器上通过Curl命令行脚本化批量导入，实现数据的敏捷上云。
#### 方式一：在Kibana上导入数据
在Kibana上支持通过POST命令使用开源Elasticsearch API导入数据。
适用场景：开发调试、临时写入少量数据、语法验证。
1. 登录Kibana。
   1. 登录[云搜索服务管理控制台](https://console.huaweicloud.com/elasticsearch/)。
   
   2. 在左侧导航栏，选择"集群管理 \> Elasticsearch"。
   
   3. 在集群列表，选择目标集群，单击操作列的"Kibana"，登录Kibana。
   
   4. 在Kibana左侧导航栏选择"Dev Tools"，进入操作页面。 控制台左侧是命令输入框，其右侧的三角形图标为执行按钮，右侧区域则显示执行结果。
      
    
2. （可选）创建索引，并指定自定义映射来定义数据类型。 例如，执行如下命令，创建索引"my_store"。
   - Elasticsearch版本大于或等于7.x的代码示例：
     ```
     PUT /my_store
     {
         "settings": {
             "number_of_shards": 1
         },
         "mappings": {
             "properties": {
                 "productName": {
                     "type": "text"
                 },
                 "size": {
                     "type": "keyword"
                 }
             }
         }
     }
     ```
     
   
   - Elasticsearch版本小于7.x的代码示例：
     ```
     PUT /my_store
     {
         "settings": {
             "number_of_shards": 1
         },
         "mappings": {
             "products": {
                 "properties": {
                     "productName": {
                         "type": "text"
                     },
                     "size": {
                         "type": "keyword"
                     }
                 }
             }
         }
     }
     ```
     
    
3. 将数据导入索引。 例如，执行如下命令，导入一条数据到索引"my_store"。
   - Elasticsearch版本大于或等于7.x的代码示例：
     ```
     POST /my_store/_bulk  
     {"index":{}}  
     {"productName":"Latest art shirts for women in 2017 autumn","size":"L"}
     ```
     
   
   - Elasticsearch版本小于7.x的代码示例：
     ```
     POST /my_store/products/_bulk 
     {"index":{}} 
     {"productName":"Latest art shirts for women in 2017 autumn","size":"L"}
     ```
     
    
4. 结果验证。 观察返回结果，当"errors"字段为"false"，且"items"数组中每条数据的"result"为"created"，则表示全部导入成功。
   
 
#### 方式二：在ECS上使用Curl命令行导入数据
在ECS服务器上支持通过Curl命令使用开源Elasticsearch API导入数据文件。
适用场景：自动化脚本、批量导入本地JSON文件。
1. 准备服务器。购买与Elasticsearch集群在同一VPC下的弹性云服务器 (ECS)。 ECS的使用指导请参见[快速购买和使用Linux ECS](https://support.huaweicloud.com/qs-ecs/ecs_01_0103.html)。
   
2. 获取集群的内网访问地址。
   1. 登录[云搜索服务管理控制台](https://console.huaweicloud.com/elasticsearch/)。
   
   2. 在左侧导航栏，选择"集群管理 \> Elasticsearch"。
   
   3. 在集群列表，选择目标集群，在"内网访问地址"列获取并记录集群的内网地址，一般是"\<host\>:\<port\>"或"\<host\>:\<port\>,\<host\>:\<port\>"样式，例如"10.62.179.32:9200,10.62.179.33:9200"。 当集群中存在Client节点时，仅显示所有Client节点的IP和端口；否则，显示所有数据节点和冷数据节点的IP和端口。
      
    
3. 验证连通性。在ECS服务器ping Elasticsearch集群的内网IP地址，能正常访问则表示网络连通。
4. 准备数据文件。将JSON数据文件上传至ECS。 例如，将如下数据保存为"test.json"文件，上传到ECS。
   - Elasticsearch版本大于或等于7.x的数据示例：
     ```
     {"index": {"_index":"my_store"}}
     {"productName": "2019秋装新款文艺衬衫女装","size": "M"}
     {"index": {"_index":"my_store"}}
     {"productName": "2019秋装新款文艺衬衫女装","size": "L"}
     ```
     
   
   - Elasticsearch版本小于7.x的数据示例：
     ```
     {"index": {"_index":"my_store","_type":"products"}}
     {"productName": "2019秋装新款文艺衬衫女装","size": "M"}
     {"index": {"_index":"my_store","_type":"products"}}
     {"productName": "2019秋装新款文艺衬衫女装","size": "L"}
     ```
     
    
5. （可选）创建索引，并指定自定义映射来定义数据类型。 例如，执行如下命令，创建索引"my_store"。
   Elasticsearch版本大于或等于7.x的代码示例如下。
   - 非安全模式的集群（HTTP）：无需认证，直接访问。
     ```
     curl -X PUT "http://<host>:<port>/my_store" \
          -H 'Content-Type: application/json' \
          -d '{
            "settings": {
              "number_of_shards": 1
            },
            "mappings": {
              "properties": {
                "productName": {
                  "type": "text"
                },
                "size": {
                  "type": "keyword"
                }
              }
            }
          }'
     ```
     
   
   - 安全模式+HTTP协议的集群：需要身份认证（如果密码包含"!"等特殊字符，请使用单引号包裹，例如："-u 'admin':'Pass!word'"）。
     ```
     curl -X PUT -u <user>:<password> "http://<host>:<port>/my_store" \
          -H 'Content-Type: application/json' \
          -d '{
            "settings": {
              "number_of_shards": 1
            },
            "mappings": {
              "properties": {
                "productName": {
                  "type": "text"
                },
                "size": {
                  "type": "keyword"
                }
              }
            }
          }'
     ```
     
   
   - 安全模式+HTTPS协议的集群：需要身份认证（如果密码包含"!"等特殊字符，请使用单引号包裹，例如："-u 'admin':'Pass!word'"），且需要忽略SSL证书校验。
     ```
     curl -X PUT -k -u <user>:<password> "https://<host>:<port>/my_store" \
          -H 'Content-Type: application/json' \
          -d '{
            "settings": {
              "number_of_shards": 1
            },
            "mappings": {
              "properties": {
                "productName": {
                  "type": "text"
                },
                "size": {
                  "type": "keyword"
                }
              }
            }
          }'
     ```
     
    
   Elasticsearch版本小于7.x的代码示例如下。
   - 非安全模式的集群（HTTP）：无需认证，直接访问。
     ```
     curl -X PUT "http://<host>:<port>/my_store" \
          -H 'Content-Type: application/json' \
          -d '{
            "settings": {
              "number_of_shards": 1
            },
            "mappings": {
              "products": {
                "properties": {
                  "productName": {
                    "type": "text"
                  },
                  "size": {
                    "type": "keyword"
                  }
                }
              }
            }
          }'
     ```
     
   
   - 安全模式+HTTP协议的集群：需要身份认证（如果密码包含"!"等特殊字符，请使用单引号包裹，例如："-u 'admin':'Pass!word'"）。
     ```
     curl -X PUT -u <user>:<password> "http://<host>:<port>/my_store" \
          -H 'Content-Type: application/json' \
          -d '{
            "settings": {
              "number_of_shards": 1
            },
            "mappings": {
              "products": {
                "properties": {
                  "productName": {
                    "type": "text"
                  },
                  "size": {
                    "type": "keyword"
                  }
                }
              }
            }
          }'
     ```
     
   
   - 安全模式+HTTPS协议的集群：需要身份认证（如果密码包含"!"等特殊字符，请使用单引号包裹，例如："-u 'admin':'Pass!word'"），且需要忽略SSL证书校验。
     ```
     curl -X PUT -k -u <user>:<password> "https://<host>:<port>/my_store" \
          -H 'Content-Type: application/json' \
          -d '{
            "settings": {
              "number_of_shards": 1
            },
            "mappings": {
              "products": {
                "properties": {
                  "productName": {
                    "type": "text"
                  },
                  "size": {
                    "type": "keyword"
                  }
                }
              }
            }
          }'
     ```
     
     
6. 执行导入命令，将JSON数据文件导入到Elasticsearch集群。 在ECS存放JSON数据文件的路径下，执行如下命令导入数据。
   - 非安全模式的集群（HTTP）：无需认证，直接访问。
     ```
     curl -X POST "http://<host>:<port>/_bulk" \
          -H 'Content-Type: application/json' \
          --data-binary @test.json
     ```
     
   
   - 安全模式+HTTP协议的集群：需要身份认证（如果密码包含"!"等特殊字符，请使用单引号包裹，例如："-u 'admin':'Pass!word'"）。
     ```
     curl -X POST -u <user>:<password> "http://<host>:<port>/_bulk" \
          -H 'Content-Type: application/json' \
          --data-binary @test.json
     ```
     
   
   - 安全模式+HTTPS协议的集群：需要身份认证（如果密码包含"!"等特殊字符，请使用单引号包裹，例如："-u 'admin':'Pass!word'"），且需要忽略SSL证书校验。
     ```
     curl -X POST -k -u <user>:<password> "https://<host>:<port>/_bulk" \
          -H 'Content-Type: application/json' \
          --data-binary @test.json
     ```
     
   
   
   ![](https://support.huaweicloud.com/usermanual-css/public_sys-resources/note_3.0-zh-cn.png)
   当访问节点出现故障时，会导致命令执行失败，如果集群包含多个节点，可以将\<host\>替换为另一个节点的IP地址，如果集群只有一个节点，则需要等待节点修复之后再执行命令导入数据。
   
7. 结果验证。
   导入成功后，会返回如下信息：
   ```
   {"took":204, "errors":false, "items":[...]}
   ```
   "errors"为"false"则表示全部导入成功。
   
 
