Create a routine

Create a routine within an existing dataset.

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""cloud.google.com/go/bigquery")// createRoutine demonstrates creating a new BigQuery UDF using the routine API.funccreateRoutine(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()metaData:=&bigquery.RoutineMetadata{Type:"SCALAR_FUNCTION",Language:"SQL",Body:"x * 3",Arguments:[]*bigquery.RoutineArgument{{Name:"x",DataType:&bigquery.StandardSQLDataType{TypeKind:"INT64"}},},}routineRef:=client.Dataset(datasetID).Routine(routineID)iferr:=routineRef.Create(ctx,metaData);err!=nil{returnerr}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.RoutineArgument;importcom.google.cloud.bigquery.RoutineId;importcom.google.cloud.bigquery.RoutineInfo;importcom.google.cloud.bigquery.StandardSQLDataType;importcom.google.cloud.bigquery.StandardSQLTypeName;importcom.google.common.collect.ImmutableList;// Sample to create a routinepublicclassCreateRoutine{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringdatasetName="MY_DATASET_NAME";StringroutineName="MY_ROUTINE_NAME";createRoutine(datasetName,routineName);}publicstaticvoidcreateRoutine(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);RoutineInforoutineInfo=RoutineInfo.newBuilder(routineId).setRoutineType("SCALAR_FUNCTION").setLanguage("SQL").setBody("x * 3").setArguments(ImmutableList.of(RoutineArgument.newBuilder().setName("x").setDataType(StandardSQLDataType.newBuilder(StandardSQLTypeName.INT64).build()).build())).build();bigquery.create(routineInfo);System.out.println("Routine created successfully");}catch(BigQueryExceptione){System.out.println("Routine was not created. \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();asyncfunctioncreateRoutine(){// Creates a new 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 referenceletroutine=dataset.routine(routineId);constconfig={arguments:[{name:'x',dataType:{typeKind:'INT64',},},],definitionBody:'x * 3',routineType:'SCALAR_FUNCTION',returnType:{typeKind:'INT64',},};// Make API call[routine]=awaitroutine.create(config);console.log(`Routine${routineId} created.`);}createRoutine();

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): Choose a fully qualified ID for the routine.# routine_id = "my-project.my_dataset.my_routine"routine=bigquery.Routine(routine_id,type_="SCALAR_FUNCTION",language="SQL",body="x * 3",arguments=[bigquery.RoutineArgument(name="x",data_type=bigquery.StandardSqlDataType(type_kind=bigquery.StandardSqlTypeNames.INT64),)],)routine=client.create_routine(routine)# Make an API request.print("Created routine{}".format(routine.reference))

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.