# How to change dynamically the index name in "saveJsonToES"?

**URL:** <https://discuss.elastic.co/t/how-to-change-dynamically-the-index-name-in-savejsontoes/66866>\
**Category:** Elasticsearch\
**Created:** [November 22, 2016, 2:30pm UTC](https://discuss.elastic.co/t/how-to-change-dynamically-the-index-name-in-savejsontoes/66866 "2016-11-22T14:30:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [November 22, 2016, 2:30pm UTC](https://discuss.elastic.co/t/how-to-change-dynamically-the-index-name-in-savejsontoes/66866/1 "2016-11-22T14:30:17Z")

</div>

Hello Folks,

I am trying to insert logs that I extract from a kafka server in order to insert in ElasticSearc 5.0.0 with Spark Streaming 2.0.0 .

Here is my code. My big problem is with line "saveJsonToES", in fact, this function has a string argument for specifying the index name. However, my index name is a JavaDStream. I did like this in oder to generate dynamic index names in another class.

```
JavaDStream<List<String>> newLines = lines.map(arg0 -> {    

    String lineToInsertInES = "";
    String indexName = "";
    List<String> list = new ArrayList<String>();

    //some code to determine strings to add in my list

    list.add(lineToInsertInES);
    list.add(indexName);
    return list;
});

JavaDStream<String> lineToInsertInES = newLines.map(list -> list.get(0));
JavaDStream<String> indexName = newLines.map(list -> list.get(1));

lineToInsertInES.foreachRDD(line->{

    if(!line.isEmpty())
        JavaEsSpark.saveJsonToEs(line,indexName); //problem at this line
});

```

Can you telle how I can solve this ?

Thank you in advance

Y

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 23, 2016, 10:20am UTC](https://discuss.elastic.co/t/how-to-change-dynamically-the-index-name-in-savejsontoes/66866/2 "2016-11-23T10:20:43Z")

</div>

Sounds more like a Spark than an Elasticsearch question to me. What prevents you from converting the JavaDStream to a regular String before passing it as an argument?

---

<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:** [December 21, 2016, 10:21am UTC](https://discuss.elastic.co/t/how-to-change-dynamically-the-index-name-in-savejsontoes/66866/3 "2016-12-21T10:21:15Z")

</div>

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