我如何在Avro中代表Scala特征?

时间:2017-09-08 16:08:15

标签: scala avro

假设这样的模型:

Sub test()

Range("A5:A30").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("'Sheet1'!E7"), Unique:=True

For i = 7 To Cells(7, 5).End(xlDown).Row
      Cells(i, 6).Value = Application.WorksheetFunction.SumIf(Range("A:A"), Cells(i, 5),Range("B:B"))
Next i


End Sub

我希望能够发送/接收Message [PayloadKind]对象并让Avro做正确的事情。 (现在它完全被困惑了!)

这种东西的Avro架构会是什么样子?我已经走到了这一步:

trait PayloadKind
trait Command extends PayloadKind
trait Event extends PayloadKind

trait Message[T <: PayloadKind] {
  val id: String
  val payload: T
}

case class Alarm(severity: Int) extends Event

case class AlarmMessage(id: String, payload: Alarm) extends Message[Event]

PayloadKind(和事件/命令)是标记特征。我如何在Avro Schema中表示它们?

1 个答案:

答案 0 :(得分:0)

要对此进行建模,您需要为记录和联合使用命名类型:

然后,Avro可以通过名称找出联盟内的WHICH记录类型。该名称充当子类的鉴别器。

{
  "type":"record","name":"Message","namespace":"com.tryme","fields":[
    {"name":"id","type":"string"},
    {"name":"payload","type":[ 
      {
      "type":"record","name":"Event","fields":[]
      },
      {
      "type":"record","name":"Command","fields":[]
      }
      ]
    }
  ]
}