Search in sources :

Example 31 with TransferConfig

use of com.google.cloud.bigquery.datatransfer.v1.TransferConfig in project java-bigquerydatatransfer by googleapis.

the class RunNotification method runNotification.

public static void runNotification(String projectId, TransferConfig transferConfig) throws IOException {
    try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
        ProjectName parent = ProjectName.of(projectId);
        CreateTransferConfigRequest request = CreateTransferConfigRequest.newBuilder().setParent(parent.toString()).setTransferConfig(transferConfig).build();
        TransferConfig config = dataTransferServiceClient.createTransferConfig(request);
        System.out.println("\nScheduled query with run notification created successfully :" + config.getName());
    } catch (ApiException ex) {
        System.out.print("\nScheduled query with run notification was not created." + ex.toString());
    }
}
Also used : DataTransferServiceClient(com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient) ProjectName(com.google.cloud.bigquery.datatransfer.v1.ProjectName) TransferConfig(com.google.cloud.bigquery.datatransfer.v1.TransferConfig) CreateTransferConfigRequest(com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest) ApiException(com.google.api.gax.rpc.ApiException)

Example 32 with TransferConfig

use of com.google.cloud.bigquery.datatransfer.v1.TransferConfig in project java-bigquerydatatransfer by googleapis.

the class UpdateCredentials method updateCredentials.

public static void updateCredentials(TransferConfig transferConfig, String serviceAccount, FieldMask updateMask) throws IOException {
    try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
        UpdateTransferConfigRequest request = UpdateTransferConfigRequest.newBuilder().setTransferConfig(transferConfig).setUpdateMask(updateMask).setServiceAccountName(serviceAccount).build();
        dataTransferServiceClient.updateTransferConfig(request);
        System.out.println("Credentials updated successfully");
    } catch (ApiException ex) {
        System.out.print("Credentials was not updated." + ex.toString());
    }
}
Also used : DataTransferServiceClient(com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient) UpdateTransferConfigRequest(com.google.cloud.bigquery.datatransfer.v1.UpdateTransferConfigRequest) ApiException(com.google.api.gax.rpc.ApiException)

Example 33 with TransferConfig

use of com.google.cloud.bigquery.datatransfer.v1.TransferConfig in project java-bigquerydatatransfer by googleapis.

the class CreateCloudStorageTransferIT method testCreateCloudStorageTransfer.

@Test
public void testCreateCloudStorageTransfer() throws IOException {
    String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
    String fileFormat = "CSV";
    String fieldDelimiter = ",";
    String skipLeadingRows = "1";
    Map<String, Value> params = new HashMap<>();
    params.put("destination_table_name_template", Value.newBuilder().setStringValue(tableName).build());
    params.put("data_path_template", Value.newBuilder().setStringValue(sourceUri).build());
    params.put("write_disposition", Value.newBuilder().setStringValue("APPEND").build());
    params.put("file_format", Value.newBuilder().setStringValue(fileFormat).build());
    params.put("field_delimiter", Value.newBuilder().setStringValue(fieldDelimiter).build());
    params.put("skip_leading_rows", Value.newBuilder().setStringValue(skipLeadingRows).build());
    TransferConfig transferConfig = TransferConfig.newBuilder().setDestinationDatasetId(datasetName).setDisplayName(displayName).setDataSourceId("google_cloud_storage").setParams(Struct.newBuilder().putAllFields(params).build()).setSchedule("every 24 hours").build();
    CreateCloudStorageTransfer.createCloudStorageTransfer(PROJECT_ID, transferConfig);
    String result = bout.toString();
    name = result.substring(result.indexOf(":") + 1, result.length() - 1);
    assertThat(result).contains("Cloud storage transfer created successfully :");
}
Also used : HashMap(java.util.HashMap) Value(com.google.protobuf.Value) TransferConfig(com.google.cloud.bigquery.datatransfer.v1.TransferConfig) Test(org.junit.Test)

Example 34 with TransferConfig

use of com.google.cloud.bigquery.datatransfer.v1.TransferConfig in project java-bigquerydatatransfer by googleapis.

the class CreateScheduledQueryIT method testCreateScheduledQuery.

@Test
public void testCreateScheduledQuery() throws IOException {
    String query = "SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time, " + "@run_date as intended_run_date, 17 as some_integer";
    String destinationTableName = "MY_DESTINATION_TABLE_" + UUID.randomUUID().toString().substring(0, 8) + "_{run_date}";
    Map<String, Value> params = new HashMap<>();
    params.put("query", Value.newBuilder().setStringValue(query).build());
    params.put("destination_table_name_template", Value.newBuilder().setStringValue(destinationTableName).build());
    params.put("write_disposition", Value.newBuilder().setStringValue("WRITE_TRUNCATE").build());
    params.put("partitioning_field", Value.newBuilder().setStringValue("").build());
    TransferConfig transferConfig = TransferConfig.newBuilder().setDestinationDatasetId(datasetName).setDisplayName(displayName).setDataSourceId("scheduled_query").setParams(Struct.newBuilder().putAllFields(params).build()).setSchedule("every 24 hours").setState(TransferState.CANCELLED).build();
    CreateScheduledQuery.createScheduledQuery(PROJECT_ID, transferConfig);
    String result = bout.toString();
    name = result.substring(result.indexOf(":") + 1, result.length() - 1);
    assertThat(result).contains("Scheduled query created successfully");
}
Also used : HashMap(java.util.HashMap) Value(com.google.protobuf.Value) TransferConfig(com.google.cloud.bigquery.datatransfer.v1.TransferConfig) Test(org.junit.Test)

Example 35 with TransferConfig

use of com.google.cloud.bigquery.datatransfer.v1.TransferConfig in project java-bigquerydatatransfer by googleapis.

the class UpdateTransferConfigIT method testUpdateTransferConfig.

@Test
public void testUpdateTransferConfig() throws IOException {
    TransferConfig transferConfig = TransferConfig.newBuilder().setName(CONFIG_NAME).setDisplayName("UPDATED_DISPLAY_NAME").build();
    FieldMask updateMask = FieldMaskUtil.fromString("display_name");
    UpdateTransferConfig.updateTransferConfig(transferConfig, updateMask);
    assertThat(bout.toString()).contains("Transfer config updated successfully");
}
Also used : TransferConfig(com.google.cloud.bigquery.datatransfer.v1.TransferConfig) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Aggregations

TransferConfig (com.google.cloud.bigquery.datatransfer.v1.TransferConfig)49 Value (com.google.protobuf.Value)22 HashMap (java.util.HashMap)22 DataTransferServiceClient (com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient)21 ApiException (com.google.api.gax.rpc.ApiException)19 CreateTransferConfigRequest (com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest)16 ProjectName (com.google.cloud.bigquery.datatransfer.v1.ProjectName)16 Test (org.junit.Test)13 FieldMask (com.google.protobuf.FieldMask)9 UpdateTransferConfigRequest (com.google.cloud.bigquery.datatransfer.v1.UpdateTransferConfigRequest)4 ListTransferConfigsPagedResponse (com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient.ListTransferConfigsPagedResponse)3 AbstractMessage (com.google.protobuf.AbstractMessage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 Before (org.junit.Before)3 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)1 GetTransferConfigRequest (com.google.cloud.bigquery.datatransfer.v1.GetTransferConfigRequest)1