Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pavankjadda/f231d57134b305573539f7651f186c10 to your computer and use it in GitHub Desktop.
Save pavankjadda/f231d57134b305573539f7651f186c10 to your computer and use it in GitHub Desktop.
Create New Schema in Confluent Kafka Registry

See Confluent Registry for more information about this

Schema Files:

Customer.avsc

{"namespace": "com.kafkastream.model",
  "type": "record",
  "name": "Customer",
  "fields": [
    {"name": "customerId", "type": "string"},
    {"name": "firstName", "type": "string"},
    {"name": "lastName", "type": "string"},
    {"name": "email", "type": "string"},
    {"name": "phone", "type": "string"}
  ]
}

CustomerOrder.avsc

{"namespace": "com.kafkastream.model",
  "type": "record",
  "name": "CustomerOrder",
  "fields": [
    {"name": "customerId", "type": "string"},
    {"name": "firstName", "type": "string"},
    {"name": "lastName", "type": "string"},
    {"name": "email", "type": "string"},
    {"name": "phone", "type": "string"},
    {"name": "orderId", "type": "string"},
    {"name": "orderItemName", "type": "string"},
    {"name": "orderPlace", "type": "string"},
    {"name": "orderPurchaseTime", "type": "string"}
  ]
}

Valid format for Customer and Customer Orders to generate schemas and post it to Schema Registry

Customer:

curl -X POST -H "Content-Type:application/vnd.schemaregistry.v1+json" --data  '{   "schema":     "{       \"namespace\": \"com.kafkastream.model\",        \"type\": \"record\",        \"name\": \"customer-value\",        \"fields\":          [            {              \"type\": \"string\",              \"name\": \"customerId\"            },            {              \"type\": \"int\",              \"name\": \"firstName\"            },            {              \"type\": \"string\",              \"name\": \"lastName\"            },            {              \"type\": \"string\",              \"name\": \"email\"            },            {              \"type\": \"string\",              \"name\": \"phone\"            }          ]      }" }'  http://localhost:8081/subjects/customer-value/versions

CustomerOrder:

curl -X POST -H "Content-Type:application/vnd.schemaregistry.v1+json" --data  '{   "schema":     "{\"namespace\": \"com.kafkastream.model\",\r\n  \"type\": \"record\",\r\n  \"name\": \"CustomerOrder\",\r\n  \"fields\": [\r\n    {\"name\": \"customerId\", \"type\": \"string\"},\r\n    {\"name\": \"firstName\", \"type\": \"string\"},\r\n    {\"name\": \"lastName\", \"type\": \"string\"},\r\n    {\"name\": \"email\", \"type\": \"string\"},\r\n    {\"name\": \"phone\", \"type\": \"string\"},\r\n    {\"name\": \"orderId\", \"type\": \"string\"},\r\n    {\"name\": \"orderItemName\", \"type\": \"string\"},\r\n    {\"name\": \"orderPlace\", \"type\": \"string\"},\r\n    {\"name\": \"orderPurchaseTime\", \"type\": \"string\"}\r\n  ]\r\n}" }'  http://localhost:8081/subjects/customer-order-value/versions

Get or Delete schemas

curl -X GET http://localhost:8081/subjects/customer-value/versions/1
curl -X DELETE http://localhost:8081/subjects/customer-value/versions/1

Following works too

./kafka-avro-console-producer \
         --broker-list localhost:9092 --topic order \
         --property value.schema='{"namespace":"com.kafkastream.model","type":"record","name":"Order","fields":[{"name":"orderId","type":"string"},{"name":"customerId","type":"string"},{"name":"orderItemName","type":"string"},{"name":"orderPlace","type":"string"},{"name":"orderPurchaseTime","type":"string"}]}'


./kafka-avro-console-producer \
                  --broker-list localhost:9092 --topic customer \
                  --property value.schema='{"namespace":"com.kafkastream.model","type":"record","name":"Customer","fields":[{"name":"customerId","type":"string"},{"name":"firstName","type":"string"},{"name":"lastName","type":"string"},{"name":"email","type":"string"},{"name":"phone","type":"string"}]}'

./kafka-avro-console-producer \
                  --broker-list localhost:9092 --topic customer-order \
                  --property value.schema='{"namespace":"com.kafkastream.model","type":"record","name":"CustomerOrder","fields":[{"name":"customerId","type":"string"},{"name":"firstName","type":"string"},{"name":"lastName","type":"string"},{"name":"email","type":"string"},{"name":"phone","type":"string"},{"name":"orderId","type":"string"},{"name":"orderItemName","type":"string"},{"name":"orderPlace","type":"string"},{"name":"orderPurchaseTime","type":"string"}]}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment