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

13 files changed

+119
-139
lines changed

examples/Makefile

+3-3
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

+4-12
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

+18
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

+19
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

+14-14
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

+13-12
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

+2-2
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

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/protobuf_consumer.py

+2-2
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

examples/protobuf_producer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
from six.moves import input
3636

37-
# Protobuf generated class; resides at ./user_pb2.py
38-
import user_pb2
37+
# Protobuf generated class; resides at ./protobuf/user_pb2.py
38+
import protobuf.user_pb2 as user_pb2
3939
from confluent_kafka import SerializingProducer
4040
from confluent_kafka.serialization import StringSerializer
4141
from confluent_kafka.schema_registry import SchemaRegistryClient
@@ -75,7 +75,7 @@ def main(args):
7575

7676
protobuf_serializer = ProtobufSerializer(user_pb2.User,
7777
schema_registry_client,
78-
{'use.deprecated.format': True})
78+
{'use.deprecated.format': False})
7979

8080
producer_conf = {'bootstrap.servers': args.bootstrap_servers,
8181
'key.serializer': StringSerializer('utf_8'),

examples/user_pb2.py

-83
This file was deleted.

src/confluent_kafka/admin/__init__.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,14 @@ def describe_acls(self, acl_binding_filter, **kwargs):
402402
must match.
403403
String attributes match exact values or any string if set to None.
404404
Enums attributes match exact values or any value if equal to `ANY`.
405-
If :class:`ResourcePatternType` is set to :attr:`ResourcePatternType.MATCH` returns ACL bindings with:
406-
:attr:`ResourcePatternType.LITERAL` pattern type with resource name equal to the given resource name;
407-
:attr:`ResourcePatternType.LITERAL` pattern type with wildcard resource name that matches the given resource name;
408-
:attr:`ResourcePatternType.PREFIXED` pattern type with resource name that is a prefix of the given resource name
405+
If :class:`ResourcePatternType` is set to :attr:`ResourcePatternType.MATCH`
406+
returns ACL bindings with:
407+
:attr:`ResourcePatternType.LITERAL` pattern type with resource name equal
408+
to the given resource name;
409+
:attr:`ResourcePatternType.LITERAL` pattern type with wildcard resource name
410+
that matches the given resource name;
411+
:attr:`ResourcePatternType.PREFIXED` pattern type with resource name
412+
that is a prefix of the given resource name
409413
:param float request_timeout: The overall request timeout in seconds,
410414
including broker lookup, request transmission, operation time
411415
on broker, and response. Default: `socket.timeout.ms*1000.0`
@@ -433,10 +437,14 @@ def delete_acls(self, acl_binding_filters, **kwargs):
433437
to match ACLs to delete.
434438
String attributes match exact values or any string if set to None.
435439
Enums attributes match exact values or any value if equal to `ANY`.
436-
If :class:`ResourcePatternType` is set to :attr:`ResourcePatternType.MATCH` deletes ACL bindings with:
437-
:attr:`ResourcePatternType.LITERAL` pattern type with resource name equal to the given resource name;
438-
:attr:`ResourcePatternType.LITERAL` pattern type with wildcard resource name that matches the given resource name;
439-
:attr:`ResourcePatternType.PREFIXED` pattern type with resource name that is a prefix of the given resource name
440+
If :class:`ResourcePatternType` is set to :attr:`ResourcePatternType.MATCH`
441+
deletes ACL bindings with:
442+
:attr:`ResourcePatternType.LITERAL` pattern type with resource name
443+
equal to the given resource name;
444+
:attr:`ResourcePatternType.LITERAL` pattern type with wildcard resource name
445+
that matches the given resource name;
446+
:attr:`ResourcePatternType.PREFIXED` pattern type with resource name
447+
that is a prefix of the given resource name
440448
:param float request_timeout: The overall request timeout in seconds,
441449
including broker lookup, request transmission, operation time
442450
on broker, and response. Default: `socket.timeout.ms*1000.0`

0 commit comments

Comments
 (0)