租户基础知识

租户是拥有向其分配的职位和公司对象的实体。他们在 Google Cloud 项目和您上传的数据之间构成了一个组织中间层。借助租户,您可以阻止任何数据跨租户共享,从而隔离不同的数据分组,无需设置多个项目。如果您为多个客户提供服务,而不希望在他们之间共享数据,但仍希望保留单个 Google Cloud 项目以进行内部结算和报告,那么设置多个租户非常有用。例如:

  • 招聘求职网站提供商为具有多个子公司的组织构建招聘求职网站。
  • 招聘代理机构为多个企业搭建求职者跟踪系统。

每个 Google Cloud 项目都为一个默认租户分配了一个 tenant_id。您也可以在给定项目中创建多个租户而不使用默认值。

各个租户彼此完全隔离。所有 API 只接受单个租户,以避免在单次 API 调用中针对多个租户查询数据。机器学习 (ML) 同样将租户视为离散单元,不会跨租户处理数据。一个项目可以根据需要支持任意数量的租户。

默认租户

Job search v4 和更新的版本都需要一个租户实体。所有项目都被分配有一个默认租户的 tenant_id。如果您不想使用多个租户,可以采用以下选项:

  1. 使用项目的默认租户(推荐)。使用默认租户时,您无需专门引用该租户。您可以使用 projects/{project_id}/jobs/{job_id} 格式,Cloud Talent Solution 后端将假定您正在使用默认租户。

  2. 创建单个租户并使其代替默认租户。如果您自行创建租户,则需要对其进行引用:projects/{project_id}/tenants/{tenant_id}/jobs/{job_id}

默认 tenant_id 对于每个项目都是唯一的,且永远不会被您创建的其他租户所覆盖或替换。您不能对默认租户调用 CRUD 方法。

创建的租户(可选)

您可以选择自行创建租户。CTS 后端为每个职位搜索项目分配了一个默认租户。如果您不想使用多租户来分隔数据,我们建议您使用默认租户。

创建租户

您需要创建一个唯一的 externalId 值并将其分配给新的租户。然后,调用会返回由我们的后端系统分配的唯一 name,此值也会分配给该租户。请务必记录和存储 nameexternalId 值,因为在更新/删除/引用时会用到它们。

以下代码示例将创建一个新租户:

Java

如需了解如何安装和使用适用于 CTS 的客户端库,请参阅 CTS 客户端库。 有关详情,请参阅 CTS Java API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.cloud.talent.v4.CreateTenantRequest;
import com.google.cloud.talent.v4.ProjectName;
import com.google.cloud.talent.v4.Tenant;
import com.google.cloud.talent.v4.TenantServiceClient;
import java.io.IOException;

public class JobSearchCreateTenant {

  public static void createTenant() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String externalId = "your-external-id";
    createTenant(projectId, externalId);
  }

  // Create Tenant for scoping resources, e.g. companies and jobs.
  public static void createTenant(String projectId, String externalId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) {
      ProjectName parent = ProjectName.of(projectId);
      Tenant tenant = Tenant.newBuilder().setExternalId(externalId).build();

      CreateTenantRequest request =
          CreateTenantRequest.newBuilder().setParent(parent.toString()).setTenant(tenant).build();

      Tenant response = tenantServiceClient.createTenant(request);
      System.out.println("Created Tenant");
      System.out.format("Name: %s%n", response.getName());
      System.out.format("External ID: %s%n", response.getExternalId());
    }
  }
}

Node.js

如需了解如何安装和使用 CTS 客户端库,请参阅 CTS 客户端库。 有关详情,请参阅 CTS Node.js API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


const talent = require('@google-cloud/talent').v4;

/** Create Tenant for scoping resources, e.g. companies and jobs */
function sampleCreateTenant(projectId, externalId) {
  const client = new talent.TenantServiceClient();
  // const projectId = 'Your Google Cloud Project ID';
  // const externalId = 'Your Unique Identifier for Tenant';
  const formattedParent = client.projectPath(projectId);
  const tenant = {
    externalId: externalId,
  };
  const request = {
    parent: formattedParent,
    tenant: tenant,
  };
  client
    .createTenant(request)
    .then(responses => {
      const response = responses[0];
      console.log('Created Tenant');
      console.log(`Name: ${response.name}`);
      console.log(`External ID: ${response.externalId}`);
    })
    .catch(err => {
      console.error(err);
    });
}

Python

如需了解如何安装和使用适用于 CTS 的客户端库,请参阅 CTS 客户端库。 有关详情,请参阅 CTS Python API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


from google.cloud import talent


def create_tenant(project_id, external_id):
    """Create Tenant for scoping resources, e.g. companies and jobs"""

    client = talent.TenantServiceClient()

    # project_id = 'Your Google Cloud Project ID'
    # external_id = 'Your Unique Identifier for Tenant'

    if isinstance(project_id, bytes):
        project_id = project_id.decode("utf-8")
    if isinstance(external_id, bytes):
        external_id = external_id.decode("utf-8")
    parent = f"projects/{project_id}"
    tenant = talent.Tenant(external_id=external_id)

    response = client.create_tenant(parent=parent, tenant=tenant)
    print("Created Tenant")
    print(f"Name: {response.name}")
    print(f"External ID: {response.external_id}")
    return response.name

