ScalarDB SQL API Guide
This guide describes how to use ScalarDB SQL API.
Add ScalarDB SQL API to your project
To add the dependencies on ScalarDB SQL API by using Gradle, use the following, replacing <VERSION>
with the versions of ScalarDB SQL API and the related library, respectively, that you are using:
dependencies {
implementation 'com.scalar-labs:scalardb-sql:<VERSION>'
implementation 'com.scalar-labs:scalardb-cluster-java-client-sdk:<VERSION>'
}
To add the dependencies by using Maven, use the following, replacing ...
with the version of ScalarDB SQL API that you are using:
<dependencies>
<dependency>
<groupId>com.scalar-labs</groupId>
<artifactId>scalardb-sql</artifactId>
<version>...</version>
</dependency>
<dependency>
<groupId>com.scalar-labs</groupId>
<artifactId>scalardb-cluster-java-client-sdk</artifactId>
<version>...</version>
</dependency>
</dependencies>
SqlSessionFactory
In ScalarDB SQL API, you execute all operations through a SqlSession
instance, which is instantiated with SqlSessionFactory
.
This section explains how to use them.
Before explaining SqlSessionFactory
, we start with the explanation for Connection mode and Transaction mode.
Transaction mode
Also, ScalarDB SQL offers two transaction modes: Transaction mode and Two-phase Commit Transaction mode.
Transaction mode exposes only commit
interface to users and runs two-phase commit behind the scene, while Two-phase Commit Transaction mode exposes two-phase commit style interfaces (prepare
and commit
) to users.
You can specify the default transaction mode in your configuration file or when you build SqlSessionFactory
.
And you also can change it with the setTransactionMode()
method of SqlSession
.
Build SqlSessionFactory
You can build SqlSessionFactory
with a properties file as follows:
SqlSessionFactory sqlSessionFactory = SqlSessionFactory.builder()
.withPropertiesFile("<your configuration file>")
// If you need to set custom properties, you can specify them with withProperty() or withProperties()
.withProperty("<custom property name>", "<custom property value>")
.build();
Please see ScalarDB Cluster SQL client configurations for the details of the configurations.
Get a SqlSession instance
You can get a SqlSession
instance with SqlSessionFactory
as follows:
SqlSession sqlSession = sqlSessionFactory.createSqlSession();
Note that SqlSession
is not thread-safe.
Please don't use it from multiple threads at the same time.
Close a SqlSession instance
Once all operations are done with a SqlSession
instance, you should close the SqlSession instance:
sqlSession.close();
Close a SqlSessionFactory instance
sqlSessionFactory
should also be closed once it's no longer needed:
sqlSessionFactory.close();
Execute SQLs
You can execute a SQL with SqlSession
as follows:
ResultSet resultSet = sqlSession.execute("<SQL>");
You can also execute a Statement
object with SqlSession
as follows:
// Build a statement
Statement statement = StatementBuilder.<factory method>...;
// Execute the statement
ResultSet resultSet = sqlSession.execute(statement);
Statement
objects can be built by StatementBuilder
that has factory methods for corresponding SQLs.
Please see the Javadoc of StatementBuilder
and ScalarDB SQL Grammar for more details.
Handle ResultSet objects
As the result of the SQL execution, SqlSession
returns a ResultSet
object.
Here, we describe how to handle ResultSet
objects.
If you want to get results one by one from the ResultSet
object, you can use the one()
method as follows:
Optional<Record> record = resultSet.one();
Or, if you want to get results all at once as a List
, you can use the all()
method as follows:
List<Record> records = resultSet.all();
Also, as ResultSet
implements Iterable
, you can use it in a for-each loop as follows:
for (Record record : resultSet) {
...
}
If you want to get the metadata of the ResultSet
object, you can use the getColumnDefinitions()
method as follows:
ColumnDefinitions columnDefinitions = resultSet.getColumnDefinitions();
Please see the Javadoc of ColumnDefinitions
for more details.