BAEL-6210: Examples for RethinkDB article (#13481)

This commit is contained in:
Graham Cox 2023-02-15 07:16:43 +00:00 committed by GitHub
parent e173c125f0
commit 7cacb73389
7 changed files with 451 additions and 0 deletions

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>rethinkdb</artifactId>
<name>rethinkdb</name>
<description>Code snippets for RethinkDB articles</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rethinkdb</groupId>
<artifactId>rethinkdb-driver</artifactId>
<version>2.4.4</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<java.version>17</java.version>
</properties>
</project>

View File

@ -0,0 +1,62 @@
package com.baeldung.rethinkdb;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static com.rethinkdb.RethinkDB.r;
/**
* Some tests demonstrating inserting data.
*/
public class InsertIntegrationTest extends TestBase {
/**
* Create a table for the tests.
*/
@BeforeEach
public void createTable() {
r.db(DB_NAME).tableCreate(tableName).run(conn);
}
/**
* Insert a single simple record into the database.
*/
@Test
public void insertSimpleRecord() {
r.db(DB_NAME).table(tableName)
.insert(
r.hashMap()
.with("name", "Baeldung")
)
.run(conn);
}
@Test
public void insertMap() {
r.db(DB_NAME).table(tableName)
.insert(
Map.of("name", "Baeldung")
)
.run(conn);
}
@Test
public void insertComplex() {
r.db(DB_NAME).table(tableName)
.insert(
r.hashMap()
.with("name", "Baeldung")
.with("articles", r.array(
r.hashMap()
.with("name", "String Interpolation in Java")
.with("url", "https://www.baeldung.com/java-string-interpolation"),
r.hashMap()
.with("name", "Access HTTPS REST Service Using Spring RestTemplate")
.with("url", "https://www.baeldung.com/spring-resttemplate-secure-https-service")
)
)
)
.run(conn);
}
}

View File

@ -0,0 +1,81 @@
package com.baeldung.rethinkdb;
import com.rethinkdb.net.Result;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static com.rethinkdb.RethinkDB.r;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Some tests demonstrating querying data.
*/
public class QueryIntegrationTest extends TestBase {
/**
* Create a table for the tests.
*/
@BeforeEach
public void createTable() {
r.db(DB_NAME).tableCreate(tableName).run(conn);
r.db(DB_NAME).table(tableName)
.insert(
r.hashMap()
.with("id", "article1")
.with("name", "String Interpolation in Java")
.with("url", "https://www.baeldung.com/java-string-interpolation")
).run(conn);
r.db(DB_NAME).table(tableName)
.insert(
r.hashMap()
.with("id", "article2")
.with("name", "Access HTTPS REST Service Using Spring RestTemplate")
.with("url", "https://www.baeldung.com/spring-resttemplate-secure-https-service")
).run(conn);
}
@Test
public void listAll() {
Result<Map> results = r.db(DB_NAME).table(tableName).run(conn, Map.class);
// We can't ensure the order the results come back in.
Set<String> expected = new HashSet<>(Set.of(
"String Interpolation in Java",
"Access HTTPS REST Service Using Spring RestTemplate"
));
for (Map result : results) {
assertTrue(expected.remove(result.get("name")));
}
assertTrue(expected.isEmpty());
}
@Test
public void listSome() {
Result<Map> results = r.db(DB_NAME)
.table(tableName)
.filter(r -> r.g("name").eq("String Interpolation in Java"))
.run(conn, Map.class);
// We can't ensure the order the results come back in.
Set<String> expected = Set.of("https://www.baeldung.com/java-string-interpolation");
assertEquals(expected, results.stream()
.map(r -> r.get("url"))
.collect(Collectors.toSet()));
}
@Test
public void getByKey() {
Result<Map> results = r.db(DB_NAME).table(tableName).get("article1").run(conn, Map.class);
assertEquals("String Interpolation in Java", results.first().get("name"));
}
}

View File

@ -0,0 +1,117 @@
package com.baeldung.rethinkdb;
import com.rethinkdb.net.Result;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static com.rethinkdb.RethinkDB.r;
/**
* Some tests demonstrating streaming live changes to data.
*/
public class StreamingIntegrationTest extends TestBase {
@Test
public void getLiveInserts() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
r.db(DB_NAME).tableCreate(tableName).run(conn);
r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", 0)).run(conn);
executorService.submit(() -> {
Result<Map> cursor = r.db(DB_NAME).table(tableName).changes().run(conn, Map.class);
cursor.stream().forEach(record -> System.out.println("Record: " + record));
});
for (int i = 0; i < 10; ++i) {
r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", i)).run(conn);
TimeUnit.MILLISECONDS.sleep(100);
}
executorService.shutdownNow();
}
@Test
public void getSomeLiveInserts() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
r.db(DB_NAME).tableCreate(tableName).run(conn);
r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", 0)).run(conn);
executorService.submit(() -> {
Result<Map> cursor = r.db(DB_NAME).table(tableName)
.filter(r -> r.g("index").eq(5))
.changes()
.run(conn, Map.class);
cursor.stream().forEach(record -> System.out.println("Record: " + record));
});
for (int i = 0; i < 10; ++i) {
r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", i)).run(conn);
TimeUnit.MILLISECONDS.sleep(100);
}
executorService.shutdownNow();
}
@Test
public void getLiveUpdates() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
r.db(DB_NAME).tableCreate(tableName).run(conn);
r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", 0)).run(conn);
executorService.submit(() -> {
Result<Map> cursor = r.db(DB_NAME).table(tableName).changes().run(conn, Map.class);
cursor.stream().forEach(record -> System.out.println("Record: " + record));
});
for (int i = 0; i < 10; ++i) {
r.db(DB_NAME).table(tableName).update(r.hashMap().with("index", i)).run(conn);
TimeUnit.MILLISECONDS.sleep(100);
}
executorService.shutdownNow();
}
@Test
public void getLiveDeletes() throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
r.db(DB_NAME).tableCreate(tableName).run(conn);
for (int i = 0; i < 10; ++i) {
r.db(DB_NAME).table(tableName).insert(r.hashMap().with("index", i)).run(conn);
}
executorService.submit(() -> {
Result<Map> cursor = r.db(DB_NAME).table(tableName).changes().run(conn, Map.class);
cursor.stream().forEach(record -> System.out.println("Record: " + record));
});
r.db(DB_NAME).table(tableName)
.filter(r -> r.g("index").eq(1))
.delete()
.run(conn);
r.db(DB_NAME).table(tableName)
.filter(r -> r.g("index").eq(3))
.delete()
.run(conn);
r.db(DB_NAME).table(tableName)
.filter(r -> r.g("index").eq(5))
.delete()
.run(conn);
executorService.shutdownNow();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.rethinkdb;
import com.rethinkdb.gen.exc.ReqlOpFailedError;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import static com.rethinkdb.RethinkDB.r;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Some tests demonstrating working with tables.
*/
public class TablesIntegrationTest extends TestBase {
@Test
public void createTable() {
r.db(DB_NAME).tableCreate(tableName).run(conn);
}
@Test
public void createTableTwice() {
r.db(DB_NAME).tableCreate(tableName).run(conn);
Assertions.assertThrows(ReqlOpFailedError.class, () -> {
r.db(DB_NAME).tableCreate(tableName).run(conn);
});
}
@Test
public void listTables() {
r.db(DB_NAME).tableCreate(tableName).run(conn);
List<String> tables = r.db(DB_NAME).tableList().run(conn, List.class).first();
assertTrue(tables.contains(tableName));
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.rethinkdb;
import com.rethinkdb.net.Connection;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import java.util.UUID;
import static com.rethinkdb.RethinkDB.r;
/**
* Base class for RethinkDB tests.
*/
public class TestBase {
/** The database name to work with */
protected static final String DB_NAME = "test";
/** A randomly generated table name so they never collide */
protected final String tableName = UUID.randomUUID().toString().replaceAll("-","");
/** A database connection */
protected Connection conn;
/**
* Connect to the database for each test
*/
@BeforeEach
public void connect() {
conn = r.connection()
.hostname("localhost")
.port(28015)
.connect();
}
/**
* Disconnect from the database after each test
*/
@AfterEach
public void disconnect() {
if (this.conn != null) {
conn.close();
}
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.rethinkdb;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static com.rethinkdb.RethinkDB.r;
/**
* Some tests demonstrating updating data.
*/
public class UpdateIntegrationTest extends TestBase {
/**
* Create a table for the tests.
*/
@BeforeEach
public void createTable() {
r.db(DB_NAME).tableCreate(tableName).run(conn);
r.db(DB_NAME).table(tableName)
.insert(
r.hashMap()
.with("id", "article1")
.with("name", "String Interpolation in Java")
.with("url", "https://www.baeldung.com/java-string-interpolation")
).run(conn);
r.db(DB_NAME).table(tableName)
.insert(
r.hashMap()
.with("id", "article2")
.with("name", "Access HTTPS REST Service Using Spring RestTemplate")
.with("url", "https://www.baeldung.com/spring-resttemplate-secure-https-service")
).run(conn);
}
@Test
public void updateAll() {
r.db(DB_NAME).table(tableName).update(r.hashMap().with("site", "Baeldung")).run(conn);
}
@Test
public void updateSome() {
r.db(DB_NAME).table(tableName)
.filter(r -> r.g("name").eq("String Interpolation in Java"))
.update(r.hashMap().with("category", "java"))
.run(conn);
}
@Test
public void delete() {
r.db(DB_NAME).table(tableName)
.filter(r -> r.g("name").eq("String Interpolation in Java"))
.delete()
.run(conn);
}
}