我正在尝试将BigQuery Java API用作涉及Dataflow和其他项目的大型项目的一部分。但是,运行mvn compile exec:java -Dexec.mainClass=com.xpw.bq_test
时出现以下错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: com/google/cloud/HttpTransportOptions
at com.google.cloud.bigquery.BigQueryOptions.getDefaultHttpTransportOptions(BigQueryOptions.java:107)
at com.google.cloud.bigquery.BigQueryOptions$BigQueryDefaults.getDefaultTransportOptions(BigQueryOptions.java:102)
at com.google.cloud.ServiceOptions.<init>(ServiceOptions.java:255)
at com.google.cloud.bigquery.BigQueryOptions.<init>(BigQueryOptions.java:84)
at com.google.cloud.bigquery.BigQueryOptions.<init>(BigQueryOptions.java:31)
at com.google.cloud.bigquery.BigQueryOptions$Builder.build(BigQueryOptions.java:79)
at com.google.cloud.bigquery.BigQueryOptions.getDefaultInstance(BigQueryOptions.java:141)
at com.xpw.bq_test.main(bq_test.java:12)
... 6 more
Caused by: java.lang.ClassNotFoundException: com.google.cloud.HttpTransportOptions
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
bq_test.java
看起来像:
package com.xpw;
import com.google.cloud.bigquery.*;
import java.io.IOException;
public class bq_test {
public static void main(String[] arg) throws IOException {
// Instantiates a client
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
}
}
我的pom文件依赖项,以防冲突可能导致错误:
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.0.0-rc2</version>
</dependency>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-core</artifactId>
<version>${beam.version}</version>
</dependency>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-io-google-cloud-platform</artifactId>
<version>${beam.version}</version>
</dependency>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-runners-google-cloud-dataflow-java</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>0.11.0-beta</version>
</dependency>
</dependencies>
到目前为止,我已经尝试过:
mvn clean
并重建。 ~/.m2/repositories
并重建。 google-cloud-core
直接添加到我的pom。答案 0 :(得分:2)
之前我遇到过同样的问题。我做了一些调整,下面的代码片段为我工作。
TransportOptions options = HttpTransportOptions.newBuilder().setHttpTransportFactory(new HttpTransportOptions.DefaultHttpTransportFactory()).build();
GoogleCredentials credential = GoogleCredentials.getApplicationDefault();
BigQuery service = BigQueryOptions.newBuilder().setProjectId(YOUR_PROJECT_ID)
.setTransportOptions(options)
.setCredentials(credential)
.build()
.getService();
请为GoogleCredentials包含以下依赖项。
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.6.0</version>
<type>jar</type>
</dependency>
试试这个并告诉我。
注意:在使用这些库时始终要注意,请确保使用相同版本的所有库。这是安全和无错误的唯一方法。 Maven发生了很多事情,使用升级和降级依赖项来匹配。在您的情况下,组ID“com.google.cloud”在创建这些冲突的依赖项上有两个版本。使用0.11.0-beta进行谷歌云存储,它也应该解决所有未来的问题