简体   繁体   English

BigQuery 与 Java SpringBoot 的 JDBC 连接问题

[英]Problems with JDBC connection for BigQuery with Java SpringBoot

I am trying to add the BigQuery JDBC driver to my Spring Boot application but I am having some problems.我正在尝试将 BigQuery JDBC 驱动程序添加到我的 Spring Boot 应用程序,但我遇到了一些问题。 I have downloaded the driver from the Google Cloud download page and added the dependency in my pom.xml file, but when I try to connect to BigQuery I get a "No suitable driver found" exception.我已经从 Google Cloud 下载页面下载了驱动程序,并在我的 pom.xml 文件中添加了依赖项,但是当我尝试连接到 BigQuery 时,出现“找不到合适的驱动程序”异常。

java.sql.SQLException: No suitable driver found for jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;...... at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702) ~[java.sql:na] at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251) ~[java.sql:na] java.sql.SQLException:找不到适合 jdbc 的驱动程序:bigquery://https://www.googleapis.com/bigquery/v2:443;......在 java.sql/java.sql.DriverManager。 getConnection(DriverManager.java:702) ~[java.sql:na] 在 java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251) ~[java.sql:na]

I have added the drive as follows in pom.xml:我在 pom.xml 中添加了如下驱动器:

 <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-bigquery-jdbc</artifactId> <version>1.3.0</version> <scope>system</scope> <systemPath>... resources/drivers/bigquer/SimbaJDBCDriverforGoogleBigQuery42_1.3.0.1001_JB.zip</systemPath> </dependency>

I'm trying to get the connection in this ways:我试图通过这种方式获得连接:

 private static Connection connectViaDM() throws Exception { Connection connection = null; connection = DriverManager.getConnection(CONNECTION_URL); return connection; }
private Connection connectViaDS() throws Exception
{
    Connection connection = null;
    DataSource ds = new com.simba.googlebigquery.jdbc42.DataSource();
    ds.setURL(CONNECTION_URL);
    ....
    connection = ds.getConnection();
    return connection;
}

The bigquery access data that I handle is correct because I am able to connect to the java client:我处理的 bigquery 访问数据是正确的,因为我能够连接到 java 客户端:

 BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId(PROJECT_ID).setCredentials( ServiceAccountCredentials.fromStream( new FileInputStream(KEY_FILE)) ).build().getService(); QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); TableResult results = bigquery.query(queryConfig);

Instead of using the SimbaJdbcDriver for Bigquery , you can use a starter to use directly Bigquery client with Spring Boot app:您可以使用启动器直接Bigquery客户端与Spring Boot应用程序一起使用,而不是将SimbaJdbcDriver用于Bigquery

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-bigquery</artifactId>
</dependency>
@Autowired
BigQuery bigquery;

// Execute a query
public void runQuery() throws InterruptedException {
  String query = "SELECT column FROM table;";
  QueryJobConfiguration queryConfig =
      QueryJobConfiguration.newBuilder(query).build();

  for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) {
    for (FieldValue val : row) {
      System.out.println(val);
    }
  }
}

Use BigQueryTemplate :使用BigQueryTemplate

@Autowired
BigQueryTemplate bigQueryTemplate;

public void loadData(InputStream dataInputStream, String tableName) {
  ListenableFuture<Job> bigQueryJobFuture =
      bigQueryTemplate.writeDataToTable(
          tableName,
          dataFile.getInputStream(),
          FormatOptions.csv());

  // After the future is complete, the data is successfully loaded.
  Job job = bigQueryJobFuture.get();
}

The following link gives more information.以下链接提供了更多信息。

In Java (Spring Boot or not), you can also directly use the BigQuery clientJava中(无论是否为 Spring Boot),您也可以直接使用BigQuery 客户端

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM