租户基础知识

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

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

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

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

默认租户

Job Search v4beta1 和更新的版本都需要一个租户实体。所有项目都被分配有一个默认租户的 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


import com.google.cloud.talent.v4beta1.CreateTenantRequest;
import com.google.cloud.talent.v4beta1.ProjectName;
import com.google.cloud.talent.v4beta1.Tenant;
import com.google.cloud.talent.v4beta1.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 {
    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.printf("Name: %s\n", response.getName());
      System.out.printf("External ID: %s\n", response.getExternalId());
    }
  }
}

PHP

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Talent\V4beta1\TenantServiceClient;
use Google\Cloud\Talent\V4beta1\Tenant;

/** Create Tenant for scoping resources, e.g. companies and jobs */
function sampleCreateTenant($projectId, $externalId)
{

    $tenantServiceClient = new TenantServiceClient();

    // $projectId = 'Your Google Cloud Project ID';
    // $externalId = 'Your Unique Identifier for Tenant';
    $formattedParent = $tenantServiceClient->projectName($projectId);
    $tenant = new Tenant();
    $tenant->setExternalId($externalId);

    try {
        $response = $tenantServiceClient->createTenant($formattedParent, $tenant);
        printf('Created Tenant'.PHP_EOL);
        printf('Name: %s'.PHP_EOL, $response->getName());
        printf('External ID: %s'.PHP_EOL, $response->getExternalId());
    } finally {
        $tenantServiceClient->close();
    }

}

Ruby

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


# Create Tenant for scoping resources, e.g. companies and jobs
def sample_create_tenant project_id, external_id
  # Instantiate a client
  tenant_client = Google::Cloud::Talent::TenantService.new version: :v4beta1

  # project_id = "Your Google Cloud Project ID"
  # external_id = "Your Unique Identifier for Tenant"
  formatted_parent = tenant_client.class.project_path(project_id)
  tenant = { external_id: external_id }

  response = tenant_client.create_tenant(formatted_parent, tenant)
  puts "Created Tenant"
  puts "Name: #{response.name}"
  puts "External ID: #{response.external_id}"
end

C#

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

public static object CreateTenant(string projectId, string externalId)
{
    TenantServiceClient tenantServiceClient = TenantServiceClient.Create();
    Tenant tenant = new Tenant
    {
        ExternalId = externalId,
    };
    ProjectName parent = new ProjectName(projectId);
    CreateTenantRequest request = new CreateTenantRequest
    {
        ParentAsProjectName = parent,
        Tenant = tenant
    };
    Tenant response = tenantServiceClient.CreateTenant(request);

    Console.WriteLine($"Created Tenant.");
    Console.WriteLine($"Name: {response.Name}");
    Console.WriteLine($"External ID: {response.ExternalId}");
    return 0;
}

检索租户

Java

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


import com.google.cloud.talent.v4beta1.GetTenantRequest;
import com.google.cloud.talent.v4beta1.Tenant;
import com.google.cloud.talent.v4beta1.TenantName;
import com.google.cloud.talent.v4beta1.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 {
    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.printf("Name: %s\n", response.getName());
      System.out.printf("External ID: %s\n", response.getExternalId());
    }
  }
}

PHP

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Talent\V4beta1\TenantServiceClient;

/** Get Tenant by name */
function sampleGetTenant($projectId, $tenantId)
{

    $tenantServiceClient = new TenantServiceClient();

    // $projectId = 'Your Google Cloud Project ID';
    // $tenantId = 'Your Tenant ID';
    $formattedName = $tenantServiceClient->tenantName($projectId, $tenantId);

    try {
        $response = $tenantServiceClient->getTenant($formattedName);
        printf('Name: %s'.PHP_EOL, $response->getName());
        printf('External ID: %s'.PHP_EOL, $response->getExternalId());
    } finally {
        $tenantServiceClient->close();
    }

}

Ruby

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


# Get Tenant by name
def sample_get_tenant project_id, tenant_id
  # Instantiate a client
  tenant_client = Google::Cloud::Talent::TenantService.new version: :v4beta1

  # project_id = "Your Google Cloud Project ID"
  # tenant_id = "Your Tenant ID"
  formatted_name = tenant_client.class.tenant_path(project_id, tenant_id)

  response = tenant_client.get_tenant(formatted_name)
  puts "Name: #{response.name}"
  puts "External ID: #{response.external_id}"
end

C#

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

