During application development, debugging, or small-scale data migration, developers frequently need to quickly write local JSON files, such as test datasets and configuration files, to an OpenSearch cluster. Compared with data migration tools such as Logstash and CDM, which are complex to configure, open-source OpenSearch APIs (such as the _bulk API) provide a more flexible and lightweight alternative, especially for ad hoc tasks. You can write data through OpenSearch Dashboards in a highly interactive manner, or run cURL commands on an ECS for batch ingestion. Both methods enable agile and efficient data ingestion into cloud-based OpenSearch clusters.
On OpenSearch Dashboards, you can run POST commands to import data using an open-source OpenSearch API.
Applicable scenarios: development and debugging, small ad-hoc data writes, and syntax verification.
The left part of the console is the command input box, and the triangle icon in its upper-right corner is the execution button. The right part shows the execution result.
For example, run the following command to create index my_store:
PUT /my_store{"settings": {"number_of_shards": 1},"mappings": {"properties": {"productName": {"type": "text"},"size": {"type": "keyword"}}}}
For example, run the following command to write a record to the my_store index:
POST /my_store/_bulk{"index":{}}{"productName":"Latest art shirts for women in 2017 autumn","size":"L"}
Check the result. If the value of errors is false and the value of result in each record in the items array is created, all records are successfully written.
On an ECS server, you can run cURL commands to import files via an open-source OpenSearch API.
Applicable scenarios: batch ingestion of local JSON files using an automation script.
If the cluster has client nodes, only the IP addresses and ports of all the client nodes are displayed. Otherwise, the IP addresses and ports of all data nodes and cold data nodes are displayed.
For example, save the following data as a test.json file and upload it to the ECS:
For example, run the following command to create index my_store:
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"}}}}'
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"}}}}'
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"}}}}'
Run the following command in the ECS directory that stores the JSON file:
curl -X POST "http://<host>:<port>/_bulk" \-H 'Content-Type: application/json' \--data-binary @test.json
curl -X POST -u <user>:<password> "http://<host>:<port>/_bulk" \-H 'Content-Type: application/json' \--data-binary @test.json
curl -X POST -k -u <user>:<password> "https://<host>:<port>/_bulk" \-H 'Content-Type: application/json' \--data-binary @test.json
If the designated cluster node is unavailable, the command will fail. If the cluster contains multiple nodes, replace <host> with the IP address of another node. If the cluster contains only one node, you have to wait until the node recovers.
After data ingestion, the following information is returned:
If the value of errors is false, all data has been successfully ingested.