Los temas pueden usar esquemas para definir un formato que deben seguir sus mensajes. Cuando te suscribes a un tema con un esquema, se garantiza que los mensajes enviados al suscriptor sean mensajes válidos. Estos mensajes se ajustan al tipo y la codificación especificados en la configuración del esquema asociada con el tema.
El suscriptor puede determinar la configuración del esquema asociada con un tema observa los siguientes atributos:
googclient_schemaname
: El nombre del esquema que se usa para la validación. Si se borra el esquema, el nombre es_deleted-schema_
.googclient_schemaencoding
: Es la codificación del mensaje, ya sea JSON o BINARY.googclient_schemarevisionid
: Es el ID de revisión del esquema que se usa para analizar y validar el mensaje. Cada revisión tiene un o el ID de revisión único asociado a ella. El ID de revisión es un un UUID de ocho caracteres generado automáticamente.
Para obtener más información sobre los esquemas, consulta Descripción general de los esquemas.
Muestras de código para suscribirse a temas asociados con un esquema
En estos ejemplos, se muestra cómo procesar mensajes cuando te suscribes a temas configurados con un esquema.
C++
Antes de probar esta muestra, sigue las instrucciones de configuración de C++ en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C++.
Avro ProtoC#
Antes de probar esta muestra, sigue las instrucciones de configuración de C# en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C#.
Avro ProtoGo
Antes de probar esta muestra, sigue las instrucciones de configuración de Go en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Go.
Avro ProtoJava
Antes de probar esta muestra, sigue las instrucciones de configuración de Go en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Go.
Avro Búfer de protocoloNode.js
Antes de probar esta muestra, sigue las instrucciones de configuración de Node.js en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Node.js.
Avro Búfer de protocoloNode.js
Antes de probar esta muestra, sigue las instrucciones de configuración de Node.js en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Node.js.
Avro Búfer de protocoloPHP
Antes de probar esta muestra, sigue las instrucciones de configuración de PHP en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para PHP.
Avro Búfer de protocoloPython
Antes de probar esta muestra, sigue las instrucciones de configuración de Python en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Python.
Avro Búfer de protocoloRuby
Antes de probar esta muestra, sigue las instrucciones de configuración de Ruby en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Ruby.
Avro Búfer de protocoloSuscribirse a un tema asociado a un esquema de Avro con revisiones
Avro requiere que los mensajes se analicen con el esquema con el que están o codificado. También puedes traducir mensajes a un esquema diferente Usa la resolución de esquema Avro.
Pub/Sub garantiza que todas las revisiones del esquema se reenvíen es retrocompatible con todas las demás revisiones. Esta compatibilidad permite que cualquier revisión para usar como esquema de lector o escritor.
Cuando analizas un mensaje codificado con una revisión de esquema diferente que la que usa tu suscriptor, es posible que debas obtener del esquema original y lo pasas como esquema de escritor.
Es mejor almacenar en caché el objeto del lector de Avro que puede analizar los mensajes
cada revisión del esquema que se encuentra para minimizar la latencia
y minimizar la cantidad de llamadas a la API de GetSchema
.
El siguiente código muestra estas funciones:
Lee los atributos que se analizaron en la sección anterior para determinar qué revisión del esquema se usa para codificar el mensaje.
Recupera la revisión del esquema y almacena en caché un lector generado con ella.
Analiza el mensaje en el esquema que usa tu suscriptor.
Go
Antes de probar esta muestra, sigue las instrucciones de configuración de Go en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Go.
Java
Antes de probar esta muestra, sigue las instrucciones de configuración de Java en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Java.
Python
Antes de probar esta muestra, sigue las instrucciones de configuración de Python en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Python.
Roles obligatorios
A fin de obtener los permisos que necesitas para validar un mensaje con un esquema, completa uno de los siguientes pasos:
- Otorga uno de los siguientes roles predefinidos a una cuenta de servicio:
roles/pubsub.admin
,roles/pubsub.editor
oroles/pubsub.viewer
. Crea un rol personalizado para una cuenta de servicio y agrega los siguientes permisos
pubsub.schemas.validate
ypubsub.schemas.get
.Para obtener más información sobre los roles personalizados, consulta Crea y administra roles de IAM personalizados.