예제 #1
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.")
예제 #2
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.")