代码之家  ›  专栏  ›  技术社区  ›  Cassie

读取avro文件时不是数据文件错误

  •  0
  • Cassie  · 技术社区  · 6 年前

    我有一个AVRO格式的文件。我想把这些数据读到genericrecord类型的数据结构或任何其他类型的数据结构中,这样我就能把它从kafka发送到spark。

    我尝试使用DataFileReader,但结果是出现以下错误:

    Exception in thread "main" java.io.IOException: Not a data file.
        at org.apache.avro.file.DataFileStream.initialize(DataFileStream.java:105)
    

    下面是生成它的代码:

    val schema = Source.fromFile(schemaPath).mkString
    val parser = new Schema.Parser
    val avroSchema = parser.parse(schema)
    val avroDataFile = new File(dataPath)
    
    val avroReader = new GenericDatumReader[GenericRecord](avroSchema)
    val dataFileReader = new DataFileReader[GenericRecord](avroDataFile, avroReader) 
    //THIS LINE PRODUCED ERROR
    

    如何修复此错误?

    这就是我的avro数据模式的样子:

    {
      "type" : "record",
      "namespace" : "input_data",
      "name" : "testUser",
      "fields" : [
        {"name" : "name", "type" : "string", "default": "NONE"},
        {"name" : "age", "type" : "int", "default": -1},
        {"name" : "phone", "type" : "string", "default" : "NONE"},
        {"name" : "city", "type" : "string", "default" : "NONE"},
        {"name" : "country", "type" : "string", "default" : "NONE"}
      ]
    }
    

    这是我试图读取的数据(它是由 this tool ):

    {
      "name" : "O= ~usP3\u0001\bY\u0011k\u0001",
      "age" : 585392215,
      "phone" : "\u0012\u001F#\u001FH]e\u0015UW\u0000\fo",
      "city" : "aWi\u001B'\u000Bh\u00163\u001A_I\u0001\u0001L",
      "country" : "]H\u001Dl(n!Sr}oVCH"
    }
    {
      "name" : "\u0011Y~\fV\u001Dv%4\u0006;\u0012",
      "age" : -2045540864,
      "phone" : "UyOdgny-hA",
      "city" : "\u0015f?\u0000\u0015oN{\u0019\u0010\u001D%",
      "country" : "eY>c\u0010j\u0002[\u001CdDQ"
    }
    ...
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   OneCricketeer Gabriele Mariotti    6 年前

    好吧,那个数据不是Avro,而是JSON。

    如果它是二进制的avro数据,那么在没有首先使用的情况下就无法读取该文件。 avro-tools.jar tojson 行动。

    如果查看usage文档,json是默认值

    -j, --json: Encode outputted data in JSON format (default)
    

    要实际获取avro,请使用 arg -s schema.avsc -b -o out.avro

    还有其他方法 generate test data in Kafka