检索租户

Java

如需了解如何安装和使用适用于 CTS 的客户端库,请参阅 CTS 客户端库。 如需了解详情,请参阅 CTS Java API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.cloud.talent.v4.GetTenantRequest;
import com.google.cloud.talent.v4.Tenant;
import com.google.cloud.talent.v4.TenantName;
import com.google.cloud.talent.v4.TenantServiceClient;
import java.io.IOException;

public class JobSearchGetTenant {

  public static void getTenant() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String tenantId = "your-tenant-id";
    getTenant(projectId, tenantId);
  }

  // Get Tenant by name.
  public static void getTenant(String projectId, String tenantId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) {
      TenantName name = TenantName.of(projectId, tenantId);

      GetTenantRequest request = GetTenantRequest.newBuilder().setName(name.toString()).build();

      Tenant response = tenantServiceClient.getTenant(request);
      System.out.format("Name: %s%n", response.getName());
      System.out.format("External ID: %s%n", response.getExternalId());
    }
  }
}

Python

如需了解如何安装和使用 CTS 客户端库,请参阅 CTS 客户端库。 如需了解详情,请参阅 CTS Python API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


from google.cloud import talent


def get_tenant(project_id, tenant_id):
    """Get Tenant by name"""

    client = talent.TenantServiceClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID'

    if isinstance(project_id, bytes):
        project_id = project_id.decode("utf-8")
    if isinstance(tenant_id, bytes):
        tenant_id = tenant_id.decode("utf-8")
    name = client.tenant_path(project_id, tenant_id)

    response = client.get_tenant(name=name)
    print(f"Name: {response.name}")
    print(f"External ID: {response.external_id}")

列出租户

Java

如需了解如何安装和使用 CTS 客户端库,请参阅 CTS 客户端库。 如需了解详情,请参阅 CTS Java API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.cloud.talent.v4.ListTenantsRequest;
import com.google.cloud.talent.v4.ProjectName;
import com.google.cloud.talent.v4.Tenant;
import com.google.cloud.talent.v4.TenantServiceClient;
import java.io.IOException;

public class JobSearchListTenants {

  public static void listTenants() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    listTenants(projectId);
  }

  // List Tenants.
  public static void listTenants(String projectId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) {
      ProjectName parent = ProjectName.of(projectId);

      ListTenantsRequest request =
          ListTenantsRequest.newBuilder().setParent(parent.toString()).build();

      for (Tenant responseItem : tenantServiceClient.listTenants(request).iterateAll()) {
        System.out.format("Tenant Name: %s%n", responseItem.getName());
        System.out.format("External ID: %s%n", responseItem.getExternalId());
      }
    }
  }
}

Python

如需了解如何安装和使用 CTS 客户端库,请参阅 CTS 客户端库。 有关详情,请参阅 CTS Python API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


from google.cloud import talent


def list_tenants(project_id):
    """List Tenants"""

    client = talent.TenantServiceClient()

    # project_id = 'Your Google Cloud Project ID'

    if isinstance(project_id, bytes):
        project_id = project_id.decode("utf-8")
    parent = f"projects/{project_id}"

    # Iterate over all results
    for response_item in client.list_tenants(parent=parent):
        print(f"Tenant Name: {response_item.name}")
        print(f"External ID: {response_item.external_id}")

删除租户

Java

如需了解如何安装和使用适用于 CTS 的客户端库,请参阅 CTS 客户端库。 如需了解详情,请参阅 CTS Java API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.cloud.talent.v4.DeleteTenantRequest;
import com.google.cloud.talent.v4.TenantName;
import com.google.cloud.talent.v4.TenantServiceClient;
import java.io.IOException;

public class JobSearchDeleteTenant {

  public static void deleteTenant() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String tenantId = "your-tenant-id";
    deleteTenant(projectId, tenantId);
  }

  // Delete Tenant.
  public static void deleteTenant(String projectId, String tenantId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) {
      TenantName name = TenantName.of(projectId, tenantId);

      DeleteTenantRequest request =
          DeleteTenantRequest.newBuilder().setName(name.toString()).build();

      tenantServiceClient.deleteTenant(request);
      System.out.println("Deleted Tenant.");
    }
  }
}

Python

如需了解如何安装和使用 CTS 客户端库,请参阅 CTS 客户端库。 有关详情,请参阅 CTS Python API 参考文档

如需向 CTS 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


from google.cloud import talent


def delete_tenant(project_id, tenant_id):
    """Delete Tenant"""

    client = talent.TenantServiceClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID)'

    if isinstance(project_id, bytes):
        project_id = project_id.decode("utf-8")
    if isinstance(tenant_id, bytes):
        tenant_id = tenant_id.decode("utf-8")
    name = client.tenant_path(project_id, tenant_id)

    client.delete_tenant(name=name)
    print("Deleted Tenant.")