使用协议缓冲区文件创建架构资源。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
C++
在尝试此示例之前,请按照《Pub/Sub 快速入门:使用客户端库》中的 C++ 设置说明进行操作。如需了解详情,请参阅 Pub/Sub C++ API 参考文档。
namespace pubsub = google::cloud::pubsub;
[](pubsub::SchemaAdminClient client, std::string const& project_id,
std::string const& schema_id) {
auto constexpr kDefinition = R"pfile(
syntax = "proto3";
package google.cloud.pubsub.samples;
message State {
string name = 1;
string post_abbr = 2;
}
)pfile";
auto schema = client.CreateProtobufSchema(
pubsub::Schema(project_id, schema_id), kDefinition);
if (schema.status().code() == google::cloud::StatusCode::kAlreadyExists) {
std::cout << "The schema already exists\n";
return;
}
if (!schema) return; // TODO(#4792) - protobuf schema support in emulator
std::cout << "Schema successfully created: " << schema->DebugString()
<< "\n";
}
Java
在尝试此示例之前,请按照《Pub/Sub 快速入门:使用客户端库》中的 Java 设置说明执行操作。如需了解详情,请参阅 Pub/Sub Java API 参考文档。
import com.google.api.gax.rpc.AlreadyExistsException;
import com.google.cloud.pubsub.v1.SchemaServiceClient;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Schema;
import com.google.pubsub.v1.SchemaName;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class CreateProtoSchemaExample {
public static void main(String... args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String schemaId = "your-schema-id";
String protoFile = "path/to/a/proto/file/(.proto)/formatted/in/protocol/buffers";
createProtoSchemaExample(projectId, schemaId, protoFile);
}
public static void createProtoSchemaExample(String projectId, String schemaId, String protoFile)
throws IOException {
ProjectName projectName = ProjectName.of(projectId);
SchemaName schemaName = SchemaName.of(projectId, schemaId);
// Read a proto file as a string.
String protoSource = new String(Files.readAllBytes(Paths.get(protoFile)));
try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
Schema schema =
schemaServiceClient.createSchema(
projectName,
Schema.newBuilder()
.setName(schemaName.toString())
.setType(Schema.Type.PROTOCOL_BUFFER)
.setDefinition(protoSource)
.build(),
schemaId);
System.out.println("Created a schema using a protobuf schema:\n" + schema);
} catch (AlreadyExistsException e) {
System.out.println(schemaName + "already exists.");
}
}
}
Python
在尝试此示例之前,请按照《Pub/Sub 快速入门:使用客户端库》中的 Python 设置说明执行操作。如需了解详情,请参阅 Pub/Sub Python API 参考文档。
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.")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器