Apache spark 如何使用bigquery api将spark连接到bigquery

Apache spark 如何使用bigquery api将spark连接到bigquery,apache-spark,google-api,google-bigquery,gcloud,google-app-invites,Apache Spark,Google Api,Google Bigquery,Gcloud,Google App Invites,我是gcloud和BigQuery的新手,希望使用spark从BigQuery读取数据。 我使用并能够连接bigquery。 我得到com.google.api.services.bigquery.bigquery对象,能够打印读取的数据集、tableId和tableData 我的问题是 如何将此Bigquery authenticate objectcredential对象连接到spark,或者是否可以将此对象与hadoopApi一起使用 如果没有可能,那么如何将凭证对象传递给newHadoo

我是gcloud和BigQuery的新手,希望使用spark从BigQuery读取数据。 我使用并能够连接bigquery。 我得到com.google.api.services.bigquery.bigquery对象,能够打印读取的数据集、tableId和tableData

我的问题是

如何将此Bigquery authenticate objectcredential对象连接到spark,或者是否可以将此对象与hadoopApi一起使用

如果没有可能,那么如何将凭证对象传递给newHadoopAPi呢

GoogleAuthorizationCodeFlow flow = getFlow();
    GoogleTokenResponse response = flow.newTokenRequest(authorizationCode)
            .setRedirectUri(REDIRECT_URI).execute();
    Credential credential=flow.createAndStoreCredential(response, null);
    return credential; 
我的Hadoop api代码是我想要使用凭证对象的地方

val tableData = sc.newAPIHadoopRDD(
  conf,
  classOf[GsonBigQueryInputFormat],
  classOf[LongWritable],
  classOf[JsonObject]).

我认为用于Hadoop的BigQuery连接器可以解决您的问题,而无需编写自己的低级客户端。请查看:


下面是一个使用它将Spark连接到BigQuery的示例:

Thanx@michael在您的链接帮助下,我找到了解决方案

只需在hadoop配置上禁用服务帐户

hadoopConfiguration.set("fs.gs.auth.service.account.enable", "false")
下面的代码将被使用

val hadoopConfiguration = sc.hadoopConfiguration
//BigQueryConfiguration.
hadoopConfiguration.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");
hadoopConfiguration.set(BigQueryConfiguration.PROJECT_ID_KEY, projectId);
hadoopConfiguration.set("fs.gs.project.id", projectId);
hadoopConfiguration.set("fs.gs.auth.service.account.enable", "false")
hadoopConfiguration.set("fs.gs.auth.client.id",
  clientId)
hadoopConfiguration.set("fs.gs.auth.client.secret",
  clientSecret)
hadoopConfiguration.set("fs.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem");
hadoopConfiguration.set("fs.gs.auth.client.file", tokenPath);
hadoopConfiguration.set(BigQueryConfiguration.GCS_BUCKET_KEY, bucket)

// Configure input and output for BigQuery access.
com.google.cloud.hadoop.io.bigquery.BigQueryConfiguration.configureBigQueryInput(hadoopConfiguration, dataSetId + "." + tableId)
val tableData = sc.newAPIHadoopRDD(
  hadoopConfiguration,
  classOf[GsonBigQueryInputFormat],
  classOf[LongWritable],
  classOf[JsonObject])
其中令牌路径包含刷新令牌

{
    "credentials": {
        "user": {
            "access_token":     "ya29..wgL6fH2Gx5asdaadsBl2Trasd0sBqV_ZAS7xKDtNS0z4Qyv5ypassdh0soplQ",
            "expiration_time_millis": 1460473581255,
            "refresh_token": "XXXXXXXXXxxxxxxxxx"
            }
       }
}

感谢回复@Michael Sheldon BigQuery hadoop连接器为我工作,但我想使用OAuth2刷新令牌进行身份验证