Question

I'm running a java code using Apache Avro. Some code gets deprecated in the java file and I'm not sure why. I'm using Maven to run my Java program. This is the java file

    public class AvroAddressTest {
public int tempRand;

 static String[] NAMES = { "Karthik", "Sam", "Joe", "Jess", "Tom",
        "Huck", "Hector", "Duke", "Jill", "Natalie", "Chirsta", "Ramya" };

 static String[] EMAILS = { "kar@gmail.com", "steve@gmail.com",
        "garry@gmail.com", "kumar@hotmail.com", "dave@hotmail.com",
        "will@hotmail.com", "rick@ymail.com", "vinod@ymail.com",
        "basu@ymail.com", "sachin@ymail.com", "chester@ymail.com",
        "anand@ymail.com" };

 static String[] PHONE_NUMBERS = { "9940099321", "9940099456",
        "9934099333", "9940099567", "9940077654", "9940088323",
        "9940097543", "9940099776", "9940000981", "9940088444",
        "9940099409", "9940033987" };




 static int[] AGES = { 32, 43, 23, 21, 55, 34, 33, 31, 22, 41, 56, 62 };
 static boolean[] STU = { true, false, true, true, false, false, true, false, true, false, false, true };


public void serializeGeneric() throws IOException {
    // Create a datum to serialize.
    Schema schema = new Schema.Parser().parse(getClass()
            .getResourceAsStream("/AddressRec.avsc"));
    GenericRecord datum = new GenericData.Record(schema);

    Random random = new Random();

    int randInt = random.nextInt(NAMES.length);

    datum.put("name", new Utf8(NAMES[randInt]));
    datum.put("email", new Utf8(EMAILS[randInt]));
    datum.put("phone", new Utf8(PHONE_NUMBERS[randInt]));
    datum.put("age", AGES[randInt]);
    datum.put("student", STU[randInt]);
    //datum.put("door",new Utf8(NAMES[randInt]) );

    // Serialize it.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(
            schema);
    Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    writer.write(datum, encoder);
    encoder.flush();
    out.close();
    System.out.println("\nSerialization: " + out);

    // Deserialize it.
    DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(
            schema);
    BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(
            out.toByteArray(), null);
    GenericRecord result = reader.read(null, decoder);
    System.out.printf(
            "Deserialized output:\nName: %s, Email: %s, Phone: %s, Age: %d, Student?: %s\n\n",
            result.get("name"), result.get("email"), result.get("phone"),
            result.get("age"), result.get("student"));
}

public void serializeSpecific() throws IOException {
    // Create a datum to serialize.
    AddressRec datum = new AddressRec();
    Random random = new Random();
    int randInt = random.nextInt(NAMES.length);

    datum.**name** = new Utf8(NAMES[randInt]);
    datum.**email** = new Utf8(EMAILS[randInt]);
    datum.**phone** = new Utf8(PHONE_NUMBERS[randInt]);
    datum.**age** = AGES[randInt];
    datum.**student** = STU[randInt];

    File tmpFile = File.createTempFile("AddressRecAvroExample", ".avro");
    // Serialize it.
    DataFileWriter<AddressRec> writer = new DataFileWriter<AddressRec>(
            new SpecificDatumWriter<AddressRec>(AddressRec.class));
    writer.create(AddressRec.SCHEMA$, tmpFile);
    writer.append(datum);
    writer.close();

    System.out.println("\nSerialization to tempfile: " + tmpFile);

    // Deserialize it.
    FileReader<AddressRec> reader = DataFileReader.openReader(tmpFile,
            new SpecificDatumReader<AddressRec>(AddressRec.class));
    while (reader.hasNext()) {
        AddressRec result = reader.next();
        System.out.printf("Deserialized output:\nName: %s, Email: %s, Phone: %s, Age: %d, Student?: %s\n\n",
                        result.**name**, result.**email**, result.**phone**,
                        result.**age**, result.**student**);
    }
    reader.close();
}

@Test
public void serializeTest() throws IOException {
    serializeGeneric();
    serializeSpecific();
}

}

What is the problem? The code in block is getting deprecated.

This is the .avsc file

{
"type": "record",
"name": "AddressRec",
"namespace":"com.mycompany.samples.avro",
"fields": [
    {"name": "name", "type": "string"},
    {"name": "email", "type": "string"},
    {"name": "phone", "type": "string"},
    {"name": "age", "type": "int"}, 
    {"name": "student", "type": "boolean"}

]

}

The program is running fine . Its just that some code is deprecated. The same code is not deprecated when i use version 1.5.1

Was it helpful?

Solution

The only thing I can think of (since you didn't provide us with the actual warning messages) is that instead of directly accessing the field values (datum.foo = x) you should use accessor methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top