Develop Java Apps

    To build your Java application using the YugabyteDB Cassandra driver, add the following Maven dependency to your application:

    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 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. </executions>
    38. </plugin>
    39. </plugins>
    40. </build>
    41. </project>

    Create the appropriate directory structure as expected by maven.

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

    Building and running the app

    To build the application, just run the following command.

    To run the program, do the following.

    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

    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>
    • installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. If not, please follow these steps in the .
    • 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.

    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

    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