예제 #1
0
def create_avro_schema(project_id, schema_id, avsc_file):
    """Create a schema resource from a JSON-formatted Avro schema file."""
    # [START pubsub_create_avro_schema]
    from google.api_core.exceptions import AlreadyExists
    from google.cloud.pubsub import SchemaServiceClient
    from google.pubsub_v1.types import Schema

    # TODO(developer): Replace these variables before running the sample.
    # project_id = "your-project-id"
    # schema_id = "your-schema-id"
    # avsc_file = "path/to/an/avro/schema/file/(.avsc)/formatted/in/json"

    project_path = f"projects/{project_id}"

    # Read a JSON-formatted Avro schema file as a string.
    with open(avsc_file, "rb") as f:
        avsc_source = f.read().decode("utf-8")

    schema_client = SchemaServiceClient()
    schema_path = schema_client.schema_path(project_id, schema_id)
    schema = Schema(name=schema_path,
                    type_=Schema.Type.AVRO,
                    definition=avsc_source)

    try:
        result = schema_client.create_schema(request={
            "parent": project_path,
            "schema": schema,
            "schema_id": schema_id
        })
        print(f"Created a schema using an Avro schema file:\n{result}")
    except AlreadyExists:
        print(f"{schema_id} already exists.")
예제 #2
0
def create_proto_schema(project_id, schema_id, proto_file):
    """Create a schema resource from a protobuf schema file."""
    # [START pubsub_create_proto_schema]
    from google.api_core.exceptions import AlreadyExists
    from google.cloud.pubsub import SchemaServiceClient
    from google.pubsub_v1.types import Schema

    # TODO(developer): Replace these variables before running the sample.
    # project_id = "your-project-id"
    # schema_id = "your-schema-id"
    # proto_file = "path/to/a/proto/file/(.proto)/formatted/in/protocol/buffers"

    project_path = f"projects/{project_id}"

    # Read a protobuf schema file as a string.
    with open(proto_file, "rb") as f:
        proto_source = f.read().decode("utf-8")

    schema_client = SchemaServiceClient()
    schema_path = schema_client.schema_path(project_id, schema_id)
    schema = Schema(name=schema_path,
                    type_=Schema.Type.PROTOCOL_BUFFER,
                    definition=proto_source)

    try:
        result = schema_client.create_schema(request={
            "parent": project_path,
            "schema": schema,
            "schema_id": schema_id
        })
        print(f"Created a schema using a protobuf schema file:\n{result}")
    except AlreadyExists:
        print(f"{schema_id} already exists.")
예제 #3
0
def list_schemas(project_id):
    """List schema resources."""
    # [START pubsub_list_schemas]
    from google.cloud.pubsub import SchemaServiceClient

    # TODO(developer): Replace these variables before running the sample.
    # project_id = "your-project-id"

    project_path = f"projects/{project_id}"
    schema_client = SchemaServiceClient()

    for schema in schema_client.list_schemas(request={"parent": project_path}):
        print(schema)

    print("Listed schemas.")
예제 #4
0
def create_topic_with_schema(project_id, topic_id, schema_id,
                             message_encoding):
    """Create a topic resource with a schema."""
    # [START pubsub_create_topic_with_schema]
    from google.api_core.exceptions import AlreadyExists, InvalidArgument
    from google.cloud.pubsub import PublisherClient, SchemaServiceClient
    from google.pubsub_v1.types import Encoding

    # TODO(developer): Replace these variables before running the sample.
    # project_id = "your-project-id"
    # topic_id = "your-topic-id"
    # schema_id = "your-schema-id"
    # Choose either BINARY or JSON as valid message encoding in this topic.
    # message_encoding = "BINARY"

    publisher_client = PublisherClient()
    topic_path = publisher_client.topic_path(project_id, topic_id)

    schema_client = SchemaServiceClient()
    schema_path = schema_client.schema_path(project_id, schema_id)

    if message_encoding == "BINARY":
        encoding = Encoding.BINARY
    elif message_encoding == "JSON":
        encoding = Encoding.JSON
    else:
        encoding = Encoding.ENCODING_UNSPECIFIED

    try:
        response = publisher_client.create_topic(
            request={
                "name": topic_path,
                "schema_settings": {
                    "schema": schema_path,
                    "encoding": encoding
                },
            })
        print(f"Created a topic:\n{response}")

    except AlreadyExists:
        print(f"{topic_id} already exists.")
    except InvalidArgument:
        print(
            "Please choose either BINARY or JSON as a valid message encoding type."
        )
예제 #5
0
def get_schema(project_id, schema_id):
    """Get a schema resource."""
    # [START pubsub_get_schema]
    from google.api_core.exceptions import NotFound
    from google.cloud.pubsub import SchemaServiceClient

    # TODO(developer): Replace these variables before running the sample.
    # project_id = "your-project-id"
    # schema_id = "your-schema-id"

    schema_client = SchemaServiceClient()
    schema_path = schema_client.schema_path(project_id, schema_id)

    try:
        result = schema_client.get_schema(request={"name": schema_path})
        print(f"Got a schema:\n{result}")
    except NotFound:
        print(f"{schema_id} not found.")
예제 #6
0
def schema_client():
    schema_client = SchemaServiceClient()
    yield schema_client
예제 #7
0
publisher = pubsub_v1.PublisherClient()
publisher_client = PublisherClient()

project_id = "gcplayproject"
schema_id = "userdetails"
avsc_file = "/home/googlecloud/mynewdata/avro.avsc"
topic_id = "schematopic"
topic_path = publisher.topic_path(project_id, topic_id)

project_path = f"projects/{project_id}"

# Read a JSON-formatted Avro schema file as a string.
with open(avsc_file, "rb") as f:
    avsc_source = f.read().decode("utf-8")

schema_client = SchemaServiceClient()
schema_path = schema_client.schema_path(project_id, schema_id)
schema = Schema(name=schema_path,
                type_=Schema.Type.AVRO,
                definition=avsc_source)


def createschema():
    try:
        result = schema_client.create_schema(request={
            "parent": project_path,
            "schema": schema,
            "schema_id": schema_id
        })
        print(f"Created a schema using an Avro schema file:\n{result}")
    except AlreadyExists:
예제 #8
0
def schema_client() -> Generator[pubsub_v1.SchemaServiceClient, None, None]:
    schema_client = SchemaServiceClient()
    yield schema_client