简体   繁体   English

使用 Bigquery Java 库从 Bigquery 读取数据时,我们可以将位置从美国更改为其他地区吗?

[英]Can we change location from US to other region while reading data from Bigquery using Bigquery java library?

I am trying to read data from Bigquery using Bigquery java library .我正在尝试使用Bigquery java 库Bigquery读取数据。

My dataset is not in US location, so when i am giving my dataset name to library , it is throwing an error that dataset not found in US location because it searches by default in US location.我的数据集不在美国位置,所以当我将我的数据集名称提供给 library 时,它会抛出一个错误,即在美国位置找不到数据集,因为它默认在美国位置搜索。

I have also tried giving the location using setLocation("asia-southeast1") but still it is finding in US location.我也尝试使用 setLocation("asia-southeast1") 给出位置,但它仍然在美国位置找到。

This is my code snippet:这是我的代码片段:

val bigquery: BigQuery =BigQueryOptions.newBuilder().setLocation("asia-southeast1").build().getService
val query = "SELECT TO_JSON_STRING(t, true) AS json_row FROM "+dbName+"."+tableName+" AS t"
logger.info("Query is " + query)
val queryResult: QueryJobConfiguration = QueryJobConfiguration.newBuilder(query).build
val result: TableResult = bigquery.query(queryResult)

I am writing code in SCALA.我正在 SCALA 中编写代码。 As it uses same libraries as JAVA and JAVA is more popular, thats why I am asking this for JAVA.因为它使用与 JAVA 相同的库,而且 JAVA 更受欢迎,这就是为什么我要求 JAVA 这样做。

Please help me to know that how I can change location from US to southeast.请帮助我知道如何将位置从美国更改为东南部。

Can I change something inside QueryJobConfiguration as i have searched a-lot but i am unable to find anything.我是否可以更改QueryJobConfiguration 中的某些内容,因为我已经搜索了很多但我找不到任何东西。

My only requirement is that I want final result as TableResult.我唯一的要求是我希望最终结果为 TableResult。

This is the exception being thrown这是抛出的异常

com.google.cloud.bigquery.BigQueryException: Not found: Dataset XXXXXXXX was not found in location US
at com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc.translate(HttpBigQueryRpc.java:106)
at com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc.getQueryResults(HttpBigQueryRpc.java:584)
at com.google.cloud.bigquery.BigQueryImpl$34.call(BigQueryImpl.java:1203)
at com.google.cloud.bigquery.BigQueryImpl$34.call(BigQueryImpl.java:1198)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
at com.google.cloud.bigquery.BigQueryImpl.getQueryResults(BigQueryImpl.java:1197)
at com.google.cloud.bigquery.BigQueryImpl.getQueryResults(BigQueryImpl.java:1181)
at com.google.cloud.bigquery.Job$1.call(Job.java:329)
at com.google.cloud.bigquery.Job$1.call(Job.java:326)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
at com.google.cloud.RetryHelper.poll(RetryHelper.java:64)
at com.google.cloud.bigquery.Job.waitForQueryResults(Job.java:325)
at com.google.cloud.bigquery.Job.getQueryResults(Job.java:291)
at com.google.cloud.bigquery.BigQueryImpl.query(BigQueryImpl.java:1168)
...

Thanks in advance.提前致谢。

You shouldn't actually need to specify the location because BigQuery will infer it from the dataset being referenced in your query.您实际上不需要指定位置,因为 BigQuery 会从查询中引用的数据集推断出它。 See here .这里

When loading data, querying data, or exporting data, BigQuery determines the location to run the job based on the datasets referenced in the request.在加载数据、查询数据或导出数据时,BigQuery 会根据请求中引用的数据集确定运行作业的位置。 For example, if a query references a table in a dataset stored in the asia-northeast1 region, the query job will run in that region.例如,如果查询引用存储在 asia-northeast1 区域中的数据集中的表,则查询作业将在该区域中运行。

I just tested using the Java SDK on a dataset/table I created in asia-southeast1 , and it worked without needing to explicitly specify the location.我刚刚在asia-southeast1创建的数据集/表上使用 Java SDK 进行了测试,它无需明确指定位置即可工作。

在此处输入图片说明

在此处输入图片说明

If it's still not working for you by default (check the table you're referncing actually exists), then you can specify the location by setting it in the JobId and passing that to the overloaded method:如果默认情况下它仍然不适合您(检查您引用的表是否实际存在),那么您可以通过在JobId设置它并将其传递给重载方法来指定位置:

String query = "SELECT * FROM `grey-sort-challenge.asia_southeast1.a_table`;";
        QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query)
                .setUseLegacySql(Boolean.FALSE)
                .build();

        JobId id = JobId.newBuilder().setLocation("asia-southeast1")
                .setRandomJob()
                .build();
        try {
            for (FieldValueList row : BIGQUERY.query(queryConfig, id).iterateAll()) {
                for (FieldValue val : row) {
                    System.out.printf("%s,", val.toString());
                }
                System.out.printf("\n");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何将 aws 实例区域从美国东部(弗吉尼亚北部)us-east-1 更改为亚太地区(孟买)ap-south-1? - How can we change aws instances region from US East (N. Virginia) us-east-1 to Asia Pacific (Mumbai) ap-south-1? 从Amazon RDS读取varbinary数据时,SAS在数据末尾附加空格。 我们可以避免吗? - SAS while reading varbinary data from Amazon RDS is appending spaces at the end of the data. Can we avoid it? 使用 Python 从 AWS Lambda 中的 BigQuery 客户端提取 JSON 对象 - Extracting JSON object from BigQuery Client in AWS Lambda using Python 如何使用 terraform 将来自不同区域 (us-east1) 的 ACM 证书附加到另一个区域中的应用程序负载均衡器 - How to attach an ACM certificate from a different region (us-east1) to an application load balancer in another region using terraform AWS:无法从VPN ping到其他区域的实例 - AWS: Can't ping from VPN to instance in other region 如何在 AWS CDK 中引用其他区域的资源? - How can I reference a resource from other region in AWS CDK? 我怎样才能从其他地区获得ssm参数? - How can i get ssm parameter from other region? 为什么我们需要 distcp 命令将数据从 hdfs 复制到 s3,而我们可以直接将数据写入 s3 位置? - Why we need distcp command to copy data from hdfs to s3, when we can directly write the data to s3 location? 如何从 us-east-1 帐户访问在 us-west-2 区域创建的 S3 存储桶? - How to access S3 bucket created in us-west-2 region from us-east-1 account? 直接从Hadoop / HDFS(本地/内部群集)将数据加载到Redshift&Bigquery - load data to Redshift & Bigquery directly from Hadoop/HDFS (local/on premises cluster)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM