Develop Java Apps
AttentionThis page documents an earlier version. Go to the latest (v2.1)version.
Maven
To build your Java application using the YugabyteDB Cassandra driver, add the following Maven dependency to your application:
<dependency>
<groupId>com.yugabyte</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.2.0-yb-12</version>
</dependency>
Working Example
Pre-requisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the CQL shell. If not, please follow these steps in the quick start guide.
- installed JDK version 1.8+ and maven 3.3+
Creating the maven build file
Create a maven build file pom.xml
and add the following content into it.
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yugabyte.sample.apps</groupId>
<artifactId>hello-world</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.yugabyte</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.2.0-yb-12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Writing a HelloWorld CQL app
Create the appropriate directory structure as expected by maven.
$ mkdir -p src/main/java/com/yugabyte/sample/apps
Copy the following contents into the file src/main/java/com/yugabyte/sample/apps/YBCqlHelloWorld.java
.
package com.yugabyte.sample.apps;
import java.util.List;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
public class YBCqlHelloWorld {
public static void main(String[] args) {
try {
// Create a Cassandra client.
Cluster cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.build();
Session session = cluster.connect();
// Create keyspace 'ybdemo' if it does not exist.
String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS ybdemo;";
ResultSet createKeyspaceResult = session.execute(createKeyspace);
System.out.println("Created keyspace ybdemo");
// Create table 'employee' if it does not exist.
String createTable = "CREATE TABLE IF NOT EXISTS ybdemo.employee (id int PRIMARY KEY, " +
"name varchar, " +
"age int, " +
"language varchar);";
ResultSet createResult = session.execute(createTable);
System.out.println("Created table employee");
// Insert a row.
String insert = "INSERT INTO ybdemo.employee (id, name, age, language)" +
" VALUES (1, 'John', 35, 'Java');";
ResultSet insertResult = session.execute(insert);
System.out.println("Inserted data: " + insert);
// Query the row and print out the result.
String select = "SELECT name, age, language FROM ybdemo.employee WHERE id = 1;";
ResultSet selectResult = session.execute(select);
List<Row> rows = selectResult.all();
String name = rows.get(0).getString(0);
int age = rows.get(0).getInt(1);
String language = rows.get(0).getString(2);
System.out.println("Query returned " + rows.size() + " row: " +
"name=" + name + ", age=" + age + ", language: " + language);
// Close the client.
session.close();
cluster.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Building and running the app
To build the application, just run the following command.
$ mvn package
To run the program, do the following.
$ java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBCqlHelloWorld
You should see the following as the output.
Created keyspace ybdemo
Created table employee
Inserted data: INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'Java');
Query returned 1 row: name=John, age=35, language: Java
Maven
To build your Java application using YugabyteDB’s version of the Jedis driver, add the following Maven dependency to your application:
<dependency>
<groupId>com.yugabyte</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0-yb-11</version>
</dependency>
Working Example
Pre-requisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. If not, please follow these steps in the quick start guide.
- installed JDK version 1.8+ and maven 3.3+
Creating the maven build file
Create a maven build file pom.xml
and add the following content into it.
<?xml version="1.0"?>
<!-- Copyright (c) Yugabyte, Inc. -->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yugabyte.sample.apps</groupId>
<artifactId>hello-world</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.yugabyte</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0-yb-11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Writing a HelloWorld Redis app
Create the appropriate directory structure as expected by maven.
$ mkdir -p src/main/java/com/yugabyte/sample/apps
Copy the following contents into the file src/main/java/com/yugabyte/sample/apps/YBRedisHelloWorld.java
.
package com.yugabyte.sample.apps;
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class YBRedisHelloWorld {
public static void main(String[] args) {
try {
// Create a Jedis client.
Jedis jedisClient = new Jedis("127.0.0.1");
// Prepare the employee information to insert.
String userid = "1";
Map<String, String> userProfile = new HashMap<String, String>();
userProfile.put("name", "John");
userProfile.put("age", "35");
userProfile.put("language", "Redis");
// Insert the data.
String result = jedisClient.hmset(userid, userProfile);
System.out.println("HMSET returned " + result + ": id=1, name=John, age=35, language=Redis");
// Query the data.
Map<String, String> userData = jedisClient.hgetAll(userid);
System.out.println("Query result: name=" + userData.get("name") +
", age=" + userData.get("age") + ", language=" + userData.get("language"));
// Close the client.
jedisClient.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Building and running the app
To build the application, just run the following command.
$ mvn package
To run the program, do the following.
$ java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBRedisHelloWorld
You should see the following as the output.
HMSET returned OK: id=1, name=John, age=35, language=Redis
Query result: name=John, age=35, language=Redis