Develop Java Apps

    Working example

    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+
    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-16</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. <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>

    Create the appropriate directory structure as expected by maven.

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

    To build the application, just run the following command.

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

    You should see the following as the output.