用模式注册表中的主题注册avro模式

lnxxn5zx  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(591)

我以以下方式运行zookeeper、kafka和schema registry:


# zookeper

docker run -d -it \
    --net=host \
    --name=zookeeper \
    -e ZOOKEEPER_CLIENT_PORT=32181 \
    confluentinc/cp-zookeeper:4.0.0

# kafka

docker run -d \
    --net=host \
    --name=kafka \
    -e KAFKA_ZOOKEEPER_CONNECT=localhost:32181 \
    -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:29092 \
    -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
    confluentinc/cp-kafka:4.0.0

# schema-registry

docker run -d \
  --net=host \
  --name=schema-registry \
  -e SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL=localhost:32181 \
  -e SCHEMA_REGISTRY_HOST_NAME=localhost \
  -e SCHEMA_REGISTRY_LISTENERS=http://localhost:8081 \
  confluentinc/cp-schema-registry:4.0.0

我在Kafka中手动创建了一个主题,如下所示:

kafka-topics --create --topic newflow --partitions 1 --replication-factor 1 --if-not-exists --zookeeper localhost:32181

我可以通过以下方式为主题分配模式:

kafka-avro-console-producer --broker-list localhost:29092 --topic newflow --property value.schema='{"type":"record","name":"myrecord","fields":[{"name":"A","type":"int"},{"name":"B","type":"long"},{"name":"C","type":"string"},{"name":"D","type":"int"}]}'

按以下方式restfully点击url时,我无法在schema registry中找到其架构:

curl -X GET http://localhost:8081/subjects
[]
dkqlctbz

dkqlctbz1#

可以通过curl选项在schema registry中以以下方式显式注册架构:

curl -X POST -H "Content-Type:application/vnd.schemaregistry.v1+json" \
--data '{"schema": "{\"type\":\"record\",\"name\":\"myrecord\",\"fields\":[{\"name\":\"A\",\"type\":\"int\"},{\"name\":\"B\",\"type\":\"long\"},{\"name\":\"C\",\"type\":\"string\"},{\"name\":\"D\",\"type\":\"int\"}]}"}' \
http://localhost:8081/subjects/newflow-value/versions

相关问题