Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Import simba JDBC to gradle JAVA project

I am developing a JAVA application which will connect to BigQuery using JDBC and execute some SQL query to fetch data from tables. This project is in gradle. Can someone help me with documentation how can I import this JDBC dependency to my gradle JAVA project and create connection to BigQuery and execute query? I am very newbie in this field so I need some detailed answer.

0 3 790
3 REPLIES 3

 Ensure the BigQuery API is enabled for your project by navigating to "APIs & Services" -> "Dashboard" -> "Enable APIs and Services". Search for and enable the "BigQuery API".

1. Obtain Service Account Credentials

  • Create a Service Account: In your project's IAM & Admin settings, create a service account. Assign it the "BigQuery Data Viewer" role (or another suitable role) for read-only operations. For more flexibility, consider creating custom roles with only the necessary BigQuery permissions.
  • Download the Key: Download the JSON key file for your service account and store it securely.

2. Add JDBC Dependency in Gradle

  • Add Dependency: In your project's build.gradle file, add the Simba JDBC Driver for Google BigQuery:

 

dependencies { 
    implementation 'com.simba.googlebigquery.jdbc42.Driver:1.2.3' // Replace with the latest version 
} 
  • Sync Dependencies: After adding the driver, synchronize your Gradle dependencies by running gradle dependencies.

3. Java Code to Connect and Query BigQuery

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BigQueryConnection { 
    public static void main(String[] args) {
        String projectId = "your-project-id"; 
        String serviceAccountKeyPath = System.getenv("BIGQUERY_KEY_PATH");

        String query = "SELECT * FROM `your-project-id.your-dataset.your_table` LIMIT 10"; 

        try (Connection conn = DriverManager.getConnection(
                "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=" + projectId + 
                ";OAuthType=0;OAuthServiceAcctEmail=your-service-account-email;OAuthPvtKeyPath=" + serviceAccountKeyPath)) {

            ResultSet resultSet = conn.createStatement().executeQuery(query);

            while (resultSet.next()) { 
                System.out.println(resultSet.getString(1)); // Example processing
            } 
        } catch (SQLException e) {
            System.err.println("Error connecting to BigQuery: " + e.getMessage());
        }
    }
} 

Important Notes

  • Security: Never hard-code credentials in your code. Use environment variables or a secure secrets management solution.
  • Best Practices: Implement robust error handling and logging. Customize result processing to fit your application's logic.
  • Up-to-date Information: Refer to the official Google Cloud BigQuery (https://cloud.google.com/bigquery/docs/) and Simba JDBC driver documentation for the latest guidelines and recommendations.

Have you tried this code block. Because I can see that my gradle project is not able to find and download this dependency. 
Error : Could not find com.simba.googlebigquery.jdbc42.Driver:1.5.2:
version 1.5.2 has been released and this is release note for this. https://storage.googleapis.com/simba-bq-release/jdbc/release-notes_1.5.2.1005.txt

I can see there is one open issue that maven and gradle dependency for this is also not available. 
https://issuetracker.google.com/issues/180413368?pli=1 

Here is how to troubleshoot the issue:

1. Verify Version and Repository

  • Double-Check the Version: Confirm that you're using the correct version number (1.5.2) in your build.gradle file.
  • Check Simba's Repository: Simba might host their driver on a private or specialized repository. Consult Simba's official documentation or contact their support for instructions on how to include their repository in your Gradle project.

2. Alternative Approach: Manual Download and Addition

If the driver isn't found automatically, try this workaround:

  • Download from Simba: If available, download the version 1.5.2 JAR file directly from Simba's website or by contacting Simba support.
  • Add to Your Project:
    • Create a libs directory at the root of your project.
    • Place the downloaded JAR file in the libs directory.
  • Modify your build.gradle file:
repositories { 
    flatDir { 
        dirs 'libs' 
    } 
} 

dependencies { 
    implementation(name: 'simba-googlebigquery-jdbc42', version: '1.5.2', ext: 'jar') 
} 

Note: Match the name attribute in the dependency to the JAR filename (without version or extension), unless you specifically rename the file.

3. Investigate the Google Issue Tracker

  • The Issue: The open issue on the Google Issue Tracker (Issue #180413368) suggests potential availability problems.
  • Stay Updated: Monitor this issue for updates.
  • Contact Simba Support: For immediate assistance, reach out to Simba support directly.