使用客户端列出公司。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
Go
import (
"context"
"fmt"
"io"
talent "cloud.google.com/go/talent/apiv4beta1"
"google.golang.org/api/iterator"
talentpb "google.golang.org/genproto/googleapis/cloud/talent/v4beta1"
)
// listCompanies lists all companies in the project.
func listCompanies(w io.Writer, projectID string) error {
ctx := context.Background()
// Initialize a compnayService client.
c, err := talent.NewCompanyClient(ctx)
if err != nil {
return fmt.Errorf("talent.NewCompanyClient: %v", err)
}
defer c.Close()
// Construct a listCompanies request.
req := &talentpb.ListCompaniesRequest{
Parent: "projects/" + projectID,
}
it := c.ListCompanies(ctx, req)
for {
resp, err := it.Next()
if err == iterator.Done {
return nil
}
if err != nil {
return fmt.Errorf("it.Next: %v", err)
}
fmt.Fprintf(w, "Listing company: %q\n", resp.GetName())
fmt.Fprintf(w, "Display name: %v\n", resp.GetDisplayName())
fmt.Fprintf(w, "External ID: %v\n", resp.GetExternalId())
}
}
Java
/*
* Please include the following imports to run this sample.
*
* import com.google.cloud.talent.v4beta1.Company;
* import com.google.cloud.talent.v4beta1.CompanyServiceClient;
* import com.google.cloud.talent.v4beta1.ListCompaniesRequest;
* import com.google.cloud.talent.v4beta1.TenantName;
* import com.google.cloud.talent.v4beta1.TenantOrProjectName;
*/
public static void sampleListCompanies() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "Your Google Cloud Project ID";
String tenantId = "Your Tenant ID (using tenancy is optional)";
sampleListCompanies(projectId, tenantId);
}
/**
* List Companies
*
* @param projectId Your Google Cloud Project ID
* @param tenantId Identifier of the Tenant
*/
public static void sampleListCompanies(String projectId, String tenantId) {
try (CompanyServiceClient companyServiceClient = CompanyServiceClient.create()) {
TenantOrProjectName parent = TenantName.of(projectId, tenantId);
ListCompaniesRequest request =
ListCompaniesRequest.newBuilder().setParent(parent.toString()).build();
for (Company responseItem : companyServiceClient.listCompanies(request).iterateAll()) {
System.out.printf("Company Name: %s\n", responseItem.getName());
System.out.printf("Display Name: %s\n", responseItem.getDisplayName());
System.out.printf("External ID: %s\n", responseItem.getExternalId());
}
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
Node.js
const talent = require('@google-cloud/talent').v4;
/**
* List Companies
*
* @param projectId {string} Your Google Cloud Project ID
* @param tenantId {string} Identifier of the Tenant
*/
function sampleListCompanies(projectId, tenantId) {
const client = new talent.CompanyServiceClient();
// Iterate over all elements.
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
const formattedParent = client.tenantPath(projectId, tenantId);
client
.listCompanies({parent: formattedParent})
.then(responses => {
const resources = responses[0];
for (const resource of resources) {
console.log(`Company Name: ${resource.name}`);
console.log(`Display Name: ${resource.displayName}`);
console.log(`External ID: ${resource.externalId}`);
}
})
.catch(err => {
console.error(err);
});
}
Python
from google.cloud import talent
import six
def list_companies(project_id, tenant_id):
"""List Companies"""
client = talent.CompanyServiceClient()
# project_id = 'Your Google Cloud Project ID'
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
if isinstance(project_id, six.binary_type):
project_id = project_id.decode("utf-8")
if isinstance(tenant_id, six.binary_type):
tenant_id = tenant_id.decode("utf-8")
parent = f"projects/{project_id}/tenants/{tenant_id}"
# Iterate over all results
results = []
for company in client.list_companies(parent=parent):
results.append(company.name)
print(f"Company Name: {company.name}")
print(f"Display Name: {company.display_name}")
print(f"External ID: {company.external_id}")
return results
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。