forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Zheng, Xiu <zhengxiu@huawei.com> Co-committed-by: Zheng, Xiu <zhengxiu@huawei.com>
146 lines
13 KiB
HTML
146 lines
13 KiB
HTML
<a name="css_01_0024"></a><a name="css_01_0024"></a>
|
|
|
|
<h1 class="topictitle1">Using Kibana or APIs to Import Data to Elasticsearch</h1>
|
|
<div id="body0000001334459437"><p id="css_01_0024__p6252043134616">You can import data in various formats, such as JSON and CSV, to Elasticsearch in <span id="css_01_0024__text18508134549">CSS</span> by using Kibana or APIs.</p>
|
|
<div class="section" id="css_01_0024__section1430231820400"><h4 class="sectiontitle">Importing Data Using Kibana</h4><div class="p" id="css_01_0024__p2960851181211">Before importing data, ensure that you can use Kibana to access the cluster. The following procedure illustrates how to use the <strong id="css_01_0024__b5736132965511">POST</strong> command to import data.<ol id="css_01_0024__ol2068013371496"><li id="css_01_0024__li1865174111515">Log in to the CSS management console.</li><li id="css_01_0024__li1274935516811">Locate the target cluster and click <strong id="css_01_0024__b177336332213">Access Kibana</strong> in the <strong id="css_01_0024__b145513371926">Operation</strong> column to log in to Kibana.</li><li id="css_01_0024__li927171291011">Click <strong id="css_01_0024__b119441642420">Dev Tools</strong> in the navigation tree on the left.</li><li id="css_01_0024__li1718421717426">(Optional) On the <strong id="css_01_0024__b1017125719311">Console</strong> page, run the related command to create an index for the data to be stored and specify a custom mapping to define the data type:<p id="css_01_0024__p1898618490556">If there is an available index in the cluster where you want to import data, skip this step. If there is no available index, create an index by referring to the following sample code.</p>
|
|
<p id="css_01_0024__p366432292117">For example, on the <strong id="css_01_0024__b132856482522">Console</strong> page of Kibana, run the following command to create an index named <span class="parmname" id="css_01_0024__parmname187471955185217"><b>my_store</b></span> and specify a user-defined mapping to define the data type:</p>
|
|
<div class="p" id="css_01_0024__p7372555996">Versions earlier than 7.<em id="css_01_0024__i923801512583">x</em><pre class="screen" id="css_01_0024__screen18381131718811">PUT /my_store
|
|
{
|
|
"settings": {
|
|
"number_of_shards": 1
|
|
},
|
|
"mappings": {
|
|
"products": {
|
|
"properties": {
|
|
"productName": {
|
|
"type": "text"
|
|
},
|
|
"size": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<p id="css_01_0024__p118001898274">Versions later than 7.<em id="css_01_0024__i101648257582">x</em></p>
|
|
<pre class="screen" id="css_01_0024__screen1181163818278">PUT /my_store
|
|
{
|
|
"settings": {
|
|
"number_of_shards": 1
|
|
},
|
|
"mappings": {
|
|
"properties": {
|
|
"productName": {
|
|
"type": "text"
|
|
},
|
|
"size": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
}
|
|
}</pre>
|
|
</li><li id="css_01_0024__li599201141619">Run commands to import data. For example, run the following command to import a piece of data:<div class="p" id="css_01_0024__p12561163415309"><a name="css_01_0024__li599201141619"></a><a name="li599201141619"></a>Versions earlier than 7.<em id="css_01_0024__i467082815588">x</em><pre class="screen" id="css_01_0024__screen7634202523011">POST /my_store/products/_bulk
|
|
{"index":{}}
|
|
{"productName":"Latest art shirts for women in 2017 autumn","size":"L"}</pre>
|
|
</div>
|
|
<p id="css_01_0024__p8754193443011">Versions later than 7.<em id="css_01_0024__i0619113055813">x</em></p>
|
|
<pre class="screen" id="css_01_0024__screen184976293110">POST /my_store/_bulk
|
|
{"index":{}}
|
|
{"productName":"Latest art shirts for women in 2017 autumn","size":"L"}</pre>
|
|
<p id="css_01_0024__p116351525183015">The command output is similar to that shown in <a href="#css_01_0024__fig86351225133018">Figure 1</a>. If the value of the <span class="parmname" id="css_01_0024__parmname19219201981614"><b>errors</b></span> field in the result is <span class="parmvalue" id="css_01_0024__parmvalue1321951912167"><b>false</b></span>, the data is successfully imported.</p>
|
|
<div class="fignone" id="css_01_0024__fig86351225133018"><a name="css_01_0024__fig86351225133018"></a><a name="fig86351225133018"></a><span class="figcap"><b>Figure 1 </b>Response message</span><br><span><img id="css_01_0024__image19635132563018" src="en-us_image_0000001525365841.png"></span></div>
|
|
</li></ol>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="css_01_0024__section239718062912"><h4 class="sectiontitle">Importing Data Using APIs</h4><p id="css_01_0024__p853221885917">The following procedure illustrates how you can import a JSON data file using bulk APIs through the cURL command.</p>
|
|
<div class="note" id="css_01_0024__note426754811453"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="css_01_0024__p52671248154517">You are advised to import a file smaller than 50 MB.</p>
|
|
</div></div>
|
|
<ol id="css_01_0024__ol4678613573"><li id="css_01_0024__li1681812204612">Log in to the ECS that you use to access the cluster.<p id="css_01_0024__p153923349313"><a name="css_01_0024__li1681812204612"></a><a name="li1681812204612"></a>For details about how to access a cluster, see <a href="css_01_0012.html#css_01_0012__section16223134914582">Accessing a Cluster by Calling Elasticsearch APIs on the ECS That Is Located in the Same VPC as the Cluster</a>.</p>
|
|
</li><li id="css_01_0024__li74710325013">Run the following command to import JSON data:<div class="p" id="css_01_0024__p719713266457"><a name="css_01_0024__li74710325013"></a><a name="li74710325013"></a>In the command, replace the value of {<em id="css_01_0024__i1032298281165252">Private network address and port number of the node</em>} with the private network address and port number of a node in the cluster. If the node fails to work, the command will fail to be executed. If the cluster contains multiple nodes, you can replace the value of {<em id="css_01_0024__i85977054016532">Private network address and port number of the node</em>} with the private network address and port number of any available node in the cluster. If the cluster contains only one node, restore the node and execute the command again. <strong id="css_01_0024__b9412141571116">test.json</strong> indicates the JSON file whose data is to be imported.<pre class="screen" id="css_01_0024__screen14946155711418">curl -X PUT "http://{<em id="css_01_0024__i9475182012386">Private network address and port number of the node</em>} /_bulk" -H 'Content-Type: application/json' --data-binary @test.json</pre>
|
|
</div>
|
|
<p id="css_01_0024__p1126010231663">If communication encryption has been enabled on the cluster where you will import data, you need to send HTTPS requests and add <strong id="css_01_0024__b48412413319">-k</strong> to the cURL command.</p>
|
|
<pre class="screen" id="css_01_0024__screen950417302617">curl -X PUT -k "https://{<em id="css_01_0024__i15935126102415">Private network address and port number of the node</em>} /_bulk" -H 'Content-Type: application/json' --data-binary @test.json</pre>
|
|
<div class="note" id="css_01_0024__note3364193816247"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="css_01_0024__p1736511381240">The value of the <strong id="css_01_0024__b17149131694818">-X</strong> parameter is a command and that of the <strong id="css_01_0024__b10501972028">-H</strong> parameter is a message header. In the preceding command, <span class="parmvalue" id="css_01_0024__parmvalue17921732162610"><b>PUT</b></span> is the value of the <strong id="css_01_0024__b176912037423">-X</strong> parameter and <span class="parmvalue" id="css_01_0024__parmvalue19618205062612"><b>'Content-Type: application/json' --data-binary @test.json</b></span> is the value of the <strong id="css_01_0024__b123415541929">-H</strong> parameter. Do not add <strong id="css_01_0024__b2328411114912">-k</strong> between a parameter and its value.</p>
|
|
</div></div>
|
|
<p id="css_01_0024__p1441510225910"><strong id="css_01_0024__b460119369412">Example 1:</strong> In this example, assume that you need to import data in the <span class="filepath" id="css_01_0024__filepath175795113105214"><b>testdata.json</b></span> file to an Elasticsearch cluster, where communication encryption is disabled and the private network address and port number of one node are <span class="parmvalue" id="css_01_0024__parmvalue2112850572165410"><b>192.168.0.90</b></span> and <span class="parmvalue" id="css_01_0024__parmvalue1903804345105237"><b>9200</b></span> respectively. The data in the <strong id="css_01_0024__b84235270610530">testdata.json</strong> file is as follows:</p>
|
|
<p id="css_01_0024__p18342256084">Versions earlier than 7.<em id="css_01_0024__i20380171392">x</em></p>
|
|
<pre class="screen" id="css_01_0024__screen48576229156">{"index": {"_index":"my_store","_type":"products"}}
|
|
{"productName":"Autumn new woman blouses 2019","size":"M"}
|
|
{"index": {"_index":"my_store","_type":"products"}}
|
|
{"productName":"Autumn new woman blouses 2019","size":"L"}</pre>
|
|
<p id="css_01_0024__p12518231199">Versions later than 7.<em id="css_01_0024__i485444918">x</em></p>
|
|
<pre class="screen" id="css_01_0024__screen1636214191792">{"index": {"_index":"my_store"}}
|
|
{"productName":"Autumn new woman blouse 2019","size":"M"}
|
|
{"index": {"_index":"my_store"}}
|
|
{"productName":"Autumn new woman blouse 2019","size":"L"}</pre>
|
|
<p id="css_01_0024__p15237162919281">Perform the following steps to import the data:</p>
|
|
<ol type="a" id="css_01_0024__ol4472337122815"><li id="css_01_0024__li1992511339283">Run the following command to create an index named <strong id="css_01_0024__b12384245966">my_store</strong>:<div class="p" id="css_01_0024__p1256364413322">Versions earlier than 7.<em id="css_01_0024__i11362133245814">x</em><pre class="screen" id="css_01_0024__screen20693154120324">curl -X PUT http://192.168.0.90:9200/my_store -H 'Content-Type: application/json' -d '
|
|
{
|
|
"settings": {
|
|
"number_of_shards": 1
|
|
},
|
|
"mappings": {
|
|
"products": {
|
|
"properties": {
|
|
"productName": {
|
|
"type": "text"
|
|
},
|
|
"size": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}'</pre>
|
|
</div>
|
|
<p id="css_01_0024__p92116213314">Versions later than 7.<em id="css_01_0024__i261693465811">x</em></p>
|
|
<pre class="screen" id="css_01_0024__screen293919145336">curl -X PUT http://192.168.0.90:9200/my_store -H 'Content-Type: application/json' -d '
|
|
{
|
|
"settings": {
|
|
"number_of_shards": 1
|
|
},
|
|
"mappings": {
|
|
"properties": {
|
|
"productName": {
|
|
"type": "text"
|
|
},
|
|
"size": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
}
|
|
}'</pre>
|
|
</li><li id="css_01_0024__li63151641202810">Run the following command to import the data in the <strong id="css_01_0024__b996013821220">testdata.json</strong> file:<pre class="screen" id="css_01_0024__screen12455428121611">curl -X PUT "http://192.168.0.90:9200/_bulk" -H 'Content-Type: application/json' --data-binary @testdata.json</pre>
|
|
</li></ol>
|
|
<p id="css_01_0024__p89581835184219"><strong id="css_01_0024__b209581335114214">Example 2:</strong> In this example, assume that you need to import data in the <span class="filepath" id="css_01_0024__filepath5959183510422"><b>testdata.json</b></span> file to an Elasticsearch cluster, where communication encryption has been enabled and the node access address and content in the <strong id="css_01_0024__b20896339203713">testdata.json</strong> are the same as those in example 1. Perform the following steps to import the data:</p>
|
|
<ol type="a" id="css_01_0024__ol59631635184211"><li id="css_01_0024__li15965163514220">Run the following command to create an index named <strong id="css_01_0024__b9488113385">my_store</strong>:<pre class="screen" id="css_01_0024__screen129651350429">curl -X PUT -k https://192.168.0.90:9200/my_store -H 'Content-Type: application/json' -d '
|
|
{
|
|
"settings": {
|
|
"number_of_shards": 1
|
|
},
|
|
"mappings": {
|
|
"products": {
|
|
"properties": {
|
|
"productName": {
|
|
"type": "text"
|
|
},
|
|
"size": {
|
|
"type": "keyword"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}'</pre>
|
|
</li><li id="css_01_0024__li169761035204215">Run the following command to import the data in the <strong id="css_01_0024__b1091226123819">testdata.json</strong> file:<pre class="screen" id="css_01_0024__screen1095155018491">curl -X PUT -k "https://192.168.0.90:9200/_bulk" -H 'Content-Type: application/json' --data-binary @testdata.json</pre>
|
|
</li></ol>
|
|
</li></ol>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="css_01_0045.html">Importing Data to Elasticsearch</a></div>
|
|
</div>
|
|
</div>
|
|
|