Get a routine

Get a routine resource for a given routine ID.

Code sample

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.

import("context""fmt""io""cloud.google.com/go/bigquery")// getRoutine demonstrates getting a routine's metadata via the API.funcgetRoutine(wio.Writer,projectID,datasetID,routineIDstring)error{// projectID := "my-project-id"// datasetID := "mydatasetid"// routineID := "myroutineid"ctx:=context.Background()client,err:=bigquery.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("bigquery.NewClient: %w",err)}deferclient.Close()meta,err:=client.Dataset(datasetID).Routine(routineID).Metadata(ctx)iferr!=nil{returnfmt.Errorf("couldn't retrieve routine metadata: %w",err)}// Print information about the routine.fmt.Fprintf(w,"Routine %s:\n",routineID)fmt.Fprintf(w,"\tType %s:\n",meta.Type)fmt.Fprintf(w,"\tLanguage %s:\n",meta.Language)fmt.Fprintln(w,"\tArguments:")for_,v:=rangemeta.Arguments{fmt.Fprintf(w,"\t\tName: %s\tType: %v",v.Name,v.DataType)}returnnil}

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.

importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQueryException;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.cloud.bigquery.Routine;importcom.google.cloud.bigquery.RoutineId;// Sample to get a routinepublicclassGetRoutine{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringdatasetName="MY_DATASET_NAME";StringroutineName="MY_ROUTINE_NAME";getRoutine(datasetName,routineName);}publicstaticvoidgetRoutine(StringdatasetName,StringroutineName){try{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.BigQuerybigquery=BigQueryOptions.getDefaultInstance().getService();RoutineIdroutineId=RoutineId.of(datasetName,routineName);Routineroutine=bigquery.getRoutine(routineId);System.out.println("Routine retrieved successfully"+routine.getDescription());}catch(BigQueryExceptione){System.out.println("Routine not retrieved. \n"+e.toString());}}}

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.

// Import the Google Cloud client library and create a clientconst{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();asyncfunctiongetRoutine(){// Gets an existing routine named "my_routine" in "my_dataset"./**   * TODO(developer): Uncomment the following lines before running the sample.   */// const datasetId = 'my_dataset';// const routineId = 'my_routine';constdataset=bigquery.dataset(datasetId);// Create routine reference and make API callconst[routine]=awaitdataset.routine(routineId).get();console.log(`Routine${routine.metadata.routineReference.routineId} retrieved.`,);}getRoutine();

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# Construct a BigQuery client object.client=bigquery.Client()# TODO(developer): Set the fully-qualified ID for the routine.# routine_id = "my-project.my_dataset.my_routine"routine=client.get_routine(routine_id)# Make an API request.print("Routine '{}':".format(routine.reference))print("\tType: '{}'".format(routine.type_))print("\tLanguage: '{}'".format(routine.language))print("\tArguments:")forargumentinroutine.arguments:print("\t\tName: '{}'".format(argument.name))print("\t\tType: '{}'".format(argument.data_type))

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.