2012-01-05 20 views
2

我正在使用Apache Avro運行java代碼。一些代碼在java文件中被棄用,我不知道爲什麼。 我正在使用Maven來運行我的Java程序。 這是java文件使用avro-maven-plugin版本1.6.1時的棄用代碼

public class AvroAddressTest { 
public int tempRand; 

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

static String[] EMAILS = { "[email protected]", "[email protected]", 
     "[email protected]", "[email protected]", "[email protected]", 
     "[email protected]", "[email protected]", "[email protected]", 
     "[email protected]", "[email protected]", "[email protected]", 
     "[email protected]" }; 

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(); 
} 

}

問題是什麼?塊中的代碼正在被棄用。

這是.avsc文件

{ 
"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"} 

] 

}

程序運行良好。它只是一些代碼已被棄用。當我使用版本1.5.1時,不會棄用相同的代碼。

回答

2

我能想到的唯一一件事(因爲您沒有提供實際的警告消息),而不是直接訪問字段值(數據。 foo = x)你應該使用訪問器方法。

+0

以下是您的假設的證明:http://grokbase.com/t/avro/user/1215gxq144/deprecation-warnings-in-generated-source-code – yair 2015-07-27 13:29:38