Lee desde una consulta
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Usa el conector de BigQueryIO para leer desde el resultado de una consulta.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to use the BigQueryIO connector to read data from a query result within a Dataflow pipeline.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Java code example executes a SQL query against a BigQuery public dataset to retrieve and process repository commit counts.\u003c/p\u003e\n"],["\u003cp\u003eThe code snippet utilizes the \u003ccode\u003eManaged.read(Managed.BIGQUERY)\u003c/code\u003e function to read the query results and then transforms the output rows.\u003c/p\u003e\n"],["\u003cp\u003eApplication Default Credentials are required to authenticate to Dataflow when using the provided code sample.\u003c/p\u003e\n"],["\u003cp\u003eThe full documentation can be found in the \u003ccode\u003eRead from BigQuery to Dataflow\u003c/code\u003e resource.\u003c/p\u003e\n"]]],[],null,["# Read from a query\n\nUse the BigQueryIO connector to read from a query result.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Read from BigQuery to Dataflow](/dataflow/docs/guides/read-from-bigquery)\n\nCode sample\n-----------\n\n### Java\n\n\nTo authenticate to Dataflow, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.common.collect.ImmutableMap;\n import org.apache.beam.sdk.Pipeline;\n import org.apache.beam.sdk.managed.Managed;\n import org.apache.beam.sdk.options.PipelineOptions;\n import org.apache.beam.sdk.options.PipelineOptionsFactory;\n import org.apache.beam.sdk.transforms.MapElements;\n import org.apache.beam.sdk.values.Row;\n import org.apache.beam.sdk.values.TypeDescriptors;\n\n public class BigQueryReadFromQuery {\n public static void main(String[] args) {\n // The SQL query to run inside BigQuery.\n final String queryString =\n \"SELECT repo_name as repo, COUNT(*) as count \"\n + \"FROM `bigquery-public-data.github_repos.sample_commits` \"\n + \"GROUP BY repo_name\";\n\n // Parse the pipeline options passed into the application.\n // For more information, see https://beam.apache.org/documentation/programming-guide/#configuring-pipeline-options\n PipelineOptions options = PipelineOptionsFactory.fromArgs(args)\n .withValidation().create();\n\n ImmutableMap\u003cString, Object\u003e config = ImmutableMap.\u003cString, Object\u003ebuilder()\n .put(\"query\", queryString)\n .build();\n\n // Create a pipeline and apply transforms.\n Pipeline pipeline = Pipeline.create(options);\n pipeline\n .apply(Managed.read(Managed.BIGQUERY).withConfig(config)).getSinglePCollection()\n .apply(MapElements\n .into(TypeDescriptors.strings())\n // Access individual fields in the row.\n .via((Row row) -\u003e {\n String output = String.format(\"Repo: %s, commits: %d%n\",\n row.getString(\"repo\"),\n row.getInt64(\"count\"));\n System.out.println(output);\n return output;\n }));\n pipeline.run().waitUntilFinish();\n }\n }\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=dataflow)."]]