Skip to content

Commit d189075

Browse files
authored
generic and specific avro examples (confluentinc#1381)
* generic and specific avro examples moved protobuf to a similiar folder * removed empty __init__.py
1 parent 01e4200 commit d189075

File tree

13 files changed

+119
-139
lines changed

13 files changed

+119
-139
lines changed

examples/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
user_pb2.py: user.proto
2-
protoc -I=. --python_out=. ./user.proto;
1+
user_pb2.py: protobuf/user.proto
2+
cd protobuf && protoc -I=. --python_out=. ./user.proto;
33

44
clean:
5-
rm -f $(TARGET_DIR)/*_pb2.py
5+
rm -f $(TARGET_DIR)/protobuf/*_pb2.py

examples/avro-cli.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,17 @@
1616
#
1717

1818
import argparse
19+
import os
1920
from uuid import uuid4
2021

2122
from six.moves import input
2223

2324
from confluent_kafka import avro
2425

2526
# Parse Schema used for serializing User class
26-
record_schema = avro.loads("""
27-
{
28-
"namespace": "confluent.io.examples.serialization.avro",
29-
"name": "User",
30-
"type": "record",
31-
"fields": [
32-
{"name": "name", "type": "string"},
33-
{"name": "favorite_number", "type": "int"},
34-
{"name": "favorite_color", "type": "string"}
35-
]
36-
}
37-
""")
27+
path = os.path.realpath(os.path.dirname(__file__))
28+
with open(f"{path}/avro/user_specific.avsc") as f:
29+
record_schema = avro.loads(f.read())
3830

3931

4032
class User(object):

examples/avro/user_generic.avsc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "User",
3+
"type": "record",
4+
"fields": [
5+
{
6+
"name": "name",
7+
"type": "string"
8+
},
9+
{
10+
"name": "favorite_number",
11+
"type": "long"
12+
},
13+
{
14+
"name": "favorite_color",
15+
"type": "string"
16+
}
17+
]
18+
}

examples/avro/user_specific.avsc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"namespace": "confluent.io.examples.serialization.avro",
3+
"name": "User",
4+
"type": "record",
5+
"fields": [
6+
{
7+
"name": "name",
8+
"type": "string"
9+
},
10+
{
11+
"name": "favorite_number",
12+
"type": "long"
13+
},
14+
{
15+
"name": "favorite_color",
16+
"type": "string"
17+
}
18+
]
19+
}

examples/avro_consumer.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# This is a simple example of the SerializingProducer using Avro.
2121
#
2222
import argparse
23+
import os
2324

2425
from confluent_kafka import DeserializingConsumer
2526
from confluent_kafka.schema_registry import SchemaRegistryClient
@@ -66,19 +67,16 @@ def dict_to_user(obj, ctx):
6667

6768
def main(args):
6869
topic = args.topic
70+
is_specific = args.specific == "true"
6971

70-
schema_str = """
71-
{
72-
"namespace": "confluent.io.examples.serialization.avro",
73-
"name": "User",
74-
"type": "record",
75-
"fields": [
76-
{"name": "name", "type": "string"},
77-
{"name": "favorite_number", "type": "int"},
78-
{"name": "favorite_color", "type": "string"}
79-
]
80-
}
81-
"""
72+
if is_specific:
73+
schema = "user_specific.avsc"
74+
else:
75+
schema = "user_generic.avsc"
76+
77+
path = os.path.realpath(os.path.dirname(__file__))
78+
with open(f"{path}/avro/{schema}") as f:
79+
schema_str = f.read()
8280

8381
sr_conf = {'url': args.schema_registry}
8482
schema_registry_client = SchemaRegistryClient(sr_conf)
@@ -110,8 +108,8 @@ def main(args):
110108
"\tfavorite_number: {}\n"
111109
"\tfavorite_color: {}\n"
112110
.format(msg.key(), user.name,
113-
user.favorite_color,
114-
user.favorite_number))
111+
user.favorite_number,
112+
user.favorite_color))
115113
except KeyboardInterrupt:
116114
break
117115

@@ -129,5 +127,7 @@ def main(args):
129127
help="Topic name")
130128
parser.add_argument('-g', dest="group", default="example_serde_avro",
131129
help="Consumer group")
130+
parser.add_argument('-p', dest="specific", default="true",
131+
help="Avro specific record")
132132

133133
main(parser.parse_args())

examples/avro_producer.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# This is a simple example of the SerializingProducer using Avro.
2121
#
2222
import argparse
23+
import os
2324
from uuid import uuid4
2425

2526
from six.moves import input
@@ -99,19 +100,17 @@ def delivery_report(err, msg):
99100

100101
def main(args):
101102
topic = args.topic
103+
is_specific = args.specific == "true"
104+
105+
if is_specific:
106+
schema = "user_specific.avsc"
107+
else:
108+
schema = "user_generic.avsc"
109+
110+
path = os.path.realpath(os.path.dirname(__file__))
111+
with open(f"{path}/avro/{schema}") as f:
112+
schema_str = f.read()
102113

103-
schema_str = """
104-
{
105-
"namespace": "confluent.io.examples.serialization.avro",
106-
"name": "User",
107-
"type": "record",
108-
"fields": [
109-
{"name": "name", "type": "string"},
110-
{"name": "favorite_number", "type": "int"},
111-
{"name": "favorite_color", "type": "string"}
112-
]
113-
}
114-
"""
115114
schema_registry_conf = {'url': args.schema_registry}
116115
schema_registry_client = SchemaRegistryClient(schema_registry_conf)
117116

@@ -158,5 +157,7 @@ def main(args):
158157
help="Schema Registry (http(s)://host[:port]")
159158
parser.add_argument('-t', dest="topic", default="example_serde_avro",
160159
help="Topic name")
160+
parser.add_argument('-p', dest="specific", default="true",
161+
help="Avro specific record")
161162

162163
main(parser.parse_args())

examples/json_consumer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def main(args):
116116
"\tfavorite_number: {}\n"
117117
"\tfavorite_color: {}\n"
118118
.format(msg.key(), user.name,
119-
user.favorite_color,
120-
user.favorite_number))
119+
user.favorite_number,
120+
user.favorite_color))
121121
except KeyboardInterrupt:
122122
break
123123

File renamed without changes.

examples/protobuf/user_pb2.py

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/protobuf_consumer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#
3232
import argparse
3333

34-
# Protobuf generated class; resides at ./user_pb2.py
35-
import user_pb2
34+
# Protobuf generated class; resides at ./protobuf/user_pb2.py
35+
import protobuf.user_pb2 as user_pb2
3636
from confluent_kafka import DeserializingConsumer
3737
from confluent_kafka.schema_registry.protobuf import ProtobufDeserializer
3838
from confluent_kafka.serialization import StringDeserializer

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy