0

对于自定义属性,Google 并未在其示例代码中提供使用示例。

谷歌文档在其 Python 示例中缺少代码

Google 创建作业的示例并注释“使用自定义属性创建作业”,但实际上并未包含自定义属性的任何代码:

def sample_create_job(project_id, tenant_id, company_name, requisition_id,
                      language_code):
    """Create Job with Custom Attributes"""

    client = talent_v4beta1.JobServiceClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID (using tenancy is optional)'
    # company_name = 'Company name, e.g. projects/your-project/companies/company-id'
    # requisition_id = 'Job requisition ID, aka Posting ID. Unique per job.'
    # language_code = 'en-US'

    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_name, six.binary_type):
        company_name = company_name.decode('utf-8')
    if isinstance(requisition_id, six.binary_type):
        requisition_id = requisition_id.decode('utf-8')
    if isinstance(language_code, six.binary_type):
        language_code = language_code.decode('utf-8')
    parent = client.tenant_path(project_id, tenant_id)
    job = {
        'company': company_name,
        'requisition_id': requisition_id,
        'language_code': language_code
    }

    response = client.create_job(parent, job)
    print('Created job: {}'.format(response.name))

如何为作业定义自定义属性?

以下内容适用于早期版本的 Talent Solution:

job['custom_attributes'] = {
    'custom_name' : {'stringValues' : ['s0', 's1', 's2']},
    ...
}

我现在已经尝试过了:

from google.cloud.talent_v4beta1.types import CustomAttribute
job['custom_attributes'] = [
    {
        'key' : 'keyname',
        'value': CustomAttribute(string_values=[valuestring], filterable=True)
    }
]

但是当我尝试创建或更新作业时,会引发异常:TypeError: {'key': 'keyname', 'value': string_values: "valuestring" filterable: true } has type dict, but expected one of: bytes, unicode

4

2 回答 2

1

2020 年 11 月,Google 文档官方说明了如何做到这一点


from google.cloud import talent
import six


def create_job(project_id, tenant_id, company_id, requisition_id):
    """Create Job with Custom Attributes"""

    client = talent.JobServiceClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID (using tenancy is optional)'
    # company_id = 'Company name, e.g. projects/your-project/companies/company-id'
    # requisition_id = 'Job requisition ID, aka Posting ID. Unique per job.'
    # language_code = 'en-US'

    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")

    # Custom attribute can be string or numeric value,
    # and can be filtered in search queries.
    # https://cloud.google.com/talent-solution/job-search/docs/custom-attributes
    custom_attribute = talent.CustomAttribute()
    custom_attribute.filterable = True
    custom_attribute.string_values.append("Intern")
    custom_attribute.string_values.append("Apprenticeship")

    parent = f"projects/{project_id}/tenants/{tenant_id}"

    job = talent.Job(
        company=company_id,
        title="Software Engineer",
        requisition_id=requisition_id,
        description="This is a description of this job",
        language_code="en-us",
        custom_attributes={"FOR_STUDENTS": custom_attribute}
    )

    response = client.create_job(parent=parent, job=job)
    print(f"Created job: {response.name}")
    return response.name

我还有一个WIP python 库来帮助它基于 pydantic 对象。

    j = Job(
            company=company.name,
            requisition_id=uuid4().hex,
            title="engineer",
            description="implement system",
            custom_attributes={
                "tags": CustomAttributes(
                    string_values=["hello"],
                    filterable=True,
                    keyword_searchable=True
                )
            }
    )
    j.create(tenant=tenant)

于 2020-11-05T11:32:02.503 回答
0
from google.cloud.talent_v4beta1.types import CustomAttribute

job = {
    'title' : ...
}

job['custom_attributes'] = {
    'keyname0' : CustomAttributes(
        string_values=['eg0', 'eg1'],
        filterable=False),
    'keyname1' : ...
}
于 2019-06-24T15:47:05.230 回答