Configurar el usuario-agente

Configura un usuario-agente personalizado en un cliente de BigQuery

Muestra de código

Java

Antes de probar este ejemplo, sigue las instrucciones de configuración para Java incluidas en la guía de inicio rápido de BigQuery sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de BigQuery para Java.

Para autenticarte en BigQuery, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para bibliotecas cliente.

import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;

public class SetUserAgent {

  private static final String USER_AGENT_HEADER = "user-agent";

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String customUserAgentValue = "my-custom-user-agent-value";
    setUserAgent(projectId, customUserAgentValue);
  }

  public static void setUserAgent(String projectId, String customUserAgentValue)
      throws IOException {
    // Setup the credentials
    GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault();

    // Initialize the HeaderProvider object with custom user agent value
    HeaderProvider headerProvider =
        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.
    BigQuery bigQuery =
        BigQueryOptions.newBuilder()
            .setProjectId(projectId)
            .setCredentials(googleCredentials)
            .setHeaderProvider(headerProvider)
            .build()
            .getService();

    System.out.println(bigQuery.getOptions().getUserAgent());
  }
}

Node.js

Antes de probar este ejemplo, sigue las instrucciones de configuración para Node.js incluidas en la guía de inicio rápido de BigQuery sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de BigQuery para Node.js.

Para autenticarte en BigQuery, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para bibliotecas cliente.

// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');
// Create a client and set the user agent
const bigquery = new BigQuery({userAgent: 'my-user-agent'});

console.log('User agent:');
console.log(bigquery.providedUserAgent);

¿Qué sigue?

Para buscar y filtrar muestras de código para otros productos de Google Cloud, consulta el navegador de muestra de Google Cloud.