Merge pull request #14305 from Eredisse/master

BAEL-6024 - Guide to YugabyteDB
This commit is contained in:
Vini 2023-07-07 15:14:50 +02:00 committed by GitHub
commit 2546a2e218
5 changed files with 132 additions and 89 deletions

View File

@ -1,55 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-yugabytedb</artifactId> <artifactId>spring-data-yugabytedb</artifactId>
<version>1.0</version> <version>1.0</version>
<name>spring-data-yugabytedb</name> <name>spring-data-yugabytedb</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath> <relativePath>../../parent-boot-2</relativePath>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.springframework</groupId>
<artifactId>lombok</artifactId> <artifactId>spring-test</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
</dependency> <scope>test</scope>
<dependency> </dependency>
<groupId>org.springframework.boot</groupId> <dependency>
<artifactId>spring-boot-starter-test</artifactId> <groupId>org.postgresql</groupId>
<scope>test</scope> <artifactId>postgresql</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>postgresql</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> </dependencies>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -8,25 +8,25 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class Main implements CommandLineRunner { public class Main implements CommandLineRunner {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Main.class, args); SpringApplication.run(Main.class, args);
} }
@Override @Override
public void run(String... args) throws InterruptedException { public void run(String... args) throws InterruptedException {
int iterationCount = 1_000; int iterationCount = 1_000;
int elementsPerIteration = 100; int elementsPerIteration = 100;
for (int i = 0; i < iterationCount; i++) { for (int i = 0; i < iterationCount; i++) {
for (long j = 0; j < elementsPerIteration; j++) { for (long j = 0; j < elementsPerIteration; j++) {
User user = new User(); User user = new User();
userRepository.save(user); userRepository.save(user);
} }
Thread.sleep(1000); Thread.sleep(1000);
} }
} }
} }

View File

@ -11,34 +11,31 @@ import javax.persistence.Table;
@Table(name = "users") @Table(name = "users")
public class User { public class User {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@Column @Column
private String name; private String name;
Long getId() { public Long getId() {
return id; return id;
} }
void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
String getName() { public String getName() {
return name; return name;
} }
void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@Override @Override
public String toString() { public String toString() {
return "User{" + return "User{" + "id=" + id + ", name='" + name + '\'' + '}';
"id=" + id + }
", name='" + name + '\'' +
'}';
}
} }

View File

@ -0,0 +1,13 @@
version: '3'
services:
yugabytedb:
image: yugabytedb/yugabyte:latest
container_name: yugabyte
user: root
ports:
- '5433:5433'
- '7000:7000'
- '9000:9000'
command: ["bin/yugabyted", "start", "--daemon=false"]

View File

@ -0,0 +1,37 @@
package com.baeldung;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/*
To run this test we need to run the databases first.
A dedicated docker-compose.yml file is located under the resources directory.
We can run it by simple executing `docker-compose up`.
*/
@SpringJUnitConfig
@SpringBootTest
@TestPropertySource("classpath:application.properties")
public class YugabyteDBLiveTest {
@Autowired
private UserRepository userRepository;
@Test
void givenTwoUsers_whenPersistUsingJPARepository_thenUserAreSaved() {
User user1 = new User();
user1.setName("Alex");
User user2 = new User();
user2.setName("John");
userRepository.save(user1);
userRepository.save(user2);
List<User> allUsers = userRepository.findAll();
assertEquals(2, allUsers.size());
}
}