# Bulk insert to elasticsearch in spark using scala

**URL:** https://discuss.elastic.co/t/bulk-insert-to-elasticsearch-in-spark-using-scala/76573
**Category:** Elasticsearch
**Tags:** es-hadoop
**Created:** [February 27, 2017, 5:29am UTC](https://discuss.elastic.co/t/bulk-insert-to-elasticsearch-in-spark-using-scala/76573 "2017-02-27T05:29:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rulanitee](https://avatars.discourse-cdn.com/v4/letter/r/b5e925/32.png) [@rulanitee](https://discuss.elastic.co/u/rulanitee)
#### Post date: [February 27, 2017, 5:29am UTC](https://discuss.elastic.co/t/bulk-insert-to-elasticsearch-in-spark-using-scala/76573/1 "2017-02-27T05:29:12Z")

</div>

Hello there,

Is there a way to save the following Json in spark using scala:

{"index":{"\_id":1662034,"\_type":"fhl","\_index":"loan"}}  
{"name":"John","surname":"Doe","dob":"1985-01-03","title":"Mr", per\_id:1662034}  
{"index":{"\_id":1662035,"\_type":"fhl","\_index":"loan"}}  
{"name":"Foo","surname":"Fooo","dob":"1980-02-14","title":"Mrs", per\_id:1662035}

If this is not possible, kindly asking on how i can do bulk inserts to Elasticsearch in spark using scala?

Regards

---

<div class="post-metadata">

### Author: ![Muthu\_Jayakumar](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@Muthu\_Jayakumar](https://discuss.elastic.co/u/Muthu_Jayakumar)
#### Post date: [February 27, 2017, 3:18pm UTC](https://discuss.elastic.co/t/bulk-insert-to-elasticsearch-in-spark-using-scala/76573/2 "2017-02-27T15:18:23Z")

</div>

Hello Rulanitee,

> Is there a way to save the following Json in spark using scala

Apache Spark has [DataFrame/Dataset APIs](https://spark.apache.org/docs/latest/sql-programming-guide.html#json-datasets) to save JSON. RDD based APIs may exist as well.

> how i can do bulk inserts to Elasticsearch in spark using scala?

I believe when you read a JSON or any datasource into Spark, the write operation into Elasticsearch may be like bulk insert (to some extent). As every executor with-in the spark cluster is going to run the `saveToEs` action in parallel.  
If you would like to use non-spark APIs to perform a bulk insert, I have used akka-http for this. Not sure if this may be an overkill for you, as akka-streams may have a bit of a learning curve.

Hope this helps.

Thanks,  
Muthu

---

<div class="post-metadata">

### Author: ![rulanitee](https://avatars.discourse-cdn.com/v4/letter/r/b5e925/32.png) [@rulanitee](https://discuss.elastic.co/u/rulanitee)
#### Post date: [February 27, 2017, 5:18pm UTC](https://discuss.elastic.co/t/bulk-insert-to-elasticsearch-in-spark-using-scala/76573/3 "2017-02-27T17:18:38Z")

</div>

Hi,

Thanks for the tip. Will research on the akka-http.

Regards

---

<div class="post-metadata">

### Author: ![rulanitee](https://avatars.discourse-cdn.com/v4/letter/r/b5e925/32.png) [@rulanitee](https://discuss.elastic.co/u/rulanitee)
#### Post date: [February 28, 2017, 9:23am UTC](https://discuss.elastic.co/t/bulk-insert-to-elasticsearch-in-spark-using-scala/76573/4 "2017-02-28T09:23:03Z")

</div>

Hi Muthu

Apparently the answer was right in front of me. From the [documentation](https://www.elastic.co/guide/en/elasticsearch/hadoop/current/spark.html) as shown below

val json1 = """{"reason" : "business", "airport" : "SFO"}"""  
val json2 = """{"participants" : 5, "airport" : "OTP"}"""

new SparkContext(conf).makeRDD(Seq(json1, json2))  
.saveJsonToEs("spark/json-trips")

Just add json string to an array and push to ES.

So if one has a class or object, using [gson](https://github.com/google/gson) or any other json framework, cast the object to json string and add it to an array or list.

Hope this helps someone ...

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 28, 2017, 9:23am UTC](https://discuss.elastic.co/t/bulk-insert-to-elasticsearch-in-spark-using-scala/76573/5 "2017-03-28T09:23:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
