Dataproc + BigQuery示例 - 任何可用的?

Gra*_*ley 10 google-bigquery google-cloud-platform google-cloud-dataproc

根据Dataproc docos,它具有" 与BigQuery的本机和自动集成 ".

我在BigQuery中有一个表.我想阅读该表并使用我创建的Dataproc集群(使用PySpark作业)对其进行一些分析.然后将此分析的结果写回BigQuery.您可能会问"为什么不直接在BigQuery中进行分析!?" - 原因是因为我们正在创建复杂的统计模型,而SQL的开发水平太高了.我们需要像Python或R,ergo Dataproc这样的东西.

他们是否有Dataproc + BigQuery示例?我找不到任何东西.

Jam*_*mes 9

首先,如本问题所述,Cloud Dataproc上预装了BigQuery连接器群集上.

这是一个如何从BigQuery读取数据到Spark的示例.在此示例中,我们将从BigQuery读取数据以执行字数统计.您使用Spark从BigQuery读取数据SparkContext.newAPIHadoopRDD.在星火文档有大约使用的详细信息SparkContext.newAPIHadoopRDD."

import com.google.cloud.hadoop.io.bigquery.BigQueryConfiguration
import com.google.cloud.hadoop.io.bigquery.GsonBigQueryInputFormat
import com.google.cloud.hadoop.io.bigquery.mapred.BigQueryMapredInputFormat
import com.google.gson.JsonObject

import org.apache.hadoop.io.LongWritable

val projectId = "<your-project-id>"
val fullyQualifiedInputTableId = "publicdata:samples.shakespeare"
val fullyQualifiedOutputTableId = "<your-fully-qualified-table-id>"
val outputTableSchema =
    "[{'name': 'Word','type': 'STRING'},{'name': 'Count','type': 'INTEGER'}]"
val jobName = "wordcount"

val conf = sc.hadoopConfiguration

// Set the job-level projectId.
conf.set(BigQueryConfiguration.PROJECT_ID_KEY, projectId)

// Use the systemBucket for temporary BigQuery export data used by the InputFormat.
val systemBucket = conf.get("fs.gs.system.bucket")
conf.set(BigQueryConfiguration.GCS_BUCKET_KEY, systemBucket)

// Configure input and output for BigQuery access.
BigQueryConfiguration.configureBigQueryInput(conf, fullyQualifiedInputTableId)
BigQueryConfiguration.configureBigQueryOutput(conf,
    fullyQualifiedOutputTableId, outputTableSchema)

val fieldName = "word"

val tableData = sc.newAPIHadoopRDD(conf,
    classOf[GsonBigQueryInputFormat], classOf[LongWritable], classOf[JsonObject])
tableData.cache()
tableData.count()
tableData.map(entry => (entry._1.toString(),entry._2.toString())).take(10)
Run Code Online (Sandbox Code Playgroud)

您需要使用您的设置自定义此示例,包括您的Cloud Platform项目ID <your-project-id>和输出表ID <your-fully-qualified-table-id>.

最后,如果您最终将BigQuery连接器与MapReduce一起使用,则此页面提供了有关如何使用BigQuery连接器编写MapReduce作业的示例.

  • 这段代码有python版本吗? (3认同)