无法解析 com.google.cloud.ServiceOptions$Builder 类型.它从所需的.class文



在这里,我正在尝试将CSV数据插入bigQuery表中,我已经在eclipse中配置了google-cloud-bigquery-1.93.0.jar文件。但是我收到"com.google.cloud.ServiceOptions$Builder"的错误。请参阅我正在使用的代码。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.google.cloud.bigquery.InsertAllResponse;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.InsertAllRequest;
import com.google.cloud.bigquery.InsertAllResponse;
import com.google.cloud.bigquery.TableId;
public class InsertData {
public static void uploaddata(String datasetname) throws IOException {
BigQuery bigquery = BigQueryOptions
.getDefaultInstance()
.toBuilder()
.setProjectId("my-project-id")
.build()
.getService();
TableId tableIdor = TableId.of(datasetname, "table_name");
String csvFile = "D:/my-file/my.csv";
BufferedReader br = null;
FileReader myFile = null;
String line = "";
String cvsSplitBy = ",";
String[] beschriftung = null;
int i = 0;
Map<String, Object> rowContent = new HashMap<>();
myFile = new FileReader(csvFile);
br = new BufferedReader(myFile);
// read CSV file line by line and upload it into BigQuery
while ((line = br.readLine()) != null) {
// get the name of the fields from the first row of the CSV File
if (i == 0) {
beschriftung = line.split(cvsSplitBy);
i = i + 1;
for (int e = 0; e < beschriftung.length; e++) {
rowContent.put(beschriftung[e], "init");
}
} else
// Format Data for BigQuery and upload the row
{
String[] Zeile = line.split(cvsSplitBy);
for (int e = 0; e < Zeile.length; e++) {
rowContent.put(beschriftung[e], Zeile[e]);
}
i = i + 1;
}
InsertAllResponse response = bigquery
.insertAll(InsertAllRequest
.newBuilder(tableIdor)
.addRow(String.valueOf(i), rowContent)
.build());
if (response.hasErrors()) {
System.out.println(response.getErrorsFor(0));
}
}
br.close();
myFile.close();
}

}

我在以下行中遇到语法错误。

BigQuery bigquery = BigQueryOptions
.getDefaultInstance()
.toBuilder()
.setProjectId("my-project-id")

错误通知是 -

无法解析 com.google.cloud.ServiceOptions$Builder 类型。它从所需的.class文件中间接引用

======= 聚甲醛.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>insertdata</groupId>
<artifactId>insertdata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>insertdata</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>1.93.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<version>1.90.0</version>
</dependency>
</dependencies>
</project>

现在我得到以下错误 -

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/cloud/bigquery/BigQueryOptions
at insertdata.insertdata.InsertDataBigQuery.uploaddata(InsertDataBigQuery.java:24)
at insertdata.insertdata.InsertDataBigQuery.main(InsertDataBigQuery.java:78)
Caused by: java.lang.ClassNotFoundException: 
com.google.cloud.bigquery.BigQueryOptions
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more

解决方案是什么,提前谢谢。

除了google-cloud-bigquery-1.93.0.jar之外,您还必须在 Eclipse 中配置至少google-cloud-core-1.90.0.jar

BigQueryOptions.getDefaultInstance().toBuilder()返回扩展缺失ServiceOptions.BuilderBigQueryOptions.Builder。这就是您收到错误的原因。

对于所有 Google Cloud BigQuery 1.93.0所需的依赖项(这些依赖项本身可能需要其他依赖项(,请参阅在 Maven 存储库中编译依赖项-Google Cloud BigQuery 1.93.0部分。

或者,您可以使用像Maven或Gradle这样的构建工具,它将自动下载所有必要的依赖项并在Eclipse中配置它们。

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>insertdata</groupId>
<artifactId>insertdata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>insertdata</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>1.93.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<version>1.90.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>                
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

相关内容

最新更新