Avro銇╯bt銈掍娇銇c仸entity銈儵銈广倰鑷嫊鐢熸垚銇欍倠(Java, Scala)


銇撱倱銇仭銇@s_tsuka銇с仚銆

Apache Avro銇儑銉笺偪銇偡銉偄銉┿偆銈恒偡銈广儐銉犮仹銇欍亴銆佷粖鍥炪伅銇撱倢銈抯bt銇у埄鐢ㄣ仚銈嬭┍銇с仚銆

Aache Avro

Apache Avro銇儠銈┿兗銉炪儍銉堛仹entity锛堛倛銇嗐仚銈嬨伀銉囥兗銈跨敤銇偗銉┿偣锛夈伄銈广偔銉笺優銈掑畾缇┿仐銆併仢銇偣銈兗銉炪儠銈°偆銉亱銈塭ntity銇偗銉┿偣銈掕嚜鍕曠敓鎴愩仚銈嬨併仺銇勩亞銈傘伄銇с仚銆

sbt/sbt-avro銈掍娇銇c仸Java銈儵銈广倰鑷嫊鐢熸垚

https://github.com/sbt/sbt-avro

鈥籗cala銉︺兗銈躲伄鏂广伅浣裤倧銇亜銇汇亞銇岃壇銇勩仺鎬濄亜銇俱仚銆

鏈杩戙伄Java妤晫銇с伅銇┿伄銉撱儷銉夈儎銉笺儷銇屾祦琛屻仯銇︺倠銈撱仹銇椼倗銇嗐亱銆傘倓銇倞Maven鈥?

Maven銇柟銇銇广仸銇勩仾銇勩伄銇с仚銇屻乻bt銇伅銇撱伄銉椼儵銈般偆銉炽亴銇傘倞銇俱仐銇熴

浣裤亜鏂广伅绨″崢銇с佸熀鏈琑EADME銇緭銇堛伆OK銇с仚

  1. plugins.sbt銇玸bt-avro銈掕拷鍔
  2. build.sbt銇玜vroSettings銈掕銇胯炯銈銈炽兗銉夈倰杩藉姞
  3. src/main/avro銉曘偐銉儉銇玜vro銉曘偐銉笺優銉冦儓銇偣銈兗銉炪儠銈°偆銉倰杩藉姞

銇撱伄鐘舵厠銇bt compile銇欍倢銇拌嚜鍕曘仹sbt-avro銇偝銉笺儔generate銇屽銇俱仯銇︺佹渶绲傜殑銇玹arget銉曘偐銉儉閰嶄笅銇畇rc_managed銈刢lasses銉曘偐銉儉閰嶄笅銇.java銉曘偂銈ゃ儷銈.class銉曘偂銈ゃ儷銇屻仹銇嶃伨銇欍

src/main/avro銉曘偐銉儉銇疆銇忋儠銈°偆銉伅user.avsc銇ㄣ亱銇悕绉般仹

{
  "namespace": "avro.entity",
  "type": "record",
  "name": "User",
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "favoriteNumber",  "type": ["int", "null"]},
    {"name": "favoriteColor", "type": ["string", "null"]}
  ]
}

銇撱倱銇劅銇樸仹缃亜銇︺亰銇戙伆澶т笀澶仹銇欍

src/main/avro銇儜銈广亴姘椼伀鍏ャ倝銇亱銇c仧銈娿乻tring銇孋harSequence銇睍闁嬨仌銈屻倠銇亴姘椼伀椋熴倧銇亜鍫村悎銇痓uild.sbt銇亾銈撱仾鎰熴仒銇ц拷鍔犺ō瀹氥倰鍏ャ倢銈屻伆OK銇с仚銆

(stringType in avroConfig) := "String"

(sourceDirectory in avroConfig) := file("src/main/resources")

娈嬪康銇亴銈夌敓鎴愩仌銈屻倠銇伅Scala銈炽兗銉夈仹銇仾銇勩伄銇с丼cala銉︺兗銈躲伅浠ヤ笅銇倐銇倰浣裤仯銇熴伝銇嗐亴鑹仌銇濄亞銇с仚銆

julianpeeters/sbt-avrohugger銈掍娇銇c仸Scala銈炽兗銉夈倰鑷嫊鐢熸垚

https://github.com/julianpeeters/sbt-avrohugger

銇撱仭銈夈伅Scala銈炽兗銉夈倰鐢熸垚銇椼仸銇忋倢銇俱仚銆

銇撱仭銈夈倐浣裤亜鏂广伅鍏堛伝銇┿仺銇汇伡鍚屻仒銇с仚銆俁EADME銈掕銈屻伆銈忋亱銈嬨亱銇ㄦ濄亜銇俱仚銆

銇撱倢銈掍娇銇嗐仺浠ヤ笅銇倛銇嗐伀Scala銈炽兗銉夈亴鐢熸垚銇曘倢銇俱仚銆

/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package avro.entity

case class User(name: String, favoriteNumber: Option[Int], favoriteColor: Option[String])

銇°們銈撱仺Option銇仾銇c仸銇俱仚锛

銇ㄣ伅銇勩亪銆併儠銈°偆銉伄鍐呭銇屻亾銈屻仐銇嬨仾銇勩伄銇с丄vro銇瓵PI銇部銇c仸銇勩倠銈儵銈广仾銇亱銇ㄣ亜銇嗐仺鎬仐銇勬皸銇屻仐銇俱仚銆傘亾銇偗銉┿偣銇嬨倝鐢熸垚銇椼仧銈儢銈搞偋銈儓銈扐vro褰㈠紡銇с偡銉偄銉┿偆銈恒仚銈嬨仺銇嶃伅銇俱仧鍒ャ伄銉┿偆銉栥儵銉倰浣裤倧銇亜銇ㄣ亜銇戙仾銇勬皸銇屻仐銇俱仚銆傘仢銇伕銈撱仼銇嗐仾銈撱仹銇椼倗銇嗐兓銉汇

浠ヤ笂銇с仚銆

瑁滆冻

銈炽儭銉炽儓娆勩伀銈傘亗銈娿伨銇欍亴銆乯ulianpeeters/sbt-avrohugger 銇屻倧銇栥倧銇栨暀銇堛仸銇忋倢銇俱仐銇熴併亗銈娿亴銇ㄣ亞锛

涓婅銇с孉vro銇瓵PI銇部銇c仸銇勩倠銈儵銈广仾銇亱銇ㄣ亜銇嗐仺鎬仐銇勬皸銇屻仐銇俱仚銆傘嶃仺瑷銇c仸銇勩伨銇欍亴銆併亾銈屻伅sbtavrohubber.SbtAvrohugger.avroSettings銈掍娇銇c仸銇勩倠銇嬨倝銇濄亞銇倠銈堛亞銇с仚銇侫vro銇簴鎷犮仐銇焎ase class銈掍綔銈娿仧銇勫牬鍚堛伅銆乻btavrohugger.SbtAvrohugger.specificAvroSettings銇柟銈掍娇銇堛伆鑹亜銈堛亞銇с仚銆傝В姹猴紒

浠ヤ笂銇с仚