使用reader schema

时间:2017-12-24 18:40:05

标签: java json avro

我想在命令行上使用与编写器架构不同的读取器架构反序列化Avro数据。我可以在序列化时指定编写器模式,但不能在反序列化期间指定。

record.json(数据文件):

{"test1": 1, "test2": 2}

writer.avsc(作者架构):

{
    "type": "record",
    "name": "pouac",
    "fields": [
        {
            "name": "test1",
            "type": "int"
        },
        {
            "name": "test2",
            "type": "int"
        }
    ]
}

reader.avsc(读者架构):

{
    "type": "record",
    "name": "pouac",
    "fields": [{
        "name": "test2",
         "type": "int",
         "aliases": ["test1"]
    }]
}

序列化数据:

$ java -jar avro-tools-1.8.2.jar fromjson --schema-file writer.avsc record.json > record.avro

为了反序列化数据,我尝试了以下方法:

$ java -jar avro-tools-1.8.2.jar tojson --schema-file reader.avsc record.avro
Exception in thread "main" joptsimple.UnrecognizedOptionException: 'schema-file' is not a recognized option
...

我主要是寻找命令行指令,因为我编写Java代码并不那么容易,但我很乐意使用Java代码来编译自己。实际上,我感兴趣的是确切的反序列化结果。 (this conversation在我为实现别名而打开的一个快速公关PR中描述了更为根本的问题

2 个答案:

答案 0 :(得分:2)

avro-tools tojson目标仅用作将二进制编码的Avro文件转换为JSON的转储工具。架构始终伴随Avro文件中的记录,如下面的链接所示。因此,avro-tools无法覆盖它。

http://avro.apache.org/docs/1.8.2/#compare

我不知道可以用来实现你想要的独立工具。我认为你需要做一些编程才能达到预期的效果。 Avro有许多支持的语言,包括Python,但跨语言的功能并不统一。 Java在我的经验中是最先进的。例如,Python缺乏在DataFileReader上指定读者架构的能力,这有助于实现您的目标:

https://github.com/apache/avro/blob/master/lang/py/src/avro/datafile.py#L224

您可以在Python中获得的最接近的内容如下;

import avro.schema as avsc
import avro.datafile as avdf
import avro.io as avio

reader_schema = avsc.parse(open("reader.avsc", "rb").read())

# need ability to inject reader schema as 3rd arg
with avdf.DataFileReader(open("record.avro", "rb"), avio.DatumReader()) as reader:
    for record in reader:
        print record

就您所概述的架构和数据而言。预期的行为应未定义,因此会发出错误

可以使用以下Java代码验证此行为;

package ca.junctionbox.soavro;

import org.apache.avro.Schema;
import org.apache.avro.SchemaValidationException;
import org.apache.avro.SchemaValidationStrategy;
import org.apache.avro.SchemaValidator;
import org.apache.avro.SchemaValidatorBuilder;

import java.util.ArrayList;

public class Main {
    public static final String V1 = "{\n" +
            "    \"type\": \"record\",\n" +
            "    \"name\": \"pouac\",\n" +
            "    \"fields\": [\n" +
            "        {\n" +
            "            \"name\": \"test1\",\n" +
            "            \"type\": \"int\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"name\": \"test2\",\n" +
            "            \"type\": \"int\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";

    public static final String V2 = "{\n" +
            "    \"type\": \"record\",\n" +
            "    \"name\": \"pouac\",\n" +
            "    \"fields\": [{\n" +
            "        \"name\": \"test2\",\n" +
            "         \"type\": \"int\",\n" +
            "         \"aliases\": [\"test1\"]\n" +
            "    }]\n" +
            "}";

    public static void main(final String[] args) {
        final SchemaValidator sv = new SchemaValidatorBuilder()
                .canBeReadStrategy()
                .validateAll();
        final Schema sv1 = new Schema.Parser().parse(V1);
        final Schema sv2 = new Schema.Parser().parse(V2);
        final ArrayList<Schema> existing = new ArrayList<>();
        existing.add(sv1);

        try {
            sv.validate(sv2, existing);
            System.out.println("Good to go!");
        } catch (SchemaValidationException e) {
            e.printStackTrace();
        }
    }
}

这会产生以下输出:

org.apache.avro.SchemaValidationException: Unable to read schema: 
{
  "type" : "record",
  "name" : "pouac",
  "fields" : [ {
    "name" : "test2",
    "type" : "int",
    "aliases" : [ "test1" ]
  } ]
}
using schema:
{
  "type" : "record",
  "name" : "pouac",
  "fields" : [ {
    "name" : "test1",
    "type" : "int"
  }, {
    "name" : "test2",
    "type" : "int"
  } ]
}
    at org.apache.avro.ValidateMutualRead.canRead(ValidateMutualRead.java:70)
    at org.apache.avro.ValidateCanBeRead.validate(ValidateCanBeRead.java:39)
    at org.apache.avro.ValidateAll.validate(ValidateAll.java:51)
    at ca.junctionbox.soavro.Main.main(Main.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
    at java.lang.Thread.run(Thread.java:748)

别名通常用于模式演进中的向后兼容性,允许从不同/遗留密钥到公共密钥名称的映射。鉴于您的作者架构并未将test1和test2字段视为&#34;可选&#34;通过使用工会,我无法看到你想要这种转变的场景。如果你想&#34;掉落&#34; test1字段然后可以通过从v2模式规范中排除它来实现。任何可以应用读者方案的读者都会使用v2模式定义忽略test1。

说明我的进化意义;

v1架构

{
  "type": "record",
  "name": "pouac",
  "fields": [
    {
        "name": "test1",
        "type": "int"
    }]
}

v2架构

{
  "type": "record",
  "name": "pouac",
  "fields": [
    {
        "name": "test2",
        "type": "int",
        "aliases": ["test1"]
    }]
}

您可以使用v1格式的TB级数据,并引入将test1字段重命名为test2的v2格式。别名允许您对v1和v2数据执行map-reduce作业,Hive查询等,而无需先主动重写所有旧的v1数据。请注意,这假设字段的类型和语义没有变化。

答案 1 :(得分:-2)

您可以运行java -jar avro-tools-1.8.2.jar tojson查看帮助,它告诉您可以使用以下命令:

java -jar avro-tools-1.8.2.jar tojson record.avro > tost.json

,这将输出到文件:

{"test1":1,"test2":2}

您也可以使用--pretty argumment:

来调用它
java -jar avro-tools-1.8.2.jar tojson --pretty record.avro > tost.json

并且输出很漂亮:

{
  "test1" : 1,
  "test2" : 2
}