gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:05:17 25 4
gpt4 key购买 nike

我在这里尝试将 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")

错误通知是——

The type com.google.cloud.ServiceOptions$Builder cannot be resolved. It is indirectly referenced from required .class files

=======pom.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 之外,您还必须至少配置 google-cloud-core-1.90.0.jar在 Eclipse 中。

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

对于 Google Cloud BigQuery 1.93.0 所需的所有依赖项,它们本身可能需要额外的依赖项,请参阅 Maven Repository - Google Cloud BigQuery 1.93.0 中的编译依赖项部分.

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

关于java - com.google.cloud.ServiceOptions$Builder 类型无法解析。它是从所需的 .class 文件中间接引用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59303542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com