簡體   English   中英

Avro 模式中的 Avro 模式文件引用

[英]Avro schema file references in a Avro schema

我有近 100 個 avsc 文件,這些 avsc 文件中的大多數通常引用另一個 asvc 文件作為它們的類型。 舉個例子

Item.avsc 在./com/example/common

{
    "namespace":"com.example.common",
    "name":"Item",
    "type":"record",
    "fields":[
        {
            "name":"itemId",
            "type":"com.example.common.ItemId"
        },
        {
            "name":"features",
            "type":"com.example.common.Features"
        }
    ]
}

ItemId.avsc 在./com/example/common

{
    "namespace":"com.example.common",
    "name":"ItemId",
    "type":"record",
    "fields":[
        {
            "name":"id",
            "type":"int"
        }
    ]
}

Features.avsc in./com/example/common

{
    "namespace":"com.example.common",
    "name":"Features",
    "type":"record",
    "fields":[
        {
            "name":"Range",
            "type":{
                "type":"array",
                "items":"com.example.common.Range"
            }
        }
    ]
}

當我想解析 Item.avsc 的架構時,它會引發:

Schema schema = new Schema.Parser().parse(new File(".\\com\\example\\common\\Item.avsc"));

Exception in thread "main" org.apache.avro.SchemaParseException: "com.example.common.ItemId" is not a defined name. The type of the "itemId" field must be a defined name or a {"type": ...} expression.

我通過使用解析器的單個實例首先解析 ItemId.avsc 和 Features.avsc,然后解析 Item.avsc 找到了解決此問題的方法,如下所示:

Parser parser = new Parser();
parser.parse(new File(".\\com\\example\\common\\ItemId.avsc"));
parser.parse(new File(".\\com\\example\\common\\Features.avsc"));
parser.parse(new File(".\\com\\example\\common\\Range.avsc"));
parser.parse(new File(".\\com\\example\\common\\Item.avsc"));

但我有將近 100 個 avsc 文件,其中大多數引用多個 avsc 文件,我需要在考慮它們的依賴等級時像這樣解析每個文件。 有更好的解決方案嗎?

遍歷你的包,並通過循環解析它們。

Schema.Parser parser = new Schema.Parser();
URI uri = Test.class.getResource("package/name/here").toURI();
Path myPath = Paths.get(uri);
try (Stream<Path> paths = Files.walk(myPath)) {
     paths.filter(Files::isRegularFile)
          .filter(path -> path.toString().endsWith(".avsc"))
          .map(path -> new File(path.toUri()))
          .forEach(file -> {
              try {
                 parser.parse(file);
              } catch (IOException e) {}
          });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM