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:

  1. <dependency>
  2. <groupId>com.yugabyte</groupId>
  3. <artifactId>cassandra-driver-core</artifactId>
  4. <version>3.2.0-yb-12</version>
  5. </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.

  1. <?xml version="1.0"?>
  2. <project
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  4. xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.yugabyte.sample.apps</groupId>
  8. <artifactId>hello-world</artifactId>
  9. <version>1.0</version>
  10. <packaging>jar</packaging>
  11. <dependencies>
  12. <dependency>
  13. <groupId>com.yugabyte</groupId>
  14. <artifactId>cassandra-driver-core</artifactId>
  15. <version>3.2.0-yb-12</version>
  16. </dependency>
  17. </dependencies>
  18. <build>
  19. <plugins>
  20. <plugin>
  21. <groupId>org.apache.maven.plugins</groupId>
  22. <artifactId>maven-dependency-plugin</artifactId>
  23. <version>2.1</version>
  24. <executions>
  25. <execution>
  26. <id>copy-dependencies</id>
  27. <phase>prepare-package</phase>
  28. <goals>
  29. <goal>copy-dependencies</goal>
  30. </goals>
  31. <configuration>
  32. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  33. <overWriteReleases>true</overWriteReleases>
  34. <overWriteSnapshots>true</overWriteSnapshots>
  35. <overWriteIfNewer>true</overWriteIfNewer>
  36. </configuration>
  37. </execution>
  38. </executions>
  39. </plugin>
  40. </plugins>
  41. </build>
  42. </project>

Writing a HelloWorld CQL app

Create the appropriate directory structure as expected by maven.

  1. $ 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.

  1. package com.yugabyte.sample.apps;
  2. import java.util.List;
  3. import com.datastax.driver.core.Cluster;
  4. import com.datastax.driver.core.ResultSet;
  5. import com.datastax.driver.core.Row;
  6. import com.datastax.driver.core.Session;
  7. public class YBCqlHelloWorld {
  8. public static void main(String[] args) {
  9. try {
  10. // Create a Cassandra client.
  11. Cluster cluster = Cluster.builder()
  12. .addContactPoint("127.0.0.1")
  13. .build();
  14. Session session = cluster.connect();
  15. // Create keyspace 'ybdemo' if it does not exist.
  16. String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS ybdemo;";
  17. ResultSet createKeyspaceResult = session.execute(createKeyspace);
  18. System.out.println("Created keyspace ybdemo");
  19. // Create table 'employee' if it does not exist.
  20. String createTable = "CREATE TABLE IF NOT EXISTS ybdemo.employee (id int PRIMARY KEY, " +
  21. "name varchar, " +
  22. "age int, " +
  23. "language varchar);";
  24. ResultSet createResult = session.execute(createTable);
  25. System.out.println("Created table employee");
  26. // Insert a row.
  27. String insert = "INSERT INTO ybdemo.employee (id, name, age, language)" +
  28. " VALUES (1, 'John', 35, 'Java');";
  29. ResultSet insertResult = session.execute(insert);
  30. System.out.println("Inserted data: " + insert);
  31. // Query the row and print out the result.
  32. String select = "SELECT name, age, language FROM ybdemo.employee WHERE id = 1;";
  33. ResultSet selectResult = session.execute(select);
  34. List<Row> rows = selectResult.all();
  35. String name = rows.get(0).getString(0);
  36. int age = rows.get(0).getInt(1);
  37. String language = rows.get(0).getString(2);
  38. System.out.println("Query returned " + rows.size() + " row: " +
  39. "name=" + name + ", age=" + age + ", language: " + language);
  40. // Close the client.
  41. session.close();
  42. cluster.close();
  43. } catch (Exception e) {
  44. System.err.println("Error: " + e.getMessage());
  45. }
  46. }
  47. }

Building and running the app

To build the application, just run the following command.

  1. $ mvn package

To run the program, do the following.

  1. $ java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBCqlHelloWorld

You should see the following as the output.

  1. Created keyspace ybdemo
  2. Created table employee
  3. Inserted data: INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'Java');
  4. 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:

  1. <dependency>
  2. <groupId>com.yugabyte</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.0-yb-11</version>
  5. </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.

  1. <?xml version="1.0"?>
  2. <!-- Copyright (c) Yugabyte, Inc. -->
  3. <project
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  5. xmlns="http://maven.apache.org/POM/4.0.0"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  7. <modelVersion>4.0.0</modelVersion>
  8. <groupId>com.yugabyte.sample.apps</groupId>
  9. <artifactId>hello-world</artifactId>
  10. <version>1.0</version>
  11. <packaging>jar</packaging>
  12. <dependencies>
  13. <dependency>
  14. <groupId>com.yugabyte</groupId>
  15. <artifactId>jedis</artifactId>
  16. <version>2.9.0-yb-11</version>
  17. </dependency>
  18. </dependencies>
  19. <build>
  20. <plugins>
  21. <plugin>
  22. <groupId>org.apache.maven.plugins</groupId>
  23. <artifactId>maven-dependency-plugin</artifactId>
  24. <version>2.1</version>
  25. <executions>
  26. <execution>
  27. <id>copy-dependencies</id>
  28. <phase>prepare-package</phase>
  29. <goals>
  30. <goal>copy-dependencies</goal>
  31. </goals>
  32. <configuration>
  33. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  34. <overWriteReleases>true</overWriteReleases>
  35. <overWriteSnapshots>true</overWriteSnapshots>
  36. <overWriteIfNewer>true</overWriteIfNewer>
  37. </configuration>
  38. </execution>
  39. </executions>
  40. </plugin>
  41. </plugins>
  42. </build>
  43. </project>

Writing a HelloWorld Redis app

Create the appropriate directory structure as expected by maven.

  1. $ 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.

  1. package com.yugabyte.sample.apps;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import redis.clients.jedis.Jedis;
  5. public class YBRedisHelloWorld {
  6. public static void main(String[] args) {
  7. try {
  8. // Create a Jedis client.
  9. Jedis jedisClient = new Jedis("127.0.0.1");
  10. // Prepare the employee information to insert.
  11. String userid = "1";
  12. Map<String, String> userProfile = new HashMap<String, String>();
  13. userProfile.put("name", "John");
  14. userProfile.put("age", "35");
  15. userProfile.put("language", "Redis");
  16. // Insert the data.
  17. String result = jedisClient.hmset(userid, userProfile);
  18. System.out.println("HMSET returned " + result + ": id=1, name=John, age=35, language=Redis");
  19. // Query the data.
  20. Map<String, String> userData = jedisClient.hgetAll(userid);
  21. System.out.println("Query result: name=" + userData.get("name") +
  22. ", age=" + userData.get("age") + ", language=" + userData.get("language"));
  23. // Close the client.
  24. jedisClient.close();
  25. } catch (Exception e) {
  26. System.err.println("Error: " + e.getMessage());
  27. }
  28. }
  29. }

Building and running the app

To build the application, just run the following command.

  1. $ mvn package

To run the program, do the following.

  1. $ java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBRedisHelloWorld

You should see the following as the output.

  1. HMSET returned OK: id=1, name=John, age=35, language=Redis
  2. Query result: name=John, age=35, language=Redis