public static object GetTenant(string projectId, string tenantId)
{
    TenantServiceClient tenantServiceClient = TenantServiceClient.Create();
    TenantName name = TenantName.FromProjectTenant(projectId, tenantId);
    GetTenantRequest request = new GetTenantRequest
    {
        TenantName = name
    };
    Tenant response = tenantServiceClient.GetTenant(request);
    Console.WriteLine($"Name: {response.Name}");
    Console.WriteLine($"External ID: {response.ExternalId}");
    return 0;
}

列出租户

Java

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


import com.google.cloud.talent.v4beta1.ListTenantsRequest;
import com.google.cloud.talent.v4beta1.ProjectName;
import com.google.cloud.talent.v4beta1.Tenant;
import com.google.cloud.talent.v4beta1.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 {
    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.printf("Tenant Name: %s\n", responseItem.getName());
        System.out.printf("External ID: %s\n", responseItem.getExternalId());
      }
    }
  }
}

PHP

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Talent\V4beta1\TenantServiceClient;

/** List Tenants */
function sampleListTenants($projectId)
{

    $tenantServiceClient = new TenantServiceClient();

    // $projectId = 'Your Google Cloud Project ID';
    $formattedParent = $tenantServiceClient->projectName($projectId);

    try {
        // Iterate through all elements
        $pagedResponse = $tenantServiceClient->listTenants($formattedParent);
        foreach ($pagedResponse->iterateAllElements() as $responseItem) {
            printf('Tenant Name: %s'.PHP_EOL, $responseItem->getName());
            printf('External ID: %s'.PHP_EOL, $responseItem->getExternalId());
        }
    } finally {
        $tenantServiceClient->close();
    }

}

Ruby

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


# List Tenants
def sample_list_tenants project_id
  # Instantiate a client
  tenant_client = Google::Cloud::Talent::TenantService.new version: :v4beta1

  # project_id = "Your Google Cloud Project ID"
  formatted_parent = tenant_client.class.project_path(project_id)

  # Iterate over all results.
  tenant_client.list_tenants(formatted_parent).each do |element|
    puts "Tenant Name: #{element.name}"
    puts "External ID: #{element.external_id}"
  end
end

C#

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

public static object ListTenants(string projectId)
{
    TenantServiceClient tenantServiceClient = TenantServiceClient.Create();
    ProjectName parent = new ProjectName(projectId);
    ListTenantsRequest request = new ListTenantsRequest
    {
        ParentAsProjectName = parent
    };
    var tenants = tenantServiceClient.ListTenants(request);
    foreach (var tenant in tenants)
    {
        Console.WriteLine($"Tenant Name: {tenant.Name}");
        Console.WriteLine($"External ID: {tenant.ExternalId}");
    }
    return 0;
}

删除租户

Java

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


import com.google.cloud.talent.v4beta1.DeleteTenantRequest;
import com.google.cloud.talent.v4beta1.TenantName;
import com.google.cloud.talent.v4beta1.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 {
    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.");
    }
  }
}

PHP

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Talent\V4beta1\TenantServiceClient;

/** Delete Tenant */
function sampleDeleteTenant($projectId, $tenantId)
{

    $tenantServiceClient = new TenantServiceClient();

    // $projectId = 'Your Google Cloud Project ID';
    // $tenantId = 'Your Tenant ID)';
    $formattedName = $tenantServiceClient->tenantName($projectId, $tenantId);

    try {
        $tenantServiceClient->deleteTenant($formattedName);
        printf('Deleted Tenant.'.PHP_EOL);
    } finally {
        $tenantServiceClient->close();
    }

}

Ruby

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库


# Delete Tenant
def sample_delete_tenant project_id, tenant_id
  # Instantiate a client
  tenant_client = Google::Cloud::Talent::TenantService.new version: :v4beta1

  # project_id = "Your Google Cloud Project ID"
  # tenant_id = "Your Tenant ID)"
  formatted_name = tenant_client.class.tenant_path(project_id, tenant_id)

  tenant_client.delete_tenant(formatted_name)

  puts "Deleted Tenant."
end

C#

如需详细了解如何安装和创建 Cloud Talent Solution 客户端,请参阅 Cloud Talent Solution 客户端库

public static object DeleteTenant(string projectId, string tenantId)
{
    TenantServiceClient tenantServiceClient = TenantServiceClient.Create();
    TenantName tenantName = TenantName.FromProjectTenant(projectId, tenantId);
    DeleteTenantRequest request = new DeleteTenantRequest
    {
        TenantName = tenantName
    };
    tenantServiceClient.DeleteTenant(request);
    Console.WriteLine($"Deleted Tenant.");
    return 0;
}