Search in sources :

Example 6 with LocationName

use of com.google.privacy.dlp.v2.LocationName in project google-cloud-java by GoogleCloudPlatform.

the class TranslateSnippetsBeta method listGlossary.

// [END translate_create_glossary_beta]
/**
 * Lists all the glossaries for a given project.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param filter - criteria for listing glossaries.
 */
// [START translate_list_glossary_beta]
static ListGlossariesPagedResponse listGlossary(String projectId, String location, String filter) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        ListGlossariesPagedResponse response = translationServiceClient.listGlossaries(locationName.toString(), filter);
        // Call the API
        for (Glossary element : response.iterateAll()) {
            System.out.format("Name: %s\n", element.getName());
            System.out.format("Language Codes Set:\n");
            System.out.format("Source Language Code: %s\n", element.getLanguageCodesSet().getLanguageCodesList().get(0));
            System.out.format("Target Language Code: %s\n", element.getLanguageCodesSet().getLanguageCodesList().get(1));
            System.out.format("Input Uri: %s\n", element.getInputConfig().getGcsSource());
        }
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) Glossary(com.google.cloud.translate.v3beta1.Glossary) ListGlossariesPagedResponse(com.google.cloud.translate.v3beta1.TranslationServiceClient.ListGlossariesPagedResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 7 with LocationName

use of com.google.privacy.dlp.v2.LocationName in project java-docs-samples by GoogleCloudPlatform.

the class CreateQueue method createQueue.

public static void createQueue(String projectId, String locationId, String queueBlueName, String queueRedName) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String queueBlueName = "queue-blue";
        // String queueRedName = "queue-red";
        LocationName parent = LocationName.of(projectId, locationId);
        Queue queueBlue = Queue.newBuilder().setName(QueueName.of(projectId, locationId, queueBlueName).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(5.0)).setAppEngineRoutingOverride(AppEngineRouting.newBuilder().setVersion("v2").setService("task-module")).build();
        Queue queueRed = Queue.newBuilder().setName(QueueName.of(projectId, locationId, queueRedName).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0)).build();
        Queue[] queues = new Queue[] { queueBlue, queueRed };
        for (Queue queue : queues) {
            Queue response = client.createQueue(parent, queue);
            System.out.println(response);
        }
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Example 8 with LocationName

use of com.google.privacy.dlp.v2.LocationName in project java-docs-samples by GoogleCloudPlatform.

the class RetryTask method retryTask.

public static void retryTask(String projectId, String locationId, String fooqueue, String barqueue, String bazqueue) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String fooqueue = "fooqueue";
        // String barqueue = "barqueue";
        // String bazqueue = "bazqueue";
        LocationName parent = LocationName.of(projectId, locationId);
        Duration retryDuration = Duration.newBuilder().setSeconds(2 * 60 * 60 * 24).build();
        Duration min = Duration.newBuilder().setSeconds(10).build();
        Duration max1 = Duration.newBuilder().setSeconds(200).build();
        Duration max2 = Duration.newBuilder().setSeconds(300).build();
        Queue foo = Queue.newBuilder().setName(QueueName.of(projectId, locationId, fooqueue).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0)).setRetryConfig(RetryConfig.newBuilder().setMaxAttempts(7).setMaxRetryDuration(retryDuration)).build();
        Queue bar = Queue.newBuilder().setName(QueueName.of(projectId, locationId, barqueue).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0)).setRetryConfig(RetryConfig.newBuilder().setMinBackoff(min).setMaxBackoff(max1).setMaxDoublings(0)).build();
        Queue baz = Queue.newBuilder().setName(QueueName.of(projectId, locationId, bazqueue).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0)).setRetryConfig(RetryConfig.newBuilder().setMinBackoff(min).setMaxBackoff(max2).setMaxDoublings(3)).build();
        Queue[] queues = new Queue[] { foo, bar, baz };
        for (Queue queue : queues) {
            Queue response = client.createQueue(parent, queue);
            System.out.println(response);
        }
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) Duration(com.google.protobuf.Duration) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Example 9 with LocationName

