Java 使用avro maven插件版本1.6.1时不推荐使用的代码
我正在使用ApacheAvro运行一个java代码。有些代码在java文件中被弃用,我不知道为什么。 我正在使用Maven运行我的Java程序。 这是java文件Java 使用avro maven插件版本1.6.1时不推荐使用的代码,java,maven-plugin,pom.xml,avro,Java,Maven Plugin,Pom.xml,Avro,我正在使用ApacheAvro运行一个java代码。有些代码在java文件中被弃用,我不知道为什么。 我正在使用Maven运行我的Java程序。 这是java文件 public class AvroAddressTest { public int tempRand; static String[] NAMES = { "Karthik", "Sam", "Joe", "Jess", "Tom", "Huck", "Hector", "Duke", "Jill", "N
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();
}
}
程序运行良好。只是有些代码被弃用了。当我使用1.5.1版时,同样的代码没有被弃用。我能想到的唯一一件事(因为您没有向我们提供实际的警告消息)是,您不应该直接访问字段值(datum.foo=x),而应该使用访问器方法。下面是您假设的证据:
{
"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"}
]