使用客户端获取公司。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
Go
import (
"context"
"fmt"
"io"
talent "cloud.google.com/go/talent/apiv4beta1"
talentpb "google.golang.org/genproto/googleapis/cloud/talent/v4beta1"
)
// getCompany gets an existing company by its resource name.
func getCompany(w io.Writer, projectID, companyID string) (*talentpb.Company, error) {
ctx := context.Background()
// Initialize a companyService client.
c, err := talent.NewCompanyClient(ctx)
if err != nil {
return nil, fmt.Errorf("talent.NewCompanyClient: %v", err)
}
defer c.Close()
// Construct a getCompany request.
companyName := fmt.Sprintf("projects/%s/companies/%s", projectID, companyID)
req := &talentpb.GetCompanyRequest{
// The resource name of the company to be retrieved.
// The format is "projects/{project_id}/companies/{company_id}".
Name: companyName,
}
resp, err := c.GetCompany(ctx, req)
if err != nil {
return nil, fmt.Errorf("GetCompany: %v", err)
}
fmt.Fprintf(w, "Company: %q\n", resp.GetName())
fmt.Fprintf(w, "Company display name: %q\n", resp.GetDisplayName())
return resp, nil
}
Java
/*
* Please include the following imports to run this sample.
*
* import com.google.cloud.talent.v4beta1.Company;
* import com.google.cloud.talent.v4beta1.CompanyName;
* import com.google.cloud.talent.v4beta1.CompanyServiceClient;
* import com.google.cloud.talent.v4beta1.CompanyWithTenantName;
* import com.google.cloud.talent.v4beta1.GetCompanyRequest;
*/
public static void sampleGetCompany() {
// 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)";
String companyId = "Company ID";
sampleGetCompany(projectId, tenantId, companyId);
}
/** Get Company */
public static void sampleGetCompany(String projectId, String tenantId, String companyId) {
try (CompanyServiceClient companyServiceClient = CompanyServiceClient.create()) {
CompanyName name = CompanyWithTenantName.of(projectId, tenantId, companyId);
GetCompanyRequest request = GetCompanyRequest.newBuilder().setName(name.toString()).build();
Company response = companyServiceClient.getCompany(request);
System.out.printf("Company name: %s\n", response.getName());
System.out.printf("Display name: %s\n", response.getDisplayName());
} catch (Exception exception) {
System.err.println("Failed to create the client due to: " + exception);
}
}
Node.js
const talent = require('@google-cloud/talent').v4;
/** Get Company */
function sampleGetCompany(projectId, tenantId, companyId) {
const client = new talent.CompanyServiceClient();
// const projectId = 'Your Google Cloud Project ID';
// const tenantId = 'Your Tenant ID (using tenancy is optional)';
// const companyId = 'Company ID';
const formattedName = client.companyPath(projectId, tenantId, companyId);
client
.getCompany({name: formattedName})
.then(responses => {
const response = responses[0];
console.log(`Company name: ${response.name}`);
console.log(`Display name: ${response.displayName}`);
})
.catch(err => {
console.error(err);
});
}
Python
from google.cloud import talent
import six
def get_company(project_id, tenant_id, company_id):
"""Get Company"""
client = talent.CompanyServiceClient()
# project_id = 'Your Google Cloud Project ID'
# tenant_id = 'Your Tenant ID (using tenancy is optional)'
# company_id = 'Company ID'
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")
if isinstance(company_id, six.binary_type):
company_id = company_id.decode("utf-8")
name = client.company_path(project_id, tenant_id, company_id)
response = client.get_company(name=name)
print(f"Company name: {response.name}")
print(f"Display name: {response.display_name}")
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。