|
| 1 | +/* |
| 2 | + * Copyright 2019 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 snippets.healthcare.datasets; |
| 18 | + |
| 19 | +// [START healthcare_deidentify_dataset] |
| 20 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 21 | +import com.google.api.client.http.HttpHeaders; |
| 22 | +import com.google.api.client.http.HttpRequestInitializer; |
| 23 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 24 | +import com.google.api.client.json.JsonFactory; |
| 25 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 26 | +import com.google.api.services.healthcare.v1beta1.CloudHealthcare; |
| 27 | +import com.google.api.services.healthcare.v1beta1.CloudHealthcare.Projects.Locations.Datasets; |
| 28 | +import com.google.api.services.healthcare.v1beta1.CloudHealthcareScopes; |
| 29 | +import com.google.api.services.healthcare.v1beta1.model.DeidentifyConfig; |
| 30 | +import com.google.api.services.healthcare.v1beta1.model.DeidentifyDatasetRequest; |
| 31 | +import com.google.api.services.healthcare.v1beta1.model.DicomConfig; |
| 32 | +import com.google.api.services.healthcare.v1beta1.model.Operation; |
| 33 | +import com.google.api.services.healthcare.v1beta1.model.TagFilterList; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Collections; |
| 37 | + |
| 38 | +public class DatasetDeIdentify { |
| 39 | + private static final String DATASET_NAME = "projects/%s/locations/%s/datasets/%s"; |
| 40 | + private static final JsonFactory JSON_FACTORY = new JacksonFactory(); |
| 41 | + private static final NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport(); |
| 42 | + |
| 43 | + public static void datasetDeIdentify(String srcDatasetName, String destDatasetName) |
| 44 | + throws IOException { |
| 45 | + // String srcDatasetName = |
| 46 | + // String.format(DATASET_NAME, "your-project-id", "your-region-id", "your-src-dataset-id"); |
| 47 | + // String destDatasetName = |
| 48 | + // String.format(DATASET_NAME, "your-project-id", "your-region-id", "your-dest-dataset-id"); |
| 49 | + |
| 50 | + // Initialize the client, which will be used to interact with the service. |
| 51 | + CloudHealthcare client = createClient(); |
| 52 | + |
| 53 | + // Configure what information needs to be De-Identified. |
| 54 | + // For more information on de-identifying using tags, please see the following: |
| 55 | + // https://cloud.google.com/healthcare/docs/how-tos/dicom-deidentify#de-identification_using_tags |
| 56 | + TagFilterList tags = new TagFilterList().setTags(Arrays.asList("PatientID")); |
| 57 | + DicomConfig dicomConfig = new DicomConfig().setKeepList(tags); |
| 58 | + DeidentifyConfig config = new DeidentifyConfig().setDicom(dicomConfig); |
| 59 | + |
| 60 | + // Create the de-identify request and configure any parameters. |
| 61 | + DeidentifyDatasetRequest deidentifyRequest = |
| 62 | + new DeidentifyDatasetRequest().setDestinationDataset(destDatasetName).setConfig(config); |
| 63 | + Datasets.Deidentify request = |
| 64 | + client.projects().locations().datasets().deidentify(srcDatasetName, deidentifyRequest); |
| 65 | + |
| 66 | + // Execute the request, wait for the operation to complete, and process the results. |
| 67 | + try { |
| 68 | + Operation operation = request.execute(); |
| 69 | + while (operation.getDone() == null || !operation.getDone()) { |
| 70 | + // Update the status of the operation with another request. |
| 71 | + Thread.sleep(500); // Pause for 500ms between requests. |
| 72 | + operation = |
| 73 | + client |
| 74 | + .projects() |
| 75 | + .locations() |
| 76 | + .datasets() |
| 77 | + .operations() |
| 78 | + .get(operation.getName()) |
| 79 | + .execute(); |
| 80 | + } |
| 81 | + System.out.println( |
| 82 | + "De-identified Dataset created. Response content: " + operation.getResponse()); |
| 83 | + } catch (Exception ex) { |
| 84 | + System.out.printf("Error during request execution: %s", ex.toString()); |
| 85 | + ex.printStackTrace(System.out); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static CloudHealthcare createClient() throws IOException { |
| 90 | + // Use Application Default Credentials (ADC) to authenticate the requests |
| 91 | + // For more information see https://cloud.google.com/docs/authentication/production |
| 92 | + GoogleCredential credential = |
| 93 | + GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) |
| 94 | + .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); |
| 95 | + |
| 96 | + // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. |
| 97 | + HttpRequestInitializer requestInitializer = |
| 98 | + request -> { |
| 99 | + credential.initialize(request); |
| 100 | + request.setHeaders(new HttpHeaders().set("X-GFE-SSL", "yes")); |
| 101 | + request.setConnectTimeout(60000); // 1 minute connect timeout |
| 102 | + request.setReadTimeout(60000); // 1 minute read timeout |
| 103 | + }; |
| 104 | + |
| 105 | + // Build the client for interacting with the service. |
| 106 | + return new CloudHealthcare.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer) |
| 107 | + .setApplicationName("your-application-name") |
| 108 | + .build(); |
| 109 | + } |
| 110 | +} |
| 111 | +// [END healthcare_deidentify_dataset] |
0 commit comments