use of com.google.privacy.dlp.v2.LocationName in project java-docs-samples by GoogleCloudPlatform.

the class UpdateQueue method updateQueue.

public static void updateQueue(String projectId, String locationId, String queueId) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String queueId = "queue-blue";
        LocationName parent = LocationName.of(projectId, locationId);
        Queue queueBlue = Queue.newBuilder().setName(QueueName.of(projectId, locationId, queueId).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(20.0).setMaxConcurrentDispatches(10)).build();
        UpdateQueueRequest request = UpdateQueueRequest.newBuilder().setQueue(queueBlue).build();
        Queue response = client.updateQueue(request);
        System.out.println(response);
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) Queue(com.google.cloud.tasks.v2.Queue) UpdateQueueRequest(com.google.cloud.tasks.v2.UpdateQueueRequest) LocationName(com.google.cloud.tasks.v2.LocationName)

Example 10 with LocationName

use of com.google.privacy.dlp.v2.LocationName in project java-translate by googleapis.

the class BatchTranslateTextWithGlossaryAndModel method batchTranslateTextWithGlossaryAndModel.

// Batch translate text with Model and Glossary
public static void batchTranslateTextWithGlossaryAndModel(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, String glossaryId, String modelId) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // Supported Locations: `global`, [glossary location], or [model location]
        // Glossaries must be hosted in `us-central1`
        // Custom Models must use the same location as your model. (us-central1)
        String location = "us-central1";
        LocationName parent = LocationName.of(projectId, location);
        // Configure the source of the file from a GCS bucket
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
        // Configure where to store the output in a GCS bucket
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        // Configure the glossary used in the request
        GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
        TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
        // Configure the model used in the request
        String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
        // Build the request that will be sent to the API
        BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).putGlossaries(targetLanguage, glossaryConfig).putModels(targetLanguage, modelPath).build();
        // Start an asynchronous request
        OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = client.batchTranslateTextAsync(request);
        System.out.println("Waiting for operation to complete...");
        // random number between 300 - 450 (maximum allowed seconds)
        long randomNumber = ThreadLocalRandom.current().nextInt(450, 600);
        BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
        // Display the translation for each input text provided
        System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
        System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3.GcsSource) TranslateTextGlossaryConfig(com.google.cloud.translate.v3.TranslateTextGlossaryConfig) BatchTranslateResponse(com.google.cloud.translate.v3.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3.LocationName) BatchTranslateMetadata(com.google.cloud.translate.v3.BatchTranslateMetadata) OutputConfig(com.google.cloud.translate.v3.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3.BatchTranslateTextRequest) GlossaryName(com.google.cloud.translate.v3.GlossaryName) InputConfig(com.google.cloud.translate.v3.InputConfig) GcsDestination(com.google.cloud.translate.v3.GcsDestination)

Aggregations

Test (org.junit.Test)28 LocationName (com.google.privacy.dlp.v2.LocationName)22 OrganizationLocationName (com.google.privacy.dlp.v2.OrganizationLocationName)22 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)17 LocationName (com.google.cloud.automl.v1.LocationName)17 AbstractMessage (com.google.protobuf.AbstractMessage)14 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)13 LocationName (com.google.cloud.translate.v3.LocationName)13 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)13 StatusRuntimeException (io.grpc.StatusRuntimeException)13 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)12 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)12 LocationName (com.google.cloud.automl.v1beta1.LocationName)12 LocationName (com.google.cloud.translate.v3beta1.LocationName)10 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)10 Model (com.google.cloud.automl.v1.Model)8 Dataset (com.google.cloud.automl.v1.Dataset)7 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 GcsSource (com.google.cloud.translate.v3.GcsSource)5 ArrayList (java.util.ArrayList)5