Tuesday, January 14, 2014

Elastic Search: Pushing data into Elastic Search from Windows PowerShell

This is a code sample on how to push simple data from PowerShell and into elastic search.  Note that $metrics can be just about anything, here it is a dictionary but you can use a PS custom object or even define your own class in an external assembly. The JSON serialization understands nesting so your data can be deeply structured.

Have fun! Post a comment if this was helpful.

I recommend using Chrome with the Sense plugin to see what landed in ES.

[Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$metrics = new-object 'System.Collections.Generic.Dictionary[string,object]'
$metrics.Add("foo",1)
$serializedMetric = new-object System.Text.StringBuilder
$javaScriptSerializer = new-object System.Web.Script.Serialization.JavaScriptSerializer
$javaScriptSerializer.Serialize($metrics, $serializedMetric);

$ESEndPoint = "http://localhost:9200"
$indexname = "my-index"
$typename = "mytype"
$id = [Guid]::NewGuid().ToString('n')
$indexedEndpoint = $ESEndPoint + "/" + $indexname + "/" + $typename + "/" + $id
$webClient = new-object System.net.WebClient
$webClient.UploadString($indexedEndpoint, $serializedMetric.ToString())

# Note: Retrieve from ES with GET /my-index/mytype/_all

No comments:

Post a Comment