【问题标题】:Convert Json to Avro schema将 Json 转换为 Avro 架构
【发布时间】:2020-07-13 01:41:48
【问题描述】:

问题

我想将如下所示的普通 Json 文件转换为 avro 模式,以使其在具有融合模式注册表的 apache kafka 中工作。

示例

输入(Json)

[
  {
    "name": "Robin Hood",
    "department": "",
    "manager": "",
    "salary": 200
  },
  {
    "name": "Arsene Wenger",
    "department": "Bar",
    "manager": "Friar Tuck",
    "salary": 50
  },
  {
    "name": "Friar Tuck",
    "department": "Foo",
    "manager": "Robin Hood",
    "salary": 100
  },
  {
    "name": "Little John",
    "department": "Foo",
    "manager": "Robin Hood",
    "salary": 100
  },
  {
    "name": "Sam Allardyce",
    "department": "",
    "manager": "",
    "salary": 250
  },
  {
    "name": "Dimi Berbatov",
    "department": "Foo",
    "manager": "Little John",
    "salary": 50
  }
]

输出(Avro 模式)

{
  "name": "MyClass",
  "type": "array",
  "namespace": "com.acme.avro",
  "items": {
    "name": "MyClass_record",
    "type": "record",
    "fields": [
      {
        "name": "name",
        "type": "string"
      },
      {
        "name": "department",
        "type": "string"
      },
      {
        "name": "manager",
        "type": "string"
      },
      {
        "name": "salary",
        "type": "int"
      }
    ]
  }
}

一个 Json Schema 作为输入也很好。

这个问题was asked a while ago 但没有好的答案。

有一个 website 可以做到这一点,但我想要一个库或 cli。

谢谢!

【问题讨论】:

    标签: json apache-kafka avro confluent-schema-registry


    【解决方案1】:

    事实证明,avro4s 可以做到这一点。这是一个 Scala 库,我在 java 中什么也没找到。

    这里有一个简单的例子来说明如何使用它。我有包含属性的事件。

    package example
    
    import com.sksamuel.avro4s.AvroSchema
    import com.sksamuel.avro4s.json.JsonToAvroConverter
    
    object Main extends App {
    
      case class Propertie(name: String, value: String)
      case class Event(name: String, properties: Seq[Propertie])
    
      val schema = AvroSchema[Event]
    
      val converter = new JsonToAvroConverter("com.example.kafkaorch")
      val string =
        """{
          |  "AvroEvent": {
          |    "name": "order-created",
          |    "AvroPropertie": {
          |      "name": "",
          |      "type": "",
          |      "value":""
          |    }
          |  }
          |}""".stripMargin
    
      print(converter.convert("test", string).toString(true))
    
    }
    
    

    结果应该是这样的:

    {
      "type" : "record",
      "name" : "test",
      "namespace" : "com.example.kafkaorch",
      "fields" : [ {
        "name" : "AvroEvent",
        "type" : {
          "type" : "record",
          "name" : "AvroEvent",
          "fields" : [ {
            "name" : "name",
            "type" : "string"
          }, {
            "name" : "AvroPropertie",
            "type" : {
              "type" : "record",
              "name" : "AvroPropertie",
              "fields" : [ {
                "name" : "name",
                "type" : "string"
              }, {
                "name" : "type",
                "type" : "string"
              }, {
                "name" : "value",
                "type" : "string"
              } ]
            }
          } ]
        }
      } ]
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2020-05-20
      • 2020-10-01
      • 1970-01-01
      • 2020-02-10
      相关资源
      最近更新 更多