Persist a Map of String to List using Gora to MongoDB

64 Views Asked by At

I am to trying to persist a POJO containing a Map of String to List to MongoDB using Gora. I am getting an exception.

java.lang.Exception: java.lang.RuntimeException: java.lang.IllegalStateException: Field ARRAY: To store a Gora 'array', target Mongo mapping have to be of 'list' type
    at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)
Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: Field ARRAY: To store a Gora 'array', target Mongo mapping have to be of 'list' type
    at org.apache.gora.mapreduce.GoraRecordWriter.write(GoraRecordWriter.java:76)
    at org.apache.hadoop.mapred.MapTask$NewDirectOutputCollector.write(MapTask.java:635)
    at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)
    at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112)
    at org.apache.nutch.parse.ParserJob$ParserMapper.map(ParserJob.java:147)
    at org.apache.nutch.parse.ParserJob$ParserMapper.map(ParserJob.java:87)
    at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:764)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:340)
    at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:243)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Field ARRAY: To store a Gora 'array', target Mongo mapping have to be of 'list' type
    at org.apache.gora.mongodb.store.MongoStore.toDBObject(MongoStore.java:747)
    at org.apache.gora.mongodb.store.MongoStore.mapToMongo(MongoStore.java:909)
    at org.apache.gora.mongodb.store.MongoStore.toDBObject(MongoStore.java:742)
    at org.apache.gora.mongodb.store.MongoStore.newUpdateSetInstance(MongoStore.java:689)
    at org.apache.gora.mongodb.store.MongoStore.performPut(MongoStore.java:349)
    at org.apache.gora.mongodb.store.MongoStore.put(MongoStore.java:326)
    at org.apache.gora.mongodb.store.MongoStore.put(MongoStore.java:70)
    at org.apache.gora.mapreduce.GoraRecordWriter.write(GoraRecordWriter.java:67)
    ... 14 more

My POJO is as follows

public class Host{
    Map<String, List<String>> outlinks;

    public Map<String, List<String>> getOutlinks() {
        return outlinks;
    }

    public void setOutlinks(Map<String, List<String>> outlinks) {
        this.outlinks = outlinks;
    }   
}

I have defined Avro schema as follows

{
  "name": "Host",
  "type": "record",
  "namespace": "com.mypackage",
  "doc": "Some information",
  "fields": [
    {
      "name": "outlinks",
      "type": {
        "type": "map",
        "values":{
            "type":"array",
            "items":"string"
        }
      },
      "default": null
    }    
  ]
}

I have defined gora-mongodb-mapping.xml as follows

<gora-otd>
    <class name="com.mypackage.Host" keyClass="java.lang.String" document="host">
        <field name="outlinks" docfield="outlinks" type="document"/>
    </class>
</gora-otd>

I am not able to understand what I am missing out here. Requests to please help. TIA

1

There are 1 best solutions below

1
Jeryl Cook On

I want to suggest you store the data correctly in mongoDB.. the ideal way to store this like..you will be able to search on outlink as well using a simple mongodb query. you don't need that xml file..

public class Host{
     Strin key;
     List<String> outlinks;

    public void setKey(String key) {
        this.key = key;
    }
    public String getKey() {
        return key;
    }

    public void setOutlinks(List<String> outlinks) {
        this.outlinks = outlinks;
    }   
    public List<String> getOutlinks(){
        return outlinks;
   }

}