Ready to get started?

Download a free trial of the Google Cloud Storage Driver to get started:

 Download Now

Learn more:

Google Cloud Storage Icon Google Cloud Storage JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Google Cloud Storage.

Create a Data Access Object for Google Cloud Storage Data using JDBI



A brief overview of creating a SQL Object API for Google Cloud Storage data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for Google Cloud Storage integrates connectivity to live Google Cloud Storage data in Java applications. By pairing these technologies, you gain simple, programmatic access to Google Cloud Storage data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read Google Cloud Storage data.

Create a DAO for the Google Cloud Storage Buckets Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyBucketsDAO { //request specific data from Google Cloud Storage (String type is used for simplicity) @SqlQuery("SELECT OwnerId FROM Buckets WHERE Name = :name") String findOwnerIdByName(@Bind("name") String name); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Google Cloud Storage

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Google Cloud Storage.

Authenticate with a User Account

You can connect without setting any connection properties for your user credentials. After setting InitiateOAuth to GETANDREFRESH, you are ready to connect.

When you connect, the Google Cloud Storage OAuth endpoint opens in your default browser. Log in and grant permissions, then the OAuth process completes

Authenticate with a Service Account

Service accounts have silent authentication, without user authentication in the browser. You can also use a service account to delegate enterprise-wide access scopes.

You need to create an OAuth application in this flow. See the Help documentation for more information. After setting the following connection properties, you are ready to connect:

  • InitiateOAuth: Set this to GETANDREFRESH.
  • OAuthJWTCertType: Set this to "PFXFILE".
  • OAuthJWTCert: Set this to the path to the .p12 file you generated.
  • OAuthJWTCertPassword: Set this to the password of the .p12 file.
  • OAuthJWTCertSubject: Set this to "*" to pick the first certificate in the certificate store.
  • OAuthJWTIssuer: In the service accounts section, click Manage Service Accounts and set this field to the email address displayed in the service account Id field.
  • OAuthJWTSubject: Set this to your enterprise Id if your subject type is set to "enterprise" or your app user Id if your subject type is set to "user".
  • ProjectId: Set this to the Id of the project you want to connect to.

The OAuth flow for a service account then completes.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Google Cloud Storage JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.googlecloudstorage.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for Google Cloud Storage will typically look like the following:

jdbc:googlecloudstorage:ProjectId='project1';InitiateOAuth=GETANDREFRESH

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:googlecloudstorage:ProjectId='project1';InitiateOAuth=GETANDREFRESH"); MyBucketsDAO dao = dbi.open(MyBucketsDAO.class); //do stuff with the DAO dao.close();

Read Google Cloud Storage Data

With the connection open to Google Cloud Storage, simply call the previously defined method to retrieve data from the Buckets entity in Google Cloud Storage.

//disply the result of our 'find' method String ownerId = dao.findOwnerIdByName("TestBucket"); System.out.println(ownerId);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Google Cloud Storage by integrating with the CData JDBC Driver for Google Cloud Storage. Download a free trial and work with live Google Cloud Storage data in custom Java applications today.