![]() |
VOOZH | about |
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 explains how to build a basic Data Access Object (DAO) and the accompanying code to read Google Cloud Storage data.
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();
}
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Google Cloud Storage.
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
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:
The OAuth flow for a service account then completes.
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.
👁 Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)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();
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.
Download a free trial of the Google Cloud Storage Driver to get started:
Download NowLearn more:
👁 Google Cloud Storage IconRapidly create and deploy powerful Java applications that integrate with Google Cloud Storage.