Redis - Lab 2
This lab will introduce the first integration between Java and Redis using Eclipse JNoSQL.
Note
The connection properties are already defined in the src/main/java/resources/META-INF folder in the microprofile-config.properties.
The connection to Redis will be done using the database, port, and host defined there.
1. The first integration
Steps
- Open the
03-key-valueproject and navigate to thesrc/main/java - Create a class called
AppFirstIntegrationin theexpert.os.labs.persistencepackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Obtain an instance of the
BucketManagerFactory(specifically,RedisBucketManagerFactory) using Jakarta EE's dependency injection by adding the following inside the try-catch-resource block -
Use the
BucketManagerFactoryto create and interact with data structures in Redis. In this case, a Redis list named "names" and a Redis set named "fruits" are created and retrieved-
add the following code after the previous one:
-
-
Add data to the Redis list and set. In this example, names and fruits are added to the respective data structures
-
add the following code after the previous one:
-
-
Print the data stored in the "names" and "fruits" data structures:
-
Define a private constructor for the
Appclass to prevent instantiation since it contains only static methods: -
Run the
main()method
Expected results
-
The following output
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import org.eclipse.jnosql.communication.keyvalue.BucketManagerFactory;
import org.eclipse.jnosql.databases.redis.communication.RedisBucketManagerFactory;
import java.util.List;
import java.util.Set;
public class AppFirstIntegration {
private AppFirstIntegration() {
}
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
BucketManagerFactory factory = container.select(RedisBucketManagerFactory.class).get();
List<String> names = factory.getList("names", String.class);
Set<String> fruits = factory.getSet("fruits", String.class);
names.addAll(List.of("Otavio", "Elias", "Ada", "Poliana", "Otavio"));
fruits.addAll(List.of("Banana", "Banana", "Apple", "Watermelon", "Banana", "Apple"));
System.out.println("Names: ");
names.forEach(System.out::println);
System.out.println("Fruits: ");
fruits.forEach(System.out::println);
}
}
}
2. Redis and a Queue
This lab will introduce the integration with Redis and a Queue.
Steps
- Create a class called
AppQueuein theexpert.os.labs.persistencepackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Obtain an instance of the
BucketManagerFactory(specifically,RedisBucketManagerFactory) using Jakarta EE's dependency injection by adding the following inside the try-catch-resource block -
Use the
BucketManagerFactoryto create and interact with a Redis queue namedordersby using the methodgetQueue- the
Queueclass is from thejava.utilpackage - its type is
String -
the
getQueue()key is name"orders"and the type isString
- the
-
Clear the previous content using
orders.clear()and then add the following items in the queue using the methodadd(...)- "Phone"
- "Tablet"
- "Book"
-
Remove the element at the front of the queue using
orders.remove() - Peek at the element at the front of the queue using
orders.peek() -
Print the contents of the Redis queue after performing the below operations
-
Define a private constructor for the
Appclass to prevent instantiation since it contains only static methods: -
Run the
main()method
Expected results
-
The following output
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import org.eclipse.jnosql.communication.keyvalue.BucketManagerFactory;
import org.eclipse.jnosql.databases.redis.communication.RedisBucketManagerFactory;
import java.util.Queue;
public class AppQueue {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
BucketManagerFactory factory = container.select(RedisBucketManagerFactory.class).get();
Queue<String> orders = factory.getQueue("orders", String.class);
orders.clear();
orders.add("Phone");
orders.add("Tablet");
orders.add("Book");
orders.remove();
orders.peek();
orders.forEach(System.out::println);
}
}
}
3. Redis and a Map
Steps
- Create a class called
AppQueuein theexpert.os.labs.persistencepackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Obtain an instance of the
BucketManagerFactory(specifically,RedisBucketManagerFactory) using Jakarta EE's dependency injection by adding the following inside the try-catch-resource block -
Use the
BucketManagerFactoryto create and interact with a Redis map namedbasketby using the methodgetBasket- the
Mapmust have the types asIntegerandString - the
getMap()the key is named"basket"and the types are IntegerandString`
- the
-
Clear the previous content using
orders.clear()and then add the following items on the basket using the methodput(...)key value 1 "Banana" 2 "Watermelon" 3 "BaApplenana" -
Print the contents of the Redis queue after performing the below operations
-
Define a private constructor for the
Appclass to prevent instantiation since it contains only static methods: -
Run the
main()method
Expected results
-
The following output
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import org.eclipse.jnosql.communication.keyvalue.BucketManagerFactory;
import org.eclipse.jnosql.databases.redis.communication.RedisBucketManagerFactory;
import java.util.Map;
public class AppMap {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
BucketManagerFactory factory = container.select(RedisBucketManagerFactory.class).get();
Map<Integer, String> basket = factory.getMap("basket", Integer.class, String.class);
basket.clear();
basket.put(1, "Banana");
basket.put(2, "Watermelon");
basket.put(4, "Apple");
basket.forEach((k, v) -> System.out.println(k + " - " + v));
}
}
private AppMap() {
}
}
4. Redis and a SortedMap
Steps
- Create a class called
AppSortedMapin theexpert.os.labs.persistencepackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Obtain an instance of the
BucketManagerFactory(specifically,RedisBucketManagerFactory) using Jakarta EE's dependency injection by adding the following inside the try-catch-resource block -
Use the
BucketManagerFactoryto create and interact with a Redis sorted map namedgameby using the methodgetSortedSet- the
SortedSetis the field class
- the
-
Add the following itens in the game using the method
add(...)key value "Otavio" 10 "Elias" 20 "Ada 30 -
Add one more item to the game, but using the
Ranking.of()fromorg.eclipse.jnosql.databases.redis.communicationpackage, adding:key value "Poliana" 40 -
Get the ranking, add it on a list
-
Print out the ranking
-
Print out the ranking in a reverse order
-
Run the
main()method
Expected results
-
The following output
Ranking: [DefaultRanking{point=10.0, member='Otavio'}, DefaultRanking{point=20.0, member='Elias'}, DefaultRanking{point=30.0, member='Ada'}, DefaultRanking{point=40.0, member='Poliana'}] The reverse ranking: [DefaultRanking{point=40.0, member='Poliana'}, DefaultRanking{point=30.0, member='Ada'}, DefaultRanking{point=20.0, member='Elias'}, DefaultRanking{point=10.0, member='Otavio'}]
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import org.eclipse.jnosql.databases.redis.communication.Ranking;
import org.eclipse.jnosql.databases.redis.communication.RedisBucketManagerFactory;
import org.eclipse.jnosql.databases.redis.communication.SortedSet;
import java.util.List;
public class AppSortedMap {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
RedisBucketManagerFactory factory = container.select(RedisBucketManagerFactory.class).get();
SortedSet game = factory.getSortedSet("game");
game.add("Otavio", 10);
game.add("Elias", 20);
game.add("Ada", 30);
game.add(Ranking.of("Poliana", 40));
List<Ranking> ranking = game.getRanking();
System.out.println("Ranking: " + ranking);
System.out.println("The reverse ranking: " + game.getRevRanking());
}
}
private AppSortedMap() {
}
}
5. Redis and Counter
Steps
- Create a class called
AppSortedMapin theexpert.os.labs.persistencepackage -
Add a main method
-
Set up a try-with-resources block, inside the
mainmethod, to manage the Jakarta EESeContainerthat is responsible for dependency injection and managing resources -
Obtain an instance of the
BucketManagerFactory(specifically,RedisBucketManagerFactory) using Jakarta EE's dependency injection by adding the following inside the try-catch-resource block -
Use the
RedisBucketManagerFactoryto create and interact with Redis counters named "home" and "products" using thecounter()method -
Increment the
homeand theproductsusing the methodincrement() -
Increment in 3 the
products -
Print out the
homeandproductsvalues -
Run the
main()method
Expected results
-
The following output
Solution
Click to see...
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import org.eclipse.jnosql.databases.redis.communication.Counter;
import org.eclipse.jnosql.databases.redis.communication.RedisBucketManagerFactory;
public class AppCounter {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
RedisBucketManagerFactory factory = container.select(RedisBucketManagerFactory.class).get();
Counter home = factory.getCounter("home");
Counter products = factory.getCounter("products");
home.increment();
products.increment();
products.increment(3L);
System.out.println("Home: " + home.get());
System.out.println("Products: " + products.get());
}
}
private AppCounter() {
}
}