これら3つのAvro定義を使って、スキーマ作成、一括インポート、バージョン比較を試してみてください。 この例は、環境測定値、ファームウェア、位置情報、診断情報を報告する装置について説明しています。
各バージョンはレコード名 DeviceTelemetryEventData と名前空間 Fabrikam.IoT保持しています。 ファイル名はサンプルバージョンを識別します。レジストリで割り当てられたバージョン番号の代わりにはなりません。
| Version | Changes | ファイル名 |
|---|---|---|
| 1 | デバイスおよびテナント識別子、キャプチャ時間、温度、湿度、バッテリーの読み取り値を定義します。 | Fabrikam.DeviceTelemetryEventData.v1.avsc |
| 2 | ファームウェア情報とオプションのネスト位置情報を追加します。 | Fabrikam.DeviceTelemetryEventData.v2.avsc |
| 3 | ステータス列挙、診断コードの配列、タグのマップを追加します。 | Fabrikam.DeviceTelemetryEventData.v3.avsc |
サンプルファイルを保存してください
各完全なJSON定義を、ファイル名を記載した別のUTF-8テキストファイルにコピーします。 これらのファイルはスキーマ定義であり、イベントペイロードではありません。
バージョン 1
最初のバージョンは元のデバイス読み取り値を定義しています。
{
"type": "record",
"name": "DeviceTelemetryEventData",
"namespace": "Fabrikam.IoT",
"doc": "Device environmental readings.",
"fields": [
{ "name": "deviceid", "type": "string", "doc": "Unique device identifier." },
{ "name": "tenantid", "type": "string", "doc": "Tenant identifier." },
{ "name": "capturedAt", "type": { "type": "long", "logicalType": "timestamp-millis" }, "doc": "Capture time in UTC." },
{ "name": "temperatureC", "type": "double", "doc": "Temperature in degrees Celsius." },
{ "name": "humidityPct", "type": "double", "doc": "Relative humidity percentage." },
{ "name": "batteryPct", "type": "int", "doc": "Battery level percentage." }
]
}
バージョン 2
第2バージョンではファームウェア情報とnull可能な位置情報が追加されます。 ネストされたレコードは外側レコードの名前空間を継承します。
{
"type": "record",
"name": "DeviceTelemetryEventData",
"namespace": "Fabrikam.IoT",
"doc": "Device environmental readings with firmware and location.",
"fields": [
{ "name": "deviceid", "type": "string", "doc": "Unique device identifier." },
{ "name": "tenantid", "type": "string", "doc": "Tenant identifier." },
{ "name": "capturedAt", "type": { "type": "long", "logicalType": "timestamp-millis" }, "doc": "Capture time in UTC." },
{ "name": "temperatureC", "type": "double", "doc": "Temperature in degrees Celsius." },
{ "name": "humidityPct", "type": "double", "doc": "Relative humidity percentage." },
{ "name": "batteryPct", "type": "int", "doc": "Battery level percentage." },
{ "name": "firmwareVersion", "type": "string", "default": "1.0.0", "doc": "Firmware version reported by the device." },
{
"name": "location",
"type": [
"null",
{
"type": "record",
"name": "DeviceLocation",
"fields": [
{ "name": "latitude", "type": "double", "doc": "Latitude in decimal degrees." },
{ "name": "longitude", "type": "double", "doc": "Longitude in decimal degrees." }
]
}
],
"default": null,
"doc": "Optional location data."
}
]
}
バージョン 3
3つ目のバージョンではデバイスの健康状態と診断機能が追加されています。 配列とマップのデフォルトは空です。
{
"type": "record",
"name": "DeviceTelemetryEventData",
"namespace": "Fabrikam.IoT",
"doc": "Device environmental readings with health and diagnostics.",
"fields": [
{ "name": "deviceid", "type": "string", "doc": "Unique device identifier." },
{ "name": "tenantid", "type": "string", "doc": "Tenant identifier." },
{ "name": "capturedAt", "type": { "type": "long", "logicalType": "timestamp-millis" }, "doc": "Capture time in UTC." },
{ "name": "temperatureC", "type": "double", "doc": "Temperature in degrees Celsius." },
{ "name": "humidityPct", "type": "double", "doc": "Relative humidity percentage." },
{ "name": "batteryPct", "type": "int", "doc": "Battery level percentage." },
{ "name": "firmwareVersion", "type": "string", "default": "1.0.0", "doc": "Firmware version reported by the device." },
{
"name": "location",
"type": [
"null",
{
"type": "record",
"name": "DeviceLocation",
"fields": [
{ "name": "latitude", "type": "double", "doc": "Latitude in decimal degrees." },
{ "name": "longitude", "type": "double", "doc": "Longitude in decimal degrees." }
]
}
],
"default": null,
"doc": "Optional location data."
},
{
"name": "status",
"type": { "type": "enum", "name": "DeviceStatus", "symbols": ["ONLINE", "DEGRADED", "OFFLINE"] },
"default": "ONLINE",
"doc": "Current health status of the device."
},
{ "name": "diagnosticCodes", "type": { "type": "array", "items": "string" }, "default": [], "doc": "Active diagnostic codes." },
{ "name": "tags", "type": { "type": "map", "values": "string" }, "default": {}, "doc": "Additional key-value metadata." }
]
}
バージョン管理のワークフローを試してみてください
-
Fabrikam device telemetryというスキーマセットを作成します。 - 3つのファイルを一緒にインポートします。 インポートを確認する前に、検出された注文を確認してください。
- インポートしたスキーマを開き、バージョン3が最新の定義であることを確認してください。
- バージョン1に切り替え て、ファームウェア、位置情報、診断フィールドが表示されていないか確認してください。
- バージョン1とバージョン3を比較し、次にバージョン2とバージョン3を比較してください。
あるいは、バージョン1からスキーマを作成し、バージョン2と3を順番に更新する方法もあります。 手動更新と大量インポート結果を混同しないように、この練習用に別のスキーマセットを使いましょう。
Note
例では、違いを確認しやすくするために、変更を追加する形とデフォルト設定を使用しています。 Avroの定義におけるデフォルトが、すべてのプロデューサー、ペイロードエンコーディング、または宛先が変更を受け入れることを保証するわけではありません。 新しいバージョンを採用する前に、エンドツーエンドのパイプラインをテストしてください。