Avro架构:从架构字段构建Avro架构

时间:2019-08-14 14:42:50

标签: scala avro avro-tools

我正在尝试编写一个函数来计算两个avro模式之间的差异并生成另一个模式。

const searchInObject = function(obj, keys) { 
    if(keys instanceof Array) { 
        for(let key of keys) {
            if(obj[key])
                return obj[key];
        }
    }
    // If keys is not an array, return -1 (or anything else if you want to)
    return -1;
}; 

要获取schema_one中的元素字段而不是schema_two中的

schema_one = {
  "type": "record",
  "name": "schema_one",
  "namespace": "test",
  "fields": [
    {
      "name": "type",
      "type": "string"
    },
    {
      "name": "id",
      "type": "string"
    }
  ]
}

schema_two = {
  "type": "record",
  "name": "schema_two",
  "namespace": "test",
  "fields": [
    {
      "name": "type",
      "type": "string"
    }
  ]
}

到目前为止,很好。

我想从diff构建一个新模式,我希望它是:

import org.apache.avro.Schema._
import org.apache.avro.{Schema, SchemaBuilder}
val diff: Set[Schema.Field] =  schema_one.getFields.asScala.toSet.filterNot(schema_two.getFields.asScala.toSet)

我似乎无法在Avro schema_three = { "type": "record", "name": "schema_three", "namespace": "test", "fields": [ { "name": "id", "type": "string" } ] } 中找到任何方法来实现此目的,而不必显式提供命名字段。即在给定SchemaBuilder的情况下构建Schema

例如:

Schema.Field

有没有办法做到这一点?欣赏评论。

1 个答案:

答案 0 :(得分:0)

我能够使用风筝sdk "org.kitesdk" % "kite-data-core" % "1.1.0"

实现这一目标
  val schema_namespace = schema_one.getNamespace
  val schema_name = schema_one.getName

  val schemas = diff.map( f => {
    SchemaBuilder
      .record(schema_name)
      .namespace(schema_namespace)
      .fields()
      .name(f.name())
      .`type`(f.schema())
      .noDefault()
        .endRecord()
   }
  )

  val schema_three = SchemaUtil.merge(schemas.asJava)
相关问题