ตัวอย่างสคีมา Avro telemetry ของอุปกรณ์

ใช้คําจํากัดความ Avro ทั้งสามนี้เพื่อลองสร้างสคีมา นําเข้าแบบกลุ่ม และเปรียบเทียบเวอร์ชัน ตัวอย่างนี้อธิบายอุปกรณ์ที่รายงานข้อมูลสิ่งแวดล้อม เฟิร์มแวร์ ตําแหน่ง และข้อมูลวินิจฉัย

แต่ละเวอร์ชันจะคงชื่อ DeviceTelemetryEventData ระเบียนและเนมสเปซ Fabrikam.IoTไว้ ชื่อไฟล์ระบุเวอร์ชันตัวอย่าง;จะไม่แทนที่หมายเลขเวอร์ชันที่รีจิสทรีกําหนด

เวอร์ชัน การเปลี่ยนแปลง ชื่อไฟล์
1 กําหนดตัวระบุอุปกรณ์และผู้เช่า เวลาการจับข้อมูล อุณหภูมิ ความชื้น และการอ่านค่าแบตเตอรี่ Fabrikam.DeviceTelemetryEventData.v1.avsc
2 เพิ่มข้อมูลเฟิร์มแวร์และบันทึกตําแหน่งซ้อนแบบเสริม Fabrikam.DeviceTelemetryEventData.v2.avsc
3 เพิ่ม enum สถานะ, อาร์เรย์ของรหัสวินิจฉัย และแผนที่ของแท็ก 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

เวอร์ชันที่สองจะเพิ่มข้อมูลเฟิร์มแวร์และบันทึกตําแหน่งที่สามารถลบได้ ระเบียนซ้อนจะสืบทอดเนมสเปซของระเบียนภายนอก

{
  "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

เวอร์ชันที่สามเพิ่มสุขภาพอุปกรณ์และการวินิจฉัย อาร์เรย์และแมปมีค่าเริ่มต้นว่างเปล่า

{
  "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." }
  ]
}

ลองใช้เวิร์กโฟลว์การจัดเวอร์ชัน

  1. สร้างชุดสคีมา ชื่อ Fabrikam device telemetry.
  2. นําเข้า ไฟล์ทั้งสามพร้อมกัน ตรวจสอบคําสั่งซื้อที่ตรวจพบก่อนยืนยันการนําเข้า
  3. เปิดสคีมาที่นําเข้าและตรวจสอบว่าเวอร์ชัน 3 เป็นนิยามล่าสุด
  4. เปลี่ยนไปใช้เวอร์ชัน 1 และยืนยันว่าไม่มีฟิลด์เฟิร์มแวร์ ตําแหน่ง และฟิลด์วินิจฉัย
  5. เปรียบเทียบเวอร์ชัน 1 กับเวอร์ชัน 3 แล้วเปรียบเทียบเวอร์ชัน 2 กับเวอร์ชัน 3

อีกทางเลือกหนึ่งคือสร้างสคีมาจากเวอร์ชัน 1 และอัปเดตเป็นเวอร์ชัน 2 และ 3 ตามลําดับ ใช้ชุดสคีมาแยกต่างหากสําหรับแบบฝึกหัดนี้ เพื่อไม่ให้การอัปเดตด้วยตนเองผสมกับผลลัพธ์การนําเข้าจํานวนมาก

Note

ตัวอย่างใช้การเปลี่ยนแปลงแบบบวกและค่าเริ่มต้นเพื่อให้ง่ายต่อการตรวจสอบความแตกต่าง ค่าเริ่มต้นในนิยาม Avro ไม่ได้รับประกันว่าผู้ผลิต การเข้ารหัสเพย์โหลด หรือปลายทางทุกแห่งจะยอมรับการเปลี่ยนแปลง ทดสอบกระบวนการตั้งแต่ต้นจนจบของคุณก่อนนําเวอร์ชันใหม่มาใช้