# Indexing AVRO Documents

**URL:** <https://discuss.elastic.co/t/indexing-avro-documents/11583>\
**Category:** Elasticsearch\
**Created:** [April 15, 2013, 9:40am UTC](https://discuss.elastic.co/t/indexing-avro-documents/11583 "2013-04-15T09:40:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuwar\_sahani\_2](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@kuwar\_sahani\_2](https://discuss.elastic.co/u/kuwar_sahani_2)\
**Post date:** [April 15, 2013, 9:40am UTC](https://discuss.elastic.co/t/indexing-avro-documents/11583/1 "2013-04-15T09:40:59Z")

</div>

Hi

I have a code to read AVRO that iterates over records and builds the XContentBuilder  
object from it and indexes it.  
I have introduced a code for Auto-Classification that accepts the record as  
XContentBuilder.string() and retruns a String with Catagories in it.

XContentBuilder jBuilder = _jsonBuilder_().startObject();

jBuilder.field("data",input);  
String s=a.addCategory(jBuilder.string());  
jBuilder.field("tags", s);// line 3  
Now I am trying to add the catagories to XContentBuilder object (line 3)  
before indexing it but have the following exception thrown:

org.elasticsearch.common.jackson.core.JsonGenerationException: Can not  
write a field name, expecting a value  
how do i resolve this issue?

Thanks

Kuwar Sahani

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 15, 2013, 9:54am UTC](https://discuss.elastic.co/t/indexing-avro-documents/11583/2 "2013-04-15T09:54:43Z")

</div>

Can you post all your code?  
I mean that I don't see any endObject() method call.  
What is the content of s? Could it be null?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 15 avr. 2013 à 11:40, kuwar sahani [kuwarsahani@gmail.com](mailto:kuwarsahani@gmail.com) a écrit :

> Hi
> 
> I have a code to read AVRO that iterates over records and builds the XContentBuilder object from it and indexes it.  
> I have introduced a code for Auto-Classification that accepts the record as XContentBuilder.string() and retruns a String with Catagories in it.
> 
> XContentBuilder jBuilder = jsonBuilder().startObject();
> 
> jBuilder.field("data",input);  
> String s=a.addCategory(jBuilder.string());  
> jBuilder.field("tags", s);// line 3
> 
> Now I am trying to add the catagories to XContentBuilder object (line 3) before indexing it but have the following exception thrown:
> 
> org.elasticsearch.common.jackson.core.JsonGenerationException: Can not write a field name, expecting a value  
> how do i resolve this issue?
> 
> Thanks
> 
> Kuwar Sahani
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![kuwar\_sahani\_2](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@kuwar\_sahani\_2](https://discuss.elastic.co/u/kuwar_sahani_2)\
**Post date:** [April 15, 2013, 10:04am UTC](https://discuss.elastic.co/t/indexing-avro-documents/11583/3 "2013-04-15T10:04:23Z")

</div>

hi  
unable to attach a file somehow.  
writing a demo code.

public class AutoClassification {

public String addCategory(String input) {

String next;  
List categories = new ArrayList();  
try{  
BufferedReader inputReader = new BufferedReader(new FileReader(  
"D:/kuwar/w2/Auto Classification/Dictionary.txt"));  
while((next=inputReader.readLine())!=null){  
if(input.contains(next)){  
categories.add("""+next+""");  
}

}  
}  
catch (Exception e) {

}

return categories.toString();  
}  
public static void main(String args) throws IOException {  
AutoClassification a= new AutoClassification();  
XContentBuilder jBuilder = jsonBuilder().startObject();  
String input="XYZ is an IT company.it has launched the product in BIG  
DATA Space";  
jBuilder.field("data",input);  
String s=a.addCategory(jBuilder.bytesStream().toString());  
System.out.println(s);  
//s =["IT","XYZ"]  
//the error is thrown here:  
jBuilder.field("tags", s);  
jBuilder.endObject();  
System.out.println(jBuilder.string());  
}  
}

On Monday, 15 April 2013 15:24:43 UTC+5:30, David Pilato wrote:

> Can you post all your code?  
> I mean that I don't see any endObject() method call.  
> What is the content of s? Could it be null?
> 
> ```
> -- 
> 
> ```
> 
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 15 avr. 2013 à 11:40, kuwar sahani \<[kuwar...@gmail.com](mailto:kuwar...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> Hi
> 
> I have a code to read AVRO that iterates over records and builds the XContentBuilder  
> object from it and indexes it.  
> I have introduced a code for Auto-Classification that accepts the record  
> as XContentBuilder.string() and retruns a String with Catagories in it.
> 
> XContentBuilder jBuilder = _jsonBuilder_().startObject();
> 
> jBuilder.field("data",input);  
> String s=a.addCategory(jBuilder.string());  
> jBuilder.field("tags", s);// line 3  
> Now I am trying to add the catagories to XContentBuilder object (line 3)  
> before indexing it but have the following exception thrown:
> 
> org.elasticsearch.common.jackson.core.JsonGenerationException: Can not  
> write a field name, expecting a value  
> how do i resolve this issue?
> 
> Thanks
> 
> Kuwar Sahani
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 15, 2013, 11:17am UTC](https://discuss.elastic.co/t/indexing-avro-documents/11583/4 "2013-04-15T11:17:18Z")

</div>

Use GIST to post your code.

If you need to add an array to your json (using jsonbuilder), use startArray() and endArray() instead of try to build a String like "["IT","XYZ"]" and put that in a tag.

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 15 avr. 2013 à 12:04, kuwar sahani [kuwarsahani@gmail.com](mailto:kuwarsahani@gmail.com) a écrit :

> hi  
> unable to attach a file somehow.  
> writing a demo code.
> 
> public class AutoClassification {
> 
> public String addCategory(String input) {
> 
> String next;  
> List categories = new ArrayList();  
> try{  
> BufferedReader inputReader = new BufferedReader(new FileReader(  
> "D:/kuwar/w2/Auto Classification/Dictionary.txt"));  
> while((next=inputReader.readLine())!=null){  
> if(input.contains(next)){  
> categories.add("""+next+""");  
> }
> 
> }  
> }  
> catch (Exception e) {
> 
> }
> 
> return categories.toString();  
> }  
> public static void main(String args) throws IOException {  
> AutoClassification a= new AutoClassification();  
> XContentBuilder jBuilder = jsonBuilder().startObject();  
> String input="XYZ is an IT company.it has launched the product in BIG DATA Space";  
> jBuilder.field("data",input);  
> String s=a.addCategory(jBuilder.bytesStream().toString());  
> System.out.println(s);  
> //s =["IT","XYZ"]  
> //the error is thrown here:  
> jBuilder.field("tags", s);  
> jBuilder.endObject();  
> System.out.println(jBuilder.string());  
> }  
> }
> 
> On Monday, 15 April 2013 15:24:43 UTC+5:30, David Pilato wrote:  
> Can you post all your code?  
> I mean that I don't see any endObject() method call.  
> What is the content of s? Could it be null?
> 
> --  
> David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
> @dadoonet | @elasticsearchfr | @scrutmydocs
> 
> Le 15 avr. 2013 à 11:40, kuwar sahani [kuwar...@gmail.com](mailto:kuwar...@gmail.com) a écrit :
> 
> > Hi
> > 
> > I have a code to read AVRO that iterates over records and builds the XContentBuilder object from it and indexes it.  
> > I have introduced a code for Auto-Classification that accepts the record as XContentBuilder.string() and retruns a String with Catagories in it.
> > 
> > XContentBuilder jBuilder = jsonBuilder().startObject();
> > 
> > jBuilder.field("data",input);  
> > String s=a.addCategory(jBuilder.string());  
> > jBuilder.field("tags", s);// line 3
> > 
> > Now I am trying to add the catagories to XContentBuilder object (line 3) before indexing it but have the following exception thrown:
> > 
> > org.elasticsearch.common.jackson.core.JsonGenerationException: Can not write a field name, expecting a value  
> > how do i resolve this issue?
> > 
> > Thanks
> > 
> > Kuwar Sahani
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![kuwar\_sahani\_2](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@kuwar\_sahani\_2](https://discuss.elastic.co/u/kuwar_sahani_2)\
**Post date:** [April 15, 2013, 11:56am UTC](https://discuss.elastic.co/t/indexing-avro-documents/11583/5 "2013-04-15T11:56:04Z")

</div>

once you call the .string() method of an XContentBuilder object, it does  
not allow to add another field to it, leave aside the data type to be added.

On Monday, 15 April 2013 16:47:18 UTC+5:30, David Pilato wrote:

> Use GIST to post your code.
> 
> If you need to add an array to your json (using jsonbuilder), use  
> startArray() and endArray() instead of try to build a String like  
> "["IT","XYZ"]" and put that in a tag.
> 
> ```
> -- 
> 
> ```
> 
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 15 avr. 2013 à 12:04, kuwar sahani \<[kuwar...@gmail.com](mailto:kuwar...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> hi  
> unable to attach a file somehow.  
> writing a demo code.
> 
> public class AutoClassification {
> 
> public String addCategory(String input) {
> 
> String next;  
> List categories = new ArrayList();  
> try{  
> BufferedReader inputReader = new BufferedReader(new FileReader(  
> "D:/kuwar/w2/Auto Classification/Dictionary.txt"));  
> while((next=inputReader.readLine())!=null){  
> if(input.contains(next)){  
> categories.add("""+next+""");  
> }
> 
> }  
> }  
> catch (Exception e) {
> 
> }
> 
> return categories.toString();  
> }  
> public static void main(String args) throws IOException {  
> AutoClassification a= new AutoClassification();  
> XContentBuilder jBuilder = jsonBuilder().startObject();  
> String input="XYZ is an IT company.it has launched the product in BIG  
> DATA Space";  
> jBuilder.field("data",input);  
> String s=a.addCategory(jBuilder.bytesStream().toString());  
> System.out.println(s);  
> //s =["IT","XYZ"]  
> //the error is thrown here:  
> jBuilder.field("tags", s);  
> jBuilder.endObject();  
> System.out.println(jBuilder.string());  
> }  
> }
> 
> On Monday, 15 April 2013 15:24:43 UTC+5:30, David Pilato wrote:
> 
> > Can you post all your code?  
> > I mean that I don't see any endObject() method call.  
> > What is the content of s? Could it be null?
> > 
> > ```
> > -- 
> > 
> > ```
> > 
> > _David Pilato_ | _Technical Advocate_ | \*[Elasticsearch.com](http://Elasticsearch.com)[http://elasticsearch.com/](http://elasticsearch.com/)  
> > \*  
> > @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> > | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> > 
> > Le 15 avr. 2013 à 11:40, kuwar sahani [kuwar...@gmail.com](mailto:kuwar...@gmail.com) a écrit :
> > 
> > Hi
> > 
> > I have a code to read AVRO that iterates over records and builds the XContentBuilder  
> > object from it and indexes it.  
> > I have introduced a code for Auto-Classification that accepts the record  
> > as XContentBuilder.string() and retruns a String with Catagories in it.
> > 
> > XContentBuilder jBuilder = _jsonBuilder_().startObject();
> > 
> > jBuilder.field("data",input);  
> > String s=a.addCategory(jBuilder.string());  
> > jBuilder.field("tags", s);// line 3  
> > Now I am trying to add the catagories to XContentBuilder object (line 3)  
> > before indexing it but have the following exception thrown:
> > 
> > org.elasticsearch.common.jackson.core.JsonGenerationException: Can not  
> > write a field name, expecting a value  
> > how do i resolve this issue?
> > 
> > Thanks
> > 
> > Kuwar Sahani
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 2:41am UTC](https://discuss.elastic.co/t/indexing-avro-documents/11583/6 "2017-07-06T02:41:04Z")

</div>


