【问题标题】:Store list or collection in Apache Avro schema在 Apache Avro 模式中存储列表或集合
【发布时间】:2017-02-28 21:43:48
【问题描述】:

我目前正在创建 Avro 模式来存储 twitter 数据流。 我的 JSON 数据源:

{
'id': '123456789',
'text': 'bla bla bla...',
'entities': {
  'hashtags': [{'text':'hashtag1'},{'text':'hashtag2'}]
  }
}

在 Cassandra 中,我可以定义集合(集合或列表)来存储主题标签数据。 但我不知道如何在 Apache Avro 中定义这种结构。

这是我最好的尝试:

{"namespace": "ln.twitter",
 "type": "record",
 "name": "main",
 "fields": [
   {"name": "id","type": "string"},
   {"name": "text","type": "string"},
   {"name": "hashtags","type": "string"} // is there any better format for this ?
 ]
}

需要你的建议。

谢谢, 汤太。

【问题讨论】:

    标签: list collections schema avro


    【解决方案1】:

    entities 字段内部需要明确的记录(或映射)。这是一个应该工作的架构:

    {
      "type": "record",
      "name": "Main",
      "fields": [
        {
          "name": "id",
          "type": "string"
        },
        {
          "name": "text",
          "type": "string"
        },
        {
          "name": "entities",
          "type": {
            "type": "record",
            "name": "Entities",
            "fields": [
              {
                "name": "hashtags",
                "type": {
                  "type": "array",
                  "items": {
                    "type": "record",
                    "name": "Hashtag",
                    "fields": [
                      {
                        "name": "text",
                        "type": "string"
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      ]
    }
    

    如果有帮助,您可以使用 this tool 从任何有效的 JSON 记录生成(匿名)Avro 架构。然后,您只需将名称添加到 record 类型。

    您可以在将' 切换为" 后在您的示例中尝试:

    {
      "id": "123456789",
      "text": "bla bla bla...",
      "entities": {"hashtags": [{"text": "hashtag1"}, {"text": "hashtag2"}]}
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2011-09-29
      • 2017-03-14
      • 2019-07-12
      • 2013-12-31
      • 2021-04-13
      • 2023-02-25
      • 2013-11-09
      • 1970-01-01
      相关资源
      最近更新 更多