List data sources

Lists all supported data sources for the BigQuery Data Transfer Service.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

Before trying this sample, follow theC# setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryC# API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

usingGoogle.Api.Gax.ResourceNames;usingGoogle.Cloud.BigQuery.DataTransfer.V1;usingSystem;namespaceGoogleCloudSamples{publicclassQuickStart{publicstaticvoidMain(string[]args){// Instantiates a clientDataTransferServiceClientclient=DataTransferServiceClient.Create();// Your Google Cloud Platform project IDstringprojectId="YOUR-PROJECT-ID";ProjectNameproject=ProjectName.FromProject(projectId);varsources=client.ListDataSources(project);Console.WriteLine("Supported Data Sources:");foreach(DataSourcesourceinsources){Console.WriteLine($"{source.DataSourceId}: "+$"{source.DisplayName} ({source.Description})");}}}}

Go

Before trying this sample, follow theGo setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryGo API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

// Sample bigquery-quickstart creates a Google BigQuery dataset.packagemainimport("context""fmt""log""google.golang.org/api/iterator"// Imports the BigQuery Data Transfer client package.datatransfer"cloud.google.com/go/bigquery/datatransfer/apiv1""cloud.google.com/go/bigquery/datatransfer/apiv1/datatransferpb")funcmain(){ctx:=context.Background()// Sets your Google Cloud Platform project ID.projectID:="YOUR_PROJECT_ID"// Creates a client.client,err:=datatransfer.NewClient(ctx)iferr!=nil{log.Fatalf("Failed to create client: %v",err)}deferclient.Close()req:=&datatransferpb.ListDataSourcesRequest{Parent:fmt.Sprintf("projects/%s",projectID),}it:=client.ListDataSources(ctx,req)fmt.Println("Supported Data Sources:")for{ds,err:=it.Next()iferr==iterator.Done{break}iferr!=nil{log.Fatalf("Failed to list sources: %v",err)}fmt.Println(ds.DisplayName)fmt.Println("\tID: ",ds.DataSourceId)fmt.Println("\tFull path: ",ds.Name)fmt.Println("\tDescription: ",ds.Description)}}

Java

Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

// Imports the Google Cloud client libraryimportcom.google.cloud.bigquery.datatransfer.v1.DataSource;importcom.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;importcom.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient.ListDataSourcesPagedResponse;importcom.google.cloud.bigquery.datatransfer.v1.ListDataSourcesRequest;publicclassQuickstartSample{/** List available data sources for the BigQuery Data Transfer service. */publicstaticvoidmain(String...args)throwsException{// Sets your Google Cloud Platform project ID.// String projectId = "YOUR_PROJECT_ID";StringprojectId=args[0];// Instantiate a client. If you don't specify credentials when constructing a client, the// client library will look for credentials in the environment, such as the// GOOGLE_APPLICATION_CREDENTIALS environment variable.try(DataTransferServiceClientclient=DataTransferServiceClient.create()){// Request the list of available data sources.Stringparent=String.format("projects/%s",projectId);ListDataSourcesRequestrequest=ListDataSourcesRequest.newBuilder().setParent(parent).build();ListDataSourcesPagedResponseresponse=client.listDataSources(request);// Print the results.System.out.println("Supported Data Sources:");for(DataSourcedataSource:response.iterateAll()){System.out.println(dataSource.getDisplayName());System.out.printf("\tID: %s%n",dataSource.getDataSourceId());System.out.printf("\tFull path: %s%n",dataSource.getName());System.out.printf("\tDescription: %s%n",dataSource.getDescription());}}}}

Node.js

Before trying this sample, follow theNode.js setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryNode.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

const{DataTransferServiceClient,}=require('@google-cloud/bigquery-data-transfer');const{status}=require('@grpc/grpc-js');constclient=newDataTransferServiceClient();/** * Lists all available data sources for a project. * Data sources are the services that can be used to create data transfers into BigQuery. * * @param {string} projectId The Google Cloud project ID, for example 'example-project-id'. */asyncfunctionlistDataSources(projectId){constrequest={parent:`projects/${projectId}`,};try{const[dataSources]=awaitclient.listDataSources(request);if(dataSources.length===0){console.log(`No data sources found in project${projectId}.`);return;}console.log('Supported data sources:');for(constdataSourceofdataSources){console.log(`\nData source:${dataSource.name}`);console.log(`  ID:${dataSource.dataSourceId}`);console.log(`  Display Name:${dataSource.displayName}`);}}catch(err){if(err.code===status.NOT_FOUND){console.error(`Project${projectId} not found.`);}else{console.error('An error occurred:',err);}}}

PHP

Before trying this sample, follow thePHP setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPHP API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

# Includes the autoloader for libraries installed with composerrequire __DIR__ . '/vendor/autoload.php';# Imports the Google Cloud client libraryuse Google\Cloud\BigQuery\DataTransfer\V1\Client\DataTransferServiceClient;use Google\Cloud\BigQuery\DataTransfer\V1\ListDataSourcesRequest;# Instantiates a client$bqdtsClient = new DataTransferServiceClient();# Your Google Cloud Platform project ID$projectId = 'YOUR_PROJECT_ID';$parent = sprintf('projects/%s/locations/us', $projectId);try {    echo 'Supported Data Sources:', PHP_EOL;    $listDataSourcesRequest = (new ListDataSourcesRequest())        ->setParent($parent);    $pagedResponse = $bqdtsClient->listDataSources($listDataSourcesRequest);    foreach ($pagedResponse->iterateAllElements() as $dataSource) {        echo 'Data source: ', $dataSource->getDisplayName(), PHP_EOL;        echo 'ID: ', $dataSource->getDataSourceId(), PHP_EOL;        echo 'Full path: ', $dataSource->getName(), PHP_EOL;        echo 'Description: ', $dataSource->getDescription(), PHP_EOL;    }} finally {    $bqdtsClient->close();}

Python

Before trying this sample, follow thePython setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPython API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

fromgoogle.cloudimportbigquery_datatransferclient=bigquery_datatransfer.DataTransferServiceClient()# TODO: Update to your project ID.project_id="my-project"# Get the full path to your project.parent=client.common_project_path(project_id)print("Supported Data Sources:")# Iterate over all possible data sources.fordata_sourceinclient.list_data_sources(parent=parent):print("{}:".format(data_source.display_name))print("\tID:{}".format(data_source.data_source_id))print("\tFull path:{}".format(data_source.name))print("\tDescription:{}".format(data_source.description))

Ruby

Before trying this sample, follow theRuby setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryRuby API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

# Imports the Google Cloud client libraryrequire"google/cloud/bigquery/data_transfer"# Your Google Cloud Platform project ID# project_id = "YOUR_PROJECT_ID"# Instantiate a clientdata_transfer=Google::Cloud::Bigquery::DataTransfer.data_transfer_service# Get the full path to your project.project_path=data_transfer.project_pathproject:project_idputs"Supported Data Sources:"# Iterate over all possible data sources.data_transfer.list_data_sources(parent:project_path).eachdo|data_source|puts"Data source:#{data_source.display_name}"puts"ID:#{data_source.data_source_id}"puts"Full path:#{data_source.name}"puts"Description:#{data_source.description}"end

What's next

To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.