Skip to content

Commit 926ee14

Browse files
xinyunh0929dzlier-gcp
authored andcommitted
Check in Cloud Job Discovery client code samples (GoogleCloudPlatform#1112)
* cjd samples * cjd samples * modify README * modify README * revert gitignore * Add license header * Update the samples * Reorganize the samples and add more comments * Change the google could samples version from LATEST to 1.0.9 * Modify the tenantJobSearch * Update the share configuration * separate the sample tests
1 parent 84c6f3b commit 926ee14

16 files changed

+2438
-0
lines changed

jobs/cjd_sample/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Cloud Job Discovery client samples
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=jobs/cjd_sample/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
[Cloud Job Discovery][jobs] is part of Google for Jobs - a Google-wide commitment to help people find jobs more easily. Job Discovery provides plug and play access to Google’s search and machine learning capabilities, enabling the entire recruiting ecosystem - company career sites, job boards, applicant tracking systems, and staffing agencies to improve job site engagement and candidate conversion.
7+
8+
This sample Java application demonstrates how to access the Cloud Job Discovery API using the [Google Cloud Client Library for Java][google-cloud-java].
9+
10+
[jobs]: https://cloud.google.com/job-discovery/
11+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
12+
13+
## Setup
14+
15+
1. Install [Maven](http://maven.apache.org/).
16+
1. [Enable](https://console.cloud.google.com/apis/api/jobs.googleapis.com/overview) Cloud Job Discovery API.
17+
18+
## Build
19+
20+
Build your project with:
21+
```
22+
mvn clean package
23+
```
24+
25+
## Local testing
26+
27+
1. [Create a service account](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account)
28+
and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, for example:
29+
```
30+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json
31+
```
32+
2. Run all samples with command:
33+
```
34+
mvn -Dtest=SampleTests test
35+
```
36+
Or a single sample:
37+
```
38+
mvn exec:java -Dexec.mainClass="com.google.samples.BatchOperationSample"
39+
```

jobs/cjd_sample/pom.xml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.google.samples</groupId>
7+
<artifactId>cloud-job-discovery-samples</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<!--
12+
The parent pom defines common style checks and testing strategies for our samples.
13+
Removing or replacing it should not affect the execution of the samples in anyway.
14+
-->
15+
<parent>
16+
<groupId>com.google.cloud.samples</groupId>
17+
<artifactId>shared-configuration</artifactId>
18+
<version>1.0.10</version>
19+
</parent>
20+
21+
<properties>
22+
<maven.compiler.target>1.8</maven.compiler.target>
23+
<maven.compiler.source>1.8</maven.compiler.source>
24+
</properties>
25+
26+
<prerequisites>
27+
<maven>3.5</maven>
28+
</prerequisites>
29+
30+
<dependencies>
31+
<!-- [START dependencies] -->
32+
<dependency>
33+
<groupId>com.google.apis</groupId>
34+
<artifactId>google-api-services-jobs</artifactId>
35+
<version>LATEST</version>
36+
</dependency>
37+
<!-- [END dependencies] -->
38+
39+
<!-- Test dependencies -->
40+
<dependency>
41+
<groupId>com.google.truth</groupId>
42+
<artifactId>truth</artifactId>
43+
<version>0.40</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>RELEASE</version>
50+
</dependency>
51+
</dependencies>
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples;
18+
19+
import com.google.api.services.jobs.v2.JobService;
20+
import com.google.api.services.jobs.v2.JobService.V2.Complete;
21+
import com.google.api.services.jobs.v2.model.Company;
22+
import com.google.api.services.jobs.v2.model.CompleteQueryResponse;
23+
import com.google.api.services.jobs.v2.model.CompletionResult;
24+
import com.google.api.services.jobs.v2.model.CreateJobRequest;
25+
import com.google.api.services.jobs.v2.model.HistogramFacets;
26+
import com.google.api.services.jobs.v2.model.Job;
27+
import com.google.api.services.jobs.v2.model.JobQuery;
28+
import com.google.api.services.jobs.v2.model.MatchingJob;
29+
import com.google.api.services.jobs.v2.model.RequestMetadata;
30+
import com.google.api.services.jobs.v2.model.SearchJobsRequest;
31+
import com.google.api.services.jobs.v2.model.SearchJobsResponse;
32+
import java.io.IOException;
33+
import java.util.ArrayList;
34+
import java.util.Arrays;
35+
import java.util.List;
36+
37+
/**
38+
* The samples in this file introduced how to do the auto complete, including:
39+
*
40+
* - Default auto complete (on both company display name and job title)
41+
*
42+
* - Auto complete on job title only
43+
*/
44+
public final class AutoCompleteSample {
45+
46+
private static JobService jobService = JobServiceUtils.getJobService();
47+
48+
//[START auto_complete_job_title]
49+
50+
/**
51+
* Auto completes job titles within given companyName.
52+
*/
53+
public static void jobTitleAutoComplete(String companyName, String query)
54+
throws IOException {
55+
Complete complete = jobService
56+
.v2()
57+
.complete()
58+
.setQuery(query)
59+
.setLanguageCode("en-US")
60+
.setType("JOB_TITLE")
61+
.setPageSize(10);
62+
if (companyName != null) {
63+
complete.setCompanyName(companyName);
64+
}
65+
66+
CompleteQueryResponse results = complete.execute();
67+
68+
System.out.println(results);
69+
}
70+
// [END auto_complete_default]
71+
72+
/**
73+
* Auto completes job titles within given companyName.
74+
*/
75+
public static void defaultAutoComplete(String companyName, String query)
76+
throws IOException {
77+
Complete complete = jobService
78+
.v2()
79+
.complete()
80+
.setQuery(query)
81+
.setLanguageCode("en-US")
82+
.setPageSize(10);
83+
if (companyName != null) {
84+
complete.setCompanyName(companyName);
85+
}
86+
87+
CompleteQueryResponse results = complete.execute();
88+
89+
System.out.println(results);
90+
}
91+
// [END auto_complete_default]
92+
93+
public static void main(String... args) throws Exception {
94+
Company companyToBeCreated = BasicCompanySample.generateCompany().setDisplayName("Google");
95+
String companyName = BasicCompanySample.createCompany(companyToBeCreated).getName();
96+
97+
Job jobToBeCreated = BasicJobSample.generateJobWithRequiredFields(companyName)
98+
.setJobTitle("Software engineer");
99+
final String jobName = BasicJobSample.createJob(jobToBeCreated).getName();
100+
101+
// Wait several seconds for post processing
102+
Thread.sleep(10000);
103+
defaultAutoComplete(companyName,"goo");
104+
defaultAutoComplete(companyName,"sof");
105+
jobTitleAutoComplete(companyName, "sof");
106+
107+
BasicJobSample.deleteJob(jobName);
108+
BasicCompanySample.deleteCompany(companyName);
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples;
18+
19+
import com.google.api.services.jobs.v2.JobService;
20+
import com.google.api.services.jobs.v2.model.Company;
21+
import java.io.IOException;
22+
import java.util.Random;
23+
24+
/**
25+
* This file contains the basic knowledge about company and job, including:
26+
*
27+
* - Construct a company with required fields
28+
*
29+
* - Create a company
30+
*
31+
* - Get a company
32+
*
33+
* - Update a company
34+
*
35+
* - Update a company with field mask
36+
*
37+
* - Delete a company
38+
*/
39+
public final class BasicCompanySample {
40+
41+
private static JobService jobService = JobServiceUtils.getJobService();
42+
43+
// [START basic_company]
44+
45+
/**
46+
* Generate a company
47+
*/
48+
public static Company generateCompany() {
49+
// distributor company id should be a unique Id in your system.
50+
String distributorCompanyId =
51+
"company:" + String.valueOf(new Random().nextLong());
52+
53+
Company company =
54+
new Company()
55+
.setDisplayName("Google")
56+
.setHqLocation("1600 Amphitheatre Parkway Mountain View, CA 94043")
57+
.setDistributorCompanyId(distributorCompanyId);
58+
System.out.println("Company generated: " + company);
59+
return company;
60+
}
61+
// [END basic_company]
62+
63+
// [START create_company]
64+
65+
/**
66+
* Create a company.
67+
*/
68+
public static Company createCompany(Company companyToBeCreated) throws IOException {
69+
try {
70+
Company companyCreated = jobService.companies().create(companyToBeCreated)
71+
.execute();
72+
System.out.println("Company created: " + companyCreated);
73+
return companyCreated;
74+
} catch (IOException e) {
75+
System.out.println("Got exception while creating company");
76+
throw e;
77+
}
78+
}
79+
// [END create_company]
80+
81+
// [START get_company]
82+
83+
/**
84+
* Get a company.
85+
*/
86+
public static Company getCompany(String companyName) throws IOException {
87+
try {
88+
Company companyExisted = jobService.companies().get(companyName).execute();
89+
System.out.println("Company existed: " + companyExisted);
90+
return companyExisted;
91+
} catch (IOException e) {
92+
System.out.println("Got exception while getting company");
93+
throw e;
94+
}
95+
}
96+
// [END get_company]
97+
98+
// [START update_company]
99+
100+
/**
101+
* Updates a company.
102+
*/
103+
public static Company updateCompany(String companyName, Company companyToBeUpdated)
104+
throws IOException {
105+
try {
106+
Company companyUpdated = jobService.companies()
107+
.patch(companyName, companyToBeUpdated)
108+
.execute();
109+
System.out.println("Company updated: " + companyUpdated);
110+
return companyUpdated;
111+
} catch (IOException e) {
112+
System.out.println("Got exception while updating company");
113+
throw e;
114+
}
115+
}
116+
// [END update_company]
117+
118+
// [START update_company_with_field_mask]
119+
120+
/**
121+
* Updates a company.
122+
*/
123+
public static Company updateCompanyWithFieldMask(String companyName, String fieldMask,
124+
Company companyToBeUpdated)
125+
throws IOException {
126+
try {
127+
// String foo = String.format("?updateCompanyFields=%s",fieldMask);
128+
Company companyUpdated = jobService.companies()
129+
.patch(companyName, companyToBeUpdated)
130+
.setUpdateCompanyFields(fieldMask)
131+
.execute();
132+
System.out.println("Company updated: " + companyUpdated);
133+
return companyUpdated;
134+
} catch (IOException e) {
135+
System.out.println("Got exception while updating company");
136+
throw e;
137+
}
138+
}
139+
// [END update_company_with_field_mask]
140+
141+
// [START delete_company]
142+
143+
/**
144+
* Delete a company.
145+
*/
146+
public static void deleteCompany(String companyName) throws IOException {
147+
try {
148+
149+
jobService.companies().delete(companyName).execute();
150+
System.out.println("Company deleted");
151+
} catch (IOException e) {
152+
System.out.println("Got exception while deleting company");
153+
throw e;
154+
}
155+
}
156+
// [END delete_company]
157+
158+
public static void main(String... args) throws Exception {
159+
// Construct a company
160+
Company companyToBeCreated = generateCompany();
161+
162+
// Create a company
163+
Company companyCreated = createCompany(companyToBeCreated);
164+
165+
// Get a company
166+
String companyName = companyCreated.getName();
167+
getCompany(companyName);
168+
169+
// Update a company
170+
Company companyToBeUpdated = companyCreated
171+
.setWebsite("https://elgoog.im/");
172+
updateCompany(companyName, companyToBeUpdated);
173+
174+
// Update a company with field mask
175+
updateCompanyWithFieldMask(companyName, "displayName",
176+
new Company().setDisplayName("changedTitle")
177+
.setDistributorCompanyId(companyCreated.getDistributorCompanyId()));
178+
179+
// Delete a company
180+
deleteCompany(companyName);
181+
}
182+
}

0 commit comments

Comments
 (0)