Definir user agent
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Defina um user agent personalizado em um cliente do BigQuery.
Exemplo de código
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to set a custom user agent for a BigQuery client using both Java and Node.js.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Java code sample initializes a \u003ccode\u003eHeaderProvider\u003c/code\u003e with a custom user agent value, which is then used when building the \u003ccode\u003eBigQuery\u003c/code\u003e client.\u003c/p\u003e\n"],["\u003cp\u003eThe Node.js example shows setting a user agent directly within the \u003ccode\u003eBigQuery\u003c/code\u003e client initialization using a simple option.\u003c/p\u003e\n"],["\u003cp\u003eBoth examples utilize the user agent values for tracking purposes, and require authentication setup with Application Default Credentials, as detailed in the documentation links.\u003c/p\u003e\n"]]],[],null,["# Set user agent\n\nSet a custom user agent on a BigQuery client.\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.FixedHeaderProvider.html;\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.HeaderProvider.html;\n import com.google.auth.oauth2.https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.common.collect.ImmutableMap;\n import java.io.IOException;\n\n public class SetUserAgent {\n\n private static final String USER_AGENT_HEADER = \"user-agent\";\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"my-project-id\";\n String customUserAgentValue = \"my-custom-user-agent-value\";\n setUserAgent(projectId, customUserAgentValue);\n }\n\n public static void setUserAgent(String projectId, String customUserAgentValue)\n throws IOException {\n // Setup the credentials\n https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html googleCredentials = https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html.https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html#com_google_auth_oauth2_GoogleCredentials_getApplicationDefault__();\n\n // Initialize the HeaderProvider object with custom user agent value\n https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.HeaderProvider.html headerProvider =\n https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.FixedHeaderProvider.html.create(ImmutableMap.of(USER_AGENT_HEADER, customUserAgentValue));\n\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigQuery =\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.newBuilder()\n .setProjectId(projectId)\n .https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ClientContext.Builder.html#com_google_api_gax_rpc_ClientContext_Builder_setCredentials_com_google_auth_Credentials_(googleCredentials)\n .setHeaderProvider(headerProvider)\n .build()\n .getService();\n\n System.out.println(bigQuery.getOptions().getUserAgent());\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Node.js API\nreference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n // Import the Google Cloud client library\n const {BigQuery} = require('https://cloud.google.com/nodejs/docs/reference/bigquery/latest/overview.html');\n // Create a client and set the user agent\n const bigquery = new https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html({userAgent: 'my-user-agent'});\n\n console.log('User agent:');\n console.log(bigquery.providedUserAgent);\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]