Set user agent

Set a custom user agent on a BigQuery client.

Code sample

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.api.gax.rpc.FixedHeaderProvider;importcom.google.api.gax.rpc.HeaderProvider;importcom.google.auth.oauth2.GoogleCredentials;importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.common.collect.ImmutableMap;importjava.io.IOException;publicclassSetUserAgent{privatestaticfinalStringUSER_AGENT_HEADER="user-agent";publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.StringprojectId="my-project-id";StringcustomUserAgentValue="my-custom-user-agent-value";setUserAgent(projectId,customUserAgentValue);}publicstaticvoidsetUserAgent(StringprojectId,StringcustomUserAgentValue)throwsIOException{// Setup the credentialsGoogleCredentialsgoogleCredentials=GoogleCredentials.getApplicationDefault();// Initialize the HeaderProvider object with custom user agent valueHeaderProviderheaderProvider=FixedHeaderProvider.create(ImmutableMap.of(USER_AGENT_HEADER,customUserAgentValue));// 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.newBuilder().setProjectId(projectId).setCredentials(googleCredentials).setHeaderProvider(headerProvider).build().getService();System.out.println(bigQuery.getOptions().getUserAgent());}}

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 libraryconst{BigQuery}=require('@google-cloud/bigquery');// Create a client and set the user agentconstbigquery=newBigQuery({userAgent:'my-user-agent'});console.log('User agent:');console.log(bigquery.providedUserAgent);

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.