Created
August 28, 2015 21:40
-
-
Save hhimanshu/0be538bc089bc259736f to your computer and use it in GitHub Desktop.
Insert JSON document into ElasticSearch using Elastic4s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependency: elastic4s | |
import java.util.Calendar | |
import com.sksamuel.elastic4s.ElasticDsl._ | |
import com.sksamuel.elastic4s.source.StringDocumentSource | |
import com.sksamuel.elastic4s.{ElasticClient, ElasticsearchClientUri} | |
import org.elasticsearch.action.index.IndexResponse | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
import scala.util.{Failure, Success} | |
object Elastic { | |
println("Creating Elastic Connection") | |
// todo (harit): ElasticSearch Configuration should be configurable | |
val uri = ElasticsearchClientUri("elasticsearch://192.168.99.100:9300") | |
val client = ElasticClient.remote(uri) | |
def insert(monitorJson: String) = { | |
client execute { | |
index into "test" -> "events" doc StringDocumentSource(monitorJson) | |
} | |
} | |
} | |
object Test extends App { | |
insertJson | |
def insertJson: IndexedSeq[Unit] = { | |
for (_ <- 1 to 100) yield { | |
val today = Calendar.getInstance() getTimeInMillis | |
val json: String = s"""{"time": ${today}}""" | |
val insert: Future[IndexResponse] = Elastic.insert(json) | |
insert onComplete { | |
case Success(response) => println("inserted successfully: {}", response) | |
case Failure(reason) => println("Insert failed: {}", reason) | |
} | |
} | |
} | |
} |
I think it is JsonDocumentSource with elastic4s 2.4.1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
com.sksamuel.elastic4s.source.StringDocumentSource, source not resolved - please update the code or let me know how to resolve it