当前位置: 首页 > 面试题库 >

使用Apache Avro反射

法弘亮
2023-03-14
问题内容

Avro序列化在Hadoop用户中很流行,但是很难找到示例。

谁能帮我这个示例代码?我对使用Reflect API读取/写入文件以及使用Union和Null注释最感兴趣。

public class Reflect {

    public class Packet {
        int cost;
        @Nullable TimeStamp stamp;
        public Packet(int cost, TimeStamp stamp){
            this.cost = cost;
            this.stamp = stamp;
        }
    }

    public class TimeStamp {
        int hour = 0;
        int second = 0;
        public TimeStamp(int hour, int second){
            this.hour = hour;
            this.second = second;
        }
    }

    public static void main(String[] args) throws IOException {
        TimeStamp stamp;
        Packet packet;

        stamp = new TimeStamp(12, 34);
        packet = new Packet(9, stamp);
        write(file, packet);

        packet = new Packet(8, null);
        write(file, packet);
        file.close();

        // open file to read.
        packet = read(file);
        packet = read(file);
    }
}

问题答案:

这是上述程序的有效版本。

这也对文件使用压缩。

import java.io.File;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.CodecFactory;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.apache.avro.reflect.ReflectDatumReader;
import org.apache.avro.reflect.Nullable;

public class Reflect {

  public static class Packet {
    int cost;
    @Nullable TimeStamp stamp;
    public Packet() {}                        // required to read
    public Packet(int cost, TimeStamp stamp){
      this.cost = cost;
      this.stamp = stamp;
    }
  }

  public static class TimeStamp {
    int hour = 0;
    int second = 0;
    public TimeStamp() {}                     // required to read
    public TimeStamp(int hour, int second){
      this.hour = hour;
      this.second = second;
    }
  }

  public static void main(String[] args) throws Exception {
    // one argument: a file name
    File file = new File(args[0]);

    // get the reflected schema for packets
    Schema schema = ReflectData.get().getSchema(Packet.class);

    // create a file of packets
    DatumWriter<Packet> writer = new ReflectDatumWriter<Packet>(Packet.class);
    DataFileWriter<Packet> out = new DataFileWriter<Packet>(writer)
      .setCodec(CodecFactory.deflateCodec(9))
      .create(schema, file);

    // write 100 packets to the file, odds with null timestamp
    for (int i = 0; i < 100; i++) {
      out.append(new Packet(i, (i%2==0) ? new TimeStamp(12, i) : null));
    }

    // close the output file
    out.close();

    // open a file of packets
    DatumReader<Packet> reader = new ReflectDatumReader<Packet>(Packet.class);
    DataFileReader<Packet> in = new DataFileReader<Packet>(file, reader);

    // read 100 packets from the file & print them as JSON
    for (Packet packet : in) {
      System.out.println(ReflectData.get().toString(packet));
    }

    // close the input file
    in.close();
  }

}


 类似资料:
  • 问题内容: 前几天,我通过手动将类的每个元素写成String来为Java中的类编写toString(),我想到使用反射可能可以创建一个通用的toString()方法在所有课程上。IE浏览器将找出字段名称和值,并将其发送到字符串。 获取字段名称非常简单,这是同事提出的: 使用工厂,我可以通过在第一次调用toString()时存储一次字段来减少性能开销。但是,找到这些值可能会贵得多。 由于反射的作用,

  • 我想动态调用jsoup中的一个方法。这里是我的用例,我实际上在jsoup中调用多个select dom方法来遍历内部, 我能实现同样的动态像, 我知道我们可以使用反射来实现,但不确定如何实现这种行为,因为模式字符串将是动态的,并且可能有n个dom选择器。 让我知道如果上面的动力是可能的。

  • 问题内容: 我试图学习反思,并且遇到了这个IllegalAccessException。请参见以下代码: 当我尝试运行该程序时,得到以下信息: 我不明白发生了什么。有任何想法吗?提前致谢。 问题答案: 您需要禁止Java语言访问检查,以便使用setAccessible(true)反射地调用另一个类中的私有方法: 此外,启用SecurityManager后,我们需要额外的权限才能调用setAcces

  • 本文向大家介绍PHP 反射(Reflection)使用实例,包括了PHP 反射(Reflection)使用实例的使用技巧和注意事项,需要的朋友参考一下 PHP Reflection是用于获取类、扩展、方法、函数、对象、参数、属性的详细信息。 ReflectionClass类获取类相关信息,如获取属性、方法、文档注释等。 ReflectionExtension 类用于获取扩展相关信息  Reflec

  • 如果您想使用 Nginx 作为 Gitea 的反向代理服务,您可以参照以下 nginx.conf 配置中 server 的 http 部分: server { listen 80; server_name git.example.com; location / { proxy_pass http://localhost:3000; } } 使用

  • 问题内容: 我需要使用反射调用类的setter方法,并且代码如下: 的是一个以及设置器方法如下: 运行此代码时,将引发A ,但是将setter方法参数类型更改为from时,将执行正常。有没有一种方法可以将setter方法的参数保持为超级类型,并且在从类中获取方法时仍无需手动指定参数的类型即可使用反射? 问题答案: 与其他答案相反,有一个非常简单的解决方案。请参阅。它为您提供了一种执行任意反射代码的