算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
data-schema-registry【设计与创意】Use this skill when asked about Schema Registry, Avro, Protobuf, schema evolution, compatibility, Confluent Schema Registry, Apicurio, serialization, deserialization, or schema validation. This skill enforces: Schema Registry architecture and deployment, Avro/Protobuf/JSON Schema definition, compatibility modes (BACKWARD, FORWARD, FULL, NONE), schema…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Design and deploy a schema registry with Avro/Protobuf/JSON Schema, compatibility enforcement, SerDe patterns, CI/CD integration, and governance for streaming and batch data.
Exact user phrases: "Schema Registry", "Avro", "Protobuf", "schema evolution", "compatibility", "Confluent Schema Registry", "Apicurio", "serialization", "deserialization", "schema validation", "subject", "SerDe".
Schema registry architecture with format selection, compatibility strategy, SerDe configuration, deployment plan, and CI/CD governance integration.
# Schema registry deployment
# Avro/Protobuf/JSON schema examples
# Compatibility rules per subject
# SerDe configuration (Kafka + batch)
# CI/CD schema enforcement pipeline
No preamble. No postamble. No explanations. No filler/hedging/transitions. Compress output — why use many token when few do trick.
350 lines of configuration.
| Format | Strengths | Weaknesses | Best For |
|---|---|---|---|
| Avro | Native Kafka/Confluent support, schema evolution, compact binary | Java-centric, limited JSON-like readability | Kafka streaming, Confluent ecosystem |
| Protobuf | Language-agnostic, fastest serialization, gRPC native | Schema evolution more strict, larger schemas | Cross-language, gRPC services |
| JSON Schema | Readable, widely understood, web-native | Larger payload, slower parsing | REST APIs, web frontends |
Default: Avro for Kafka/Confluent stacks. Protobuf for gRPC + streaming. JSON Schema for REST APIs only.
{
"type": "record", "name": "Order", "namespace": "com.org.data.orders",
"doc": "Order event schema",
"fields": [
{"name": "order_id", "type": "string", "doc": "Unique order identifier"},
{"name": "customer_id", "type": "string"},
{"name": "total_amount", "type": "double"},
{"name": "currency", "type": "string", "default": "USD"},
{"name": "status", "type": {"type": "enum", "name": "OrderStatus",
"symbols": ["PENDING", "CONFIRMED", "SHIPPED", "DELIVERED", "CANCELLED"]}},
{"name": "items", "type": {"type": "array", "items": {
"type": "record", "name": "LineItem",
"fields": [
{"name": "product_id", "type": "string"},
{"name": "quantity", "type": "int"},
{"name": "unit_price", "type": "double"}
]
}}},
{"name": "created_at", "type": {"type": "long", "logicalType": "timestamp-millis"}}
]
}
| Mode | Producer Change | Consumer Impact | Use Case |
|---|---|---|---|
| BACKWARD | Can delete fields, add optional | Old consumers read new data | Default for most |
| FORWARD | Can add fields, delete optional | New consumers read old data | Long-lived consumers |
| FULL | Add optional fields only | Both directions compatible | Strict governance |
| NONE | Any change allowed | Consumers must sync | Dev/test only |
services:
schema-registry:
image: confluentinc/cp-schema-registry:7.6.0
ports: ["8081:8081"]
environment:
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: PLAINTEXT://kafka:9092
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_COMPATIBILITY_LEVEL: BACKWARD
SCHEMA_REGISTRY_LISTENERS: http://0.0.0.0:8081
SCHEMA_REGISTRY_KAFKASTORE_TOPIC: _schemas
// Kafka Producer — Avro Serde with Schema Registry
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());
props.put(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://schema-registry:8081");
props.put(KafkaAvroSerializerConfig.AUTO_REGISTER_SCHEMAS, "false");
props.put(KafkaAvroSerializerConfig.USE_LATEST_VERSION, "true");
jobs:
schema-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Validate and Check Compatibility
run: |
for schema in schemas/**/*.avsc; do
python scripts/validate_and_register.py \
--subject "$(basename $schema .avsc)-value" \
--schema "$schema" --mode BACKWARD
done
- name: Register Schema
if: success() && github.ref == 'refs/heads/main'
run: |
for schema in schemas/**/*.avsc; do
subject=$(basename "$schema" .avsc)-value
python scripts/register_schema.py --subject "$subject" --schema "$schema"
done
| Rule | Rationale |
|---|---|
Always provide default for new fields | Ensures backward compatibility |
| Never remove a field without deprecation period | Avoids breaking consumers |
| Use enum types with care | Adding symbols is backward-safe, removing is not |
| Key subjects use FULL compatibility | Keys are referenced across topics |
| Auto-register schemas disabled in production | Prevents accidental schema registration |
AsyncAPI pairs with Schema Registry by documenting event-driven API message flow with topic names, publish/subscribe patterns, and payload schemas.
Buf enforces Protobuf lint rules and breaking change detection in CI/CD. buf breaking --against .git checks breaking changes against previous commit. Use for gRPC microservices requiring rigorous Protobuf governance.
The schema registry is the authoritative source for all streaming schemas. Producers must register schemas before writing data. Consumers fetch schemas from registry to deserialize. No schema should be hardcoded in application code — always reference the registry.
For organizations with multiple business units, use subject prefix isolation (commerce.orders-value, finance.invoices-value). Each tenant manages its own schemas within its prefix. The platform team manages registry infrastructure. Use Confluent Schema Registry's authorization mechanism with RBAC to isolate tenant access.
For breaking schema changes that cannot be avoided, follow a multi-step migration:
New schema needed
├── Kafka/Confluent ecosystem? → Avro
├── gRPC services / multi-language? → Protobuf
├── REST APIs / web frontends? → JSON Schema
└── Need both streaming + REST? → Avro for streaming, JSON Schema for REST
Subject type:
├── Key → FULL (keys are critical, must never break)
├── Value, production → BACKWARD (safe default)
├── Value, strict governance → FULL
├── Value, CDC/stream consumers → FORWARD
└── Dev/test → NONE
Deployment scale:
├── Single team, < 100 subjects → Single instance
├── Multi-team, < 1000 subjects → HA cluster (3 nodes)
├── Multi-region, < 10000 subjects → Per-registry + replication
└── Enterprise, global → Multi-registry federation
auto.register.schemas = false.<topic_name>-key and <topic_name>-value.commerce.orders-value).doc attribute in schema definition.| Feature | Confluent SR | Apicurio | Buf Schema Registry |
|---|---|---|---|
| Formats | Avro, Protobuf, JSON | Avro, Protobuf, JSON, GraphQL, OpenAPI | Protobuf only |
| Compatibility | BACKWARD, FORWARD, FULL, NONE, TRANSITIVE | Same + CUSTOM | Wire + Source compatibility |
| Deployment | Standalone, Confluent Cloud | Standalone, Red Hat | SaaS, self-hosted |
| Integrations | Kafka, KSQL, Connect | Kafka, Quarkus, Camel | gRPC, Connect |
| Governance | Client-side enforcement | Server-side rules | Lint + breaking change rules |
| Ecosystem | Largest Kafka ecosystem | Cloud-native Java | Protobuf-centric |
Schema registry vs data catalog: schema registry focuses on schema storage, compatibility checking, and providing schemas at serialization/deserialization time. Data catalog focuses on metadata management, discovery, and lineage. They are complementary: the schema registry feeds schemas to the catalog, and the catalog provides business context around schemas.
| Tool | Purpose |
|---|---|
| Confluent Schema Registry | Primary schema registry for Kafka |
| Apicurio | Alternative registry, multi-format support |
| Buf | Protobuf linting, breaking change detection, BSR |
| Avro Tools CLI | Schema validation, code generation |
| protoc | Protobuf compilation, code generation |
| AsyncAPI | Event-driven API documentation |
| Karapace | Open-source schema registry (Kafka-compatible API) |
| kcat | Kafka command-line tool with schema support |
| SchemaCrawler | Schema visualization and documentation |
syntax = "proto3";
package com.org.data.orders;
import "google/protobuf/timestamp.proto";
message Order {
string order_id = 1;
string customer_id = 2;
repeated LineItem line_items = 3;
double total_amount = 4;
Currency currency = 5;
OrderStatus status = 6;
google.protobuf.Timestamp created_at = 7;
google.protobuf.Timestamp updated_at = 8;
map<string, string> metadata = 9;
message LineItem {
string product_id = 1;
int32 quantity = 2;
double unit_price = 3;
}
enum Currency {
CURRENCY_UNSPECIFIED = 0;
USD = 1;
EUR = 2;
GBP = 3;
}
enum OrderStatus {
STATUS_UNSPECIFIED = 0;
PENDING = 1;
CONFIRMED = 2;
SHIPPED = 3;
DELIVERED = 4;
CANCELLED = 5;
}
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Order",
"type": "object",
"properties": {
"order_id": { "type": "string", "description": "Unique order identifier" },
"customer_id": { "type": "string" },
"line_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"product_id": { "type": "string" },
"quantity": { "type": "integer", "minimum": 1 },
"unit_price": { "type": "number", "minimum": 0 }
},
"required": ["product_id", "quantity", "unit_price"]
}
},
"total_amount": { "type": "number", "minimum": 0 },
"status": { "type": "string", "enum": ["pending", "confirmed", "shipped", "delivered", "cancelled"] },
"created_at": { "type": "string", "format": "date-time" }
},
"required": ["order_id", "customer_id", "total_amount"]
}
# Confluent Schema Registry HA deployment
registry_cluster:
nodes: 3 # Minimum 3 for quorum-based HA
storage: "kafka" # Uses internal Kafka topic _schemas
replication_factor: 3
kafka_bootstrap_servers: "broker1:9092,broker2:9092,broker3:9092"
# Multi-datacenter setup
primary_region: "us-east-1"
secondary_region: "us-west-2"
replication:
type: "active-standby" # Primary handles writes, standby serves reads
sync_interval: "5s" # Schema replication delay
# Security
ssl:
enabled: true
mutual_auth: true # mTLS for producer/consumer auth
rbac:
enabled: true # Role-based access control
admin_principal: "schema-admin"
# Apicurio Registry deployment (alternative, multi-format)
apicurio_registry:
storage: "sql" # PostgreSQL, SQL Server, or Kafka
formats: ["AVRO", "PROTOBUF", "JSON_SCHEMA", "ASYNCAPI", "OPENAPI"]
rules:
global: ["VALIDITY", "COMPATIBILITY"]
per_artifact: true
auth: "keycloak" # OIDC integration
ui: true
# Kafka Avro SerDe (Confluent)
kafka_producer_config:
key.serializer: "org.apache.kafka.common.serialization.StringSerializer"
value.serializer: "io.confluent.kafka.serializers.KafkaAvroSerializer"
schema.registry.url: "https://schema-registry:8081"
auto.register.schemas: false
use.latest.version: true # Use latest compatible schema version
kafka_consumer_config:
key.deserializer: "org.apache.kafka.common.serialization.StringDeserializer"
value.deserializer: "io.confluent.kafka.serializers.KafkaAvroDeserializer"
schema.registry.url: "https://schema-registry:8081"
specific.avro.reader: true
# Kafka Protobuf SerDe
kafka_protobuf_producer:
key.serializer: "org.apache.kafka.common.serialization.StringSerializer"
value.serializer: "io.confluent.kafka.serializers.protobuf.KafkaProtobufSerializer"
schema.registry.url: "https://schema-registry:8081"
# Batch processing with Avro (Spark)
spark_avro_config:
spark.sql.avro.compression.codec: "snappy"
spark.sql.avro.schema.literal: "{\"type\":\"record\",\"name\":\"Order\",...}" # Or use schema registry
# GitHub Action: validate schema compatibility on PR
schema_validation:
name: "Validate Schema Change"
steps:
- "Extract new schema version from PR diff"
- "Register in schema registry with compatibility check only (dry-run)"
- "Validate: compatibility_mode = BACKWARD"
- "If compatible: merge PR, register new schema version"
- "If breaking: reject PR, notify producer + consumer owners"
compatibility_rules:
BACKWARD:
- "New schema can read data written with old schema"
- "Allowed: adding optional fields, removing default fields"
- "Breaking: removing required fields, changing field types"
FORWARD:
- "Old schema can read data written with new schema"
- "Allowed: removing fields, adding default fields"
- "Breaking: adding required fields"
FULL:
- "Both backward and forward compatible"
- "Most restrictive — use for shared datasets"
# Example: CI check script
check_schema_compatibility:
cli: "curl -X POST https://registry:8081/compatibility/subjects/orders-value/versions \
-H 'Content-Type: application/vnd.schemaregistry.v1+json' \
-d '{\"schema\": \"$(cat new_schema.avsc | jq -Rs .)\"}'"
subject_naming:
pattern: "<domain>.<dataset-name>-<key|value>"
examples:
- "orders.order-events-value"
- "inventory.stock-updates-key"
- "analytics.user-sessions-value"
record_name_strategy:
# Confluent default: topic-name-value
# RecordNameStrategy: uses schema record name
# TopicRecordNameStrategy: topic-name + record name
use: "TopicRecordNameStrategy"
rationale: "Allows multiple record types per topic, better for union types"
migration_workflow:
phase_1_propose:
- "Create new schema version in development branch"
- "Run compatibility check against latest production version"
- "Document: what changed, why, expected impact"
- "Tag with semver change type (MAJOR/MINOR/PATCH)"
phase_2_review:
- "Notify all topic consumers of upcoming change"
- "Check consumer compatibility reports"
- "If BACKWARD compatible: standard review"
- "If breaking: consumer acceptance required, migration plan needed"
phase_3_deploy:
- "Register new schema version (not yet default)"
- "Stage deployment: producers and consumers upgrade gradually"
- "Phase A: all consumers upgraded to handle new schema (read-compatible)"
- "Phase B: producers switch to new schema version"
- "Phase C: old schema deprecated, retention period starts"
phase_4_cleanup:
- "Archive old schema versions after retention (typically 6 months)"
- "Remove deprecated field documentation from registry"
- "Update consumer documentation with new schema details"
Consumer deployment flexibility?
├── Consumers can upgrade at any time (same team/org)
│ └── BACKWARD compatibility (default, practical)
├── Consumers on fixed release cycles (out of sync)
│ └── FULL compatibility (both directions)
├── Producers need flexibility to iterate quickly
│ └── FORWARD compatibility (producers can remove fields)
├── Protobuf with wire compatibility
│ └── Use Protobuf built-in compatibility (field numbers never reused)
└── Experimental / dev topics
└── NONE (no compatibility enforcement, dev only)
Primary use case?
├── Kafka streaming, Confluent ecosystem
│ └── Avro (best tooling, native Confluent support)
├── Cross-language services, gRPC
│ └── Protobuf (fastest, most language bindings)
├── REST APIs, web frontends, NoSQL
│ └── JSON Schema (readable, web-native)
├── Event-driven APIs, AsyncAPI
│ └── JSON Schema with AsyncAPI wrapper
└── Mixed ecosystem
└── Apicurio (multi-format registry)
auto.register.schemas = false in all production producers<domain>.<topic-name>-<key/value> conventiondoc attributedata-data-platform for registry deployment. data-data-catalog for schema metadata. data-data-contracts for data contract schema integration. data-data-observability for schema drift monitoring.