Search in sources :

Example 1 with ExportDataRequest

use of com.google.cloud.datalabeling.v1beta1.ExportDataRequest in project java-datalabeling by googleapis.

the class ExportData method exportData.

// Export data from an annotated dataset.
static void exportData(String datasetName, String annotatedDatasetName, String gcsOutputUri) throws IOException {
    // String datasetName = DataLabelingServiceClient.formatDatasetName(
    // "YOUR_PROJECT_ID", "YOUR_DATASETS_UUID");
    // String annotatedDatasetName = DataLabelingServiceClient.formatAnnotatedDatasetName(
    // "YOUR_PROJECT_ID",
    // "YOUR_DATASET_UUID",
    // "YOUR_ANNOTATED_DATASET_UUID");
    // String gcsOutputUri = "gs://YOUR_BUCKET_ID/export_path";
    // [END datalabeling_export_data_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_export_data_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUri(gcsOutputUri).setMimeType("text/csv").build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        ExportDataRequest exportDataRequest = ExportDataRequest.newBuilder().setName(datasetName).setOutputConfig(outputConfig).setAnnotatedDataset(annotatedDatasetName).build();
        OperationFuture<ExportDataOperationResponse, ExportDataOperationMetadata> operation = dataLabelingServiceClient.exportDataAsync(exportDataRequest);
        ExportDataOperationResponse response = operation.get();
        System.out.format("Exported item count: %d\n", response.getExportCount());
        LabelStats labelStats = response.getLabelStats();
        Set<Entry<String, Long>> entries = labelStats.getExampleCountMap().entrySet();
        for (Entry<String, Long> entry : entries) {
            System.out.format("\tLabel: %s\n", entry.getKey());
            System.out.format("\tCount: %d\n\n", entry.getValue());
        }
    } catch (IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) ExportDataRequest(com.google.cloud.datalabeling.v1beta1.ExportDataRequest) IOException(java.io.IOException) ExportDataOperationResponse(com.google.cloud.datalabeling.v1beta1.ExportDataOperationResponse) Entry(java.util.Map.Entry) OutputConfig(com.google.cloud.datalabeling.v1beta1.OutputConfig) ExportDataOperationMetadata(com.google.cloud.datalabeling.v1beta1.ExportDataOperationMetadata) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings) GcsDestination(com.google.cloud.datalabeling.v1beta1.GcsDestination) LabelStats(com.google.cloud.datalabeling.v1beta1.LabelStats) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

DataLabelingServiceClient (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient)1 DataLabelingServiceSettings (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings)1 ExportDataOperationMetadata (com.google.cloud.datalabeling.v1beta1.ExportDataOperationMetadata)1 ExportDataOperationResponse (com.google.cloud.datalabeling.v1beta1.ExportDataOperationResponse)1 ExportDataRequest (com.google.cloud.datalabeling.v1beta1.ExportDataRequest)1 GcsDestination (com.google.cloud.datalabeling.v1beta1.GcsDestination)1 LabelStats (com.google.cloud.datalabeling.v1beta1.LabelStats)1 OutputConfig (com.google.cloud.datalabeling.v1beta1.OutputConfig)1 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 ExecutionException (java.util.concurrent.ExecutionException)1