| by Arround The Web | No comments

How to Store Data in Elasticsearch?

Elasticsearch is one of the best choices to analyze and preserve data. It can store various kinds of data such as geospatial data, numeric data, bulky and raw data, and semi-structured data. It usually stores data in documents that are further sterilized in JSON format. The stored data can be easily searched, updated, and deleted with the help of Rest APIs.

This post will demonstrate how to store data in Elasticsearch.

How to Store Data in Elasticsearch?

In Elasticsearch, data is usually stored in Documents in JSON format. Then, these documents can be managed by indexes just like SQL databases. To store the data in Elasticsearch, go through the following instructions.

Step 1: Start Elasticsearch Engine

First, run the Elasticsearch database or engine. To do so, navigate to the “bin” folder of Elasticsearch using the “cd” command:

cd C:\Users\Dell\Documents\Elk stack\elasticsearch-8.7.0\bin

 

Then, run the batch file of Elasticsearch to start the Elasticsearch engine:

elasticsearch.bat

 

Step 2: Access Elasticsearch From Command Line

When the engine is started, utilize the “curl” command along with the default Elasticsearch URL “localhost:9200” and login credentials to access Elasticsearch from the command line:

curl localhost:9200 -u elastic:<user-password>

 

Step 3: Create Index

Next, create a new index using the “PUT” API along with Elasticsearch default URL and index name. For instance, we have created the “employee-data” index:

curl -XPUT "http://localhost:9200/employee-data" -H "kbn-xsrf: reporting" -u elastic:<user-password>

 

Step 4: Store Data in Elasticsearch Index

Now, store the data using a “POST” API request. It will carry a complete field structure and store the data in the specified location. For demonstration check out the below command:

curl -XPOST -H "Content-type:application/json" --data-binary "{"add-field": {"Name":"Rafia", "DOB":"19-11-1997","stored":true}}" http://localhost:9200/employee-data/_doc/1/pretty -u elastic:<user-password>

 

In the above command:

  • “–data-binary” is used to store binary data.
  • add-field” is used to specify the document field. For instance, we are specifying the “Name”, “DOB” and “stored” fields:
  • This data will be stored in the “employee-data” index in the “_doc” document format and assigning it an “id” as “1”:

Step 5: Verification

For verification, check whether the data is stored or not. For this purpose, utilize the “GET” API request to access or retrieve the data from Elasticsearch:

curl -X GET "localhost:9200/employee-data/_doc/1?pretty" -u elastic:<user-password>

 

How to Store Data in Elasticsearch Using Kibana?

Kibana is a visualization tool for Elasticsearch that represents the data in the form of Pie-chart, histograms, or line graphs. It also makes it easy to execute the APIs and saves the user from using complex command structures as used in the above section.

To use Kibana to store data in Elasticsearch, go through the following steps.

Step 1: Start Kibana

First, run the Kibana on the system by visiting its “bin” directory through the “cd” command:

cd C:\Users\Dell\Documents\Elk stack\kibana-8.7.0\bin

 

After that, run the batch file of Kibana using the below command:

kibana.bat

 

Step 2: Sign in to Kibana

When the Kibana is started properly, navigate to the “localhost:5601” URL to access the Kibana user interface from the browser. After that, provide the Elasticsearch username and password to sign in to Kibana:

Step 3: Create Index

Create a new index using the “PUT” API request along with the index name:

PUT /employee-data

 

The output shows that we have successfully created the “employee-data” index:

Step 4: Insert Data in Document

Utilize the “POST” API to store the data in the newly created index. Also, specify the data in JSON format as shown below:

POST /employee-data/_doc/1?pretty
{
  "Name":"Rafia",
  "DOB":"19-NOV-1997",
  "stored":true
}

 

Step 5: Verification

For verification, retrieve the data from the “employee-data” index using the “GET” API:

GET /employee-data/_doc/1?pretty

 

The below output indicates that we have successfully stored and accessed the data in Elasticsearch:

That is all about storing the data in Elasticsearch.

Conclusion

To store the data in Elasticsearch, first, start the Elasticsearch database. After that, create a new index using the “PUT” API request along with the index name. After that, store the data with the help of the “POST” API. It will carry a complete field structure, data, and the specified location where data is going to store. In this blog, we have demonstrated how to store data in Elasticsearch.

Share Button

Source: linuxhint.com

Leave a Reply