import json
import logging
import argparse
from google.protobuf.wrappers_pb2 import Int64Value
import doublecloud
from doublecloud.kafka.v1.cluster_pb2 import ClusterResources
from doublecloud.kafka.v1.cluster_service_pb2 import CreateClusterRequest
from doublecloud.kafka.v1.cluster_service_pb2_grpc import ClusterServiceStub
from doublecloud.kafka.v1.config_pb2 import SchemaRegistryConfig
defcreate_cluster(sdk, project_id, region_id, name, network_id):
cluster_service = sdk.client(ClusterServiceStub)
operation = cluster_service.Create(
CreateClusterRequest(
project_id=project_id,
cloud_type="aws",
region_id="eu-central-1",
name="quickstart-cluster",
resources=ClusterResources(
kafka=ClusterResources.Kafka(
resource_preset_id="s2-c2-m4",
disk_size=Int64Value(value=34359738368),
broker_count=Int64Value(value=1),
zone_count=Int64Value(value=3),
)
),
network_id=network_id,
schema_registry_config=SchemaRegistryConfig(enabled=True) # enable schema registry for the cluster
)
)
logging.info("Creating initiated")
return operation
Note
The DoubleCloud service creates the superuser admin and its password automatically. You can find both the User and the Password in the Overview tab on the cluster information page.
Create a topic
After you've created a cluster, you also need to create a topic for messages:
API
Use the TopicServicecreate method and pass the following parameters:
cluster_id - the ID of the cluster in which you want to create a topic. To find the cluster ID, get a list of clusters in the project.
topic_spec - let's configure the required topic specifications:
name - specify the topic name, first-topic.
partitions - set the minimum number of partitions for this quickstart, 1.
replication_factor - go for the basic option here as well, specify 1.
topic_config_3: use the doublecloud.kafka.v1.TopicConfig3 model to set the further topic configuration for Apache Kafka® version 3 and above:
cleanup policy - set the cleanup policy for the topic, in this case CLEANUP_POLICY_DELETE.
compression_type - we don't need compression for this tutorial, specify COMPRESSION_TYPE_UNCOMPRESSED.
retention_bytes - 1048576 (1 Mb).
retention_ms - 600000 (10 minutes).
Connect to your cluster
When you have a cluster and a topic in it, connect to the cluster and transfer a text message between a consumer and a producer:
Run a command that contains a connection string to create a consumer. You can use the Connection string from the Overview tab on your cluster information page. The command has the following structure: