You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/11/22 09:51:04 UTC

[GitHub] [pulsar] gaoran10 commented on a change in pull request #12914: [Doc] add explanations for Avro schema in Python client

gaoran10 commented on a change in pull request #12914:
URL: https://github.com/apache/pulsar/pull/12914#discussion_r754105818



##########
File path: site2/docs/client-libraries-python.md
##########
@@ -327,6 +272,198 @@ The schema definition is like this.
 }
 ```
 
+### Declare and validate schema
+
+You can send messages using `BytesSchema`, `StringSchema`, `AvroSchema`, and `JsonSchema`.
+
+<!--DOCUSAURUS_CODE_TABS-->
+
+<!--BytesSchema-->
+
+You can send byte data using a `BytesSchema`.
+
+**Example**
+
+```python
+producer = client.create_producer(
+                'bytes-schema-topic',
+                schema=BytesSchema())
+producer.send(b"Hello")
+
+consumer = client.subscribe(
+				'bytes-schema-topic',
+				'sub',
+				schema=BytesSchema())
+msg = consumer.receive()
+data = msg.value()
+```
+
+<!--StringSchema-->
+
+You can send string data using a `StringSchema`.
+
+**Example**
+
+```python
+producer = client.create_producer(
+                'string-schema-topic',
+                schema=StringSchema())
+producer.send("Hello")
+
+consumer = client.subscribe(
+				'string-schema-topic',
+				'sub',
+				schema=StringSchema())
+msg = consumer.receive()
+str = msg.value()
+```
+
+<!--AvroSchema-->
+
+You can declare an `AvroSchema` using one of the following methods.
+
+#### Method 1: Record
+
+You can declare an `AvroSchema` by passing a class that inherits
+from `pulsar.schema.Record` and defines the fields as
+class variables. 
+
+**Example**
+
+```python
+class Example(Record):
+    a = Integer()
+    b = Integer()
+
+producer = client.create_producer(
+                'avro-schema-topic',
+                schema=AvroSchema(Example))
+r = Example(a=1, b=2)
+producer.send(r)
+
+consumer = client.subscribe(
+				'avro-schema-topic',
+				'sub',
+				schema=AvroSchema(Example))
+msg = consumer.receive()
+e = msg.value()
+```
+
+After the producer is created, the Pulsar broker validates that the existing topic schema is "Avro" type and that the format is compatible with the schema definition of the `Example` class. If the format of the topic schema is incompatible with the schema definition, an exception occurs in the producer creation.

Review comment:
       Before creating a producer, the Pulsar broker will check the schema compatible first, if there is a schema compatible problem, an exception occurs in the producer creation.
   
   By the way, the schema compatible check applies to all schema types, maybe we don't need to mention it in AvroSchema.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org