premdeep sharma
premdeep sharma

Reputation: 1

Connecting to BigQuery via jdbc driver

Background: I am trying to run a spring-batch job where the reader needs to read from "BigQuery". I need this reader to be "restartable". The existing solution https://github.com/spring-projects/spring-batch-extensions/blob/main/spring-batch-bigquery/src/main/java/org/springframework/batch/extensions/bigquery/reader/BigQueryQueryItemReader.java is not restartable.

So, i turned to using https://docs.spring.io/spring-batch/reference/readers-and-writers/database.html#cursorBasedItemReaders JdbcCursor Based Readers. But for these i need a "Datasource" for bigQuery to connect to.

Upon searching i found a few ways to make jdbc connection to BigQuery.

  1. https://www.cdata.com/kb/tech/bigquery-jdbc-spring-boot.rst
  2. Any JDBC Driver for Google BigQuery Standard SQL

But the following method gives me this error

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1' defined in class path resource [com/google/gitsolution/snapshotservice/SnapshotJobConfiguration.class]: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'step1' threw exception with message: Failed to load driver class cdata.jdbc.googlebigquery.GoogleBigQueryDriver in either of HikariConfig class loader or Thread context classloader
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-6.1.3.jar:6.1.3]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:639) ~[spring-beans-6.1.3.jar:6.1.3]

@Configuration
public class BigQueryJdbcCursorItemReader {

    public static DataSource getDataSource() {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("cdata.jdbc.googlebigquery.GoogleBigQueryDriver");
        dataSourceBuilder.url(connectionUrl);
        return dataSourceBuilder.build();
    }

    private final static String connectionUrl = "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=myproject;OAuthType=1";
    private final String ConnectionUrlCdata = "jdbc:googlebigquery:DataSetId=myproject.myproject_depsdev_sample;ProjectId=myproject;InitiateOAuth=GETANDREFRESH";
    public static JdbcCursorItemReader<Package> bigQueryjdbcCursorReader() {
        return new JdbcCursorItemReaderBuilder<Package>()
                .dataSource(getDataSource())
                .name("bigQueryjdbcCursorReader")
                .sql("SELECT p.System, p.Name, p.Version FROM myproject.myproject_depsdev_sample.allpackage_recent_5000 p LIMIT 55")
                .rowMapper(new PackageResultSetRowMapper()).build();
    }
}

Question

  1. How to solve for this error and get a working Datasource for BigQuery
  2. Is there any other way to get BigQuery DataSource

Note: I know that i can use the "Restartable" ItemStream on the BigQueryReader i am trying that as well in parallel.

Thanks!

Thanks!

I was expecting the BigQuery Cursor based reader to work.

I looked at Spring boot BigQuery datasource connection But that also did not solve my problem

Upvotes: 0

Views: 104

Answers (0)

Related Questions