【问题标题】:The type com.google.cloud.ServiceOptions$Builder cannot be resolved. It is indirectly referenced from required .class filescom.google.cloud.ServiceOptions$Builder 类型无法解析。它是从所需的 .class 文件中间接引用的
【发布时间】:2019-12-12 11:26:35
【问题描述】:

这里我试图将 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 文件中间接引用的

======= 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

有什么解决方案,在此先感谢。

【问题讨论】:

    标签: java eclipse google-bigquery


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 感谢您的回复,现在编译时错误已解决,但是当我尝试运行 jar 文件时,线程“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) 原因:java.lang.ClassNotFoundException: com.google.cloud.bigquery .BigQueryOptions
    • 请看上面的 pom.xml 文件
    • 确保项目配置为 Maven 项目:右键单击项目并选择 配置 > 转换为 Maven 项目。您不必指定 google-cloud-core 依赖项,因为您可以通过 google-cloud-bigquery 依赖项获得此依赖项(Maven 知道 google-cloud-bigquery 需要什么)。
    • @BhageshArora 您能否确认给出的答案是否足以解决问题?谢谢!
    • Sergi Muznos,以下 pom.xml 对我有用,添加 google-cloud jar 后,我们需要修复使用
    【解决方案2】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 2013-05-14
      • 2017-02-26
      • 2016-10-31
      • 2016-02-20
      • 2015-02-10
      相关资源
      最近更新 更多