I have got a complex type avro which is here, complex being because of
arrays being used in user/media

************************************************************
***********************
{
"namespace":"com.bph.model",
"type":"record",
"name": "FBPage",
"fields": [
    {"name": "streamId", "type": ["string", "null"]},
    {"name": "source", "type": ["string", "null"]},
    {"name": "sourceType", "type": ["string", "null"]},
    {"name": "referenceId", "type": ["string", "null"]},
    {"name": "referenceMessageUrl", "type": ["string", "null"]},
    {"name": "text", "type": ["string", "null"]},
    {"name": "createdAt", "type": ["string", "null"]},
    {"name":"user","type":{
          "type":"array",
        "items": {
            "namespace":"com.bph.model",
            "name":"FBUser",
            "type":"record",
            "fields": [
              {"name": "userId", "type": ["string", "null"] },
              {"name": "userName", "type": ["string", "null"]},
              {"name": "userProfileUrl", "type": ["string", "null"]},
              {"name": "userProfileImageUrl", "type": ["string", "null"]},
              {"name": "createdAt", "type": ["string", "null"]},
              {"name": "location", "type": ["string", "null"]},
              {"name": "score", "type": ["string", "null"]}]
            }
        }
    },
    {"name":"media","type":{
          "type":"array",
        "items": {
            "namespace":"com.bph.model",
            "name":"FBMedia",
            "type":"record",
            "fields": [
              {"name": "type", "type": ["string", "null"] },
              {"name": "score", "type": "int"},
              {"name": "createdAt", "type": ["string", "null"]},
              {"name": "imgUrl", "type": ["string", "null"]},
              {"name": "videoUrl", "type": ["string", "null"]},
              {"name": "title", "type": ["string", "null"]}]
            }
        }
    }]
}
************************************************************
***********************

Here is the code snippet that I am using

************************************************************
************************
GenericRecord dataRecord = new GenericData.Record(schema);

                dataRecord.put("streamId", "sid");
                dataRecord.put("source", "source");
                dataRecord.put("sourceType", "sourceType");
                dataRecord.put("referenceId", "referenceId");
                dataRecord.put("referenceMessageUrl",
"referenceMessageUrl");
                dataRecord.put("text", "text");
                dataRecord.put("createdAt", "createdAt");
                String[] arr = {"userId","userName","userProfileUrl","
userProfileImageUrl","createdAt","location","score"};

                //GenericData.Array<GenericRecord> userRecord = new
GenericData.Array<GenericRecord>(6, schema.getField("user").schema());
                GenericData.Array<String> userRecord = new
GenericData.Array<String>(6, schema.getField("user").schema());
                userRecord.add("userId");
                //userRecord.add(createGenericRecord(Arrays.
asList(arr),schema));
                //GenericArray<Integer> forward = new
GenericData.Array<Integer>(10, schema);
                dataRecord.put("user",userRecord);
                writer.write(dataRecord, encoder);

                encoder.flush();
                ByteArrayInputStream inputStream =
                        new ByteArrayInputStream(
outputStream.toByteArray());
                Decoder decoder = DecoderFactory.defaultFactory().
                        createBinaryDecoder(inputStream, null);
                GenericDatumReader<GenericRecord> reader =
                        new GenericDatumReader<GenericRecord>(schema);
                while(true){
                        try{
                              GenericRecord result = reader.read(null,
decoder);
                              System.out.println(result);
                        }
                        catch(EOFException eof){
                                break;
                        }
                        catch(Exception ex){
                                ex.printStackTrace();
                        }
                }
************************************************************
************************

It is a test code however it fails with this error
Exception in thread "main" java.lang.ClassCastException: java.lang.String
cannot be cast to org.apache.avro.generic.IndexedRecord


I can understand the cause of it from the source code, however I am not
find a simple way to put data in the complex avro schema.

I am in process of taking at look at the docs and code, would appericiate
if someone can point the direction.

Thanks,
Vicky

Reply via email to