Adding data to Elasticsearch

Posted by
  1. Install Elasticsearch on Linux
  2. Adding data to Elasticsearch

In the first part of the series we installed Elasticsearch. Now its time to fill that Elasticsearch instance with some data.

As mentioned before we are going to store FX buy / sell data in elastic. Before doing this we need to determine a index and a type of our data. For this example I use “banking” as my index and “fx” as type.  In this example we also use curl to post JSON data to elastic.

Adding data to Elasticsearch using Curl

The format looks like this. As you can see you can add the index and type to the uri and give a id. Use a unique id, otherwise the entry will be overwritten.

Check the entry with the following query.

rverhe@ 
/$ curl 
index" 
• "banking", 
" _ type 
" id" 
version" 
seq_no" 
term" 
_ primary 
"found" 
true, 
source" • 
"type 
"ticker" 
: "xauusd", 
"buy" 
. 1.23, 
"target" 
. 1.25, 
"stop" 
way" • "long" 
-X GET 
http://localhost : 9288/banking/fx/1 ?pretty

Adding data to Elasticsearch using Python

Python is a great scripting language to bulk import data to your Elastic instance. Scripting against Elasticsearch with python is made very easy with the already scripted Python module Elasticsearch. The module is basically a REST api parser. Very basic example script is added below.

Create a json file wich you want to import in Elastic

Install the python module requirements

The example script

Leave a Reply

Your email address will not be published. Required fields are marked *