使用 avro-maven-plugin 版本 1.6.1 时已弃用的代码

发布于 2024-12-25 08:52:21 字数 5940 浏览 3 评论 0原文

我正在使用 Apache Avro 运行 java 代码。 java 文件中的某些代码已被弃用,我不知道为什么。 我正在使用 Maven 来运行我的 Java 程序。 这是java文件

    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 时,相同的代码不会被弃用

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 = { "[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();
}

}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2025-01-01 08:52:21

我唯一能想到的(因为您没有向我们提供实际的警告消息)是您应该使用访问器方法,而不是直接访问字段值(datum.foo = x)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文