Merge pull request #15319 from parthiv39731/PR-7259

BAEL-7259
This commit is contained in:
davidmartinezbarua
2023-12-02 14:14:23 -03:00
committed by GitHub
8 changed files with 604 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>java-harperdb</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>java-harperdb</artifactId>
<version>${haperdb-driver.version}</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/cdata.jdbc.harperdb.jar</systemPath>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>1.19.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<haperdb-driver.version>4.2</haperdb-driver.version>
</properties>
</project>
@@ -0,0 +1,41 @@
@startuml
'https://gist.github.com/QuantumGhost/0955a45383a0b6c0bc24f9654b3cb561
' uncomment the line below if you're using computer with a retina display
' skinparam dpi 300
!theme sketchy-outline
!define Table(name,desc) class name as "desc" << (T, #63b175) >>
' we use bold for primary key
' green color for unique
' and underscore for not_null
!define primary_key(x) <b>x</b>
!define unique(x) <color:green>x</color>
!define not_null(x) <u>x</u>
' other tags available:
' <i></i>
' <back:COLOR></color>, where color is a color name or html color code
' (#FFAACC)
' see: http://plantuml.com/classes.html#More
hide methods
hide stereotypes
' entities
Table(Teacher, "Teacher") {
primary_key(Id) Number
Name Varchar
joining_date Date
}
Table(Subject, "Subject") {
primary_key(Id) Number
Name Varchar
}
Table(Teacher_Details, "Teacher_Details") {
primary_key(Id) Number
teacher_id Number
subject_id Number
}
Teacher_Details "*" -left-> "1" Subject: " "
Teacher "1" -left-> "*" Teacher_Details: " "
@enduml
@@ -0,0 +1,78 @@
package com.baeldung.harperdb;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.classic.methods.HttpPost;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.ClassicHttpRequest;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpEntity;
import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.io.entity.StringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Base64;
public class HarperDBApiService {
private static final Logger LOGGER = LoggerFactory.getLogger(HarperDBApiService.class);
private String url;
private String base64auth;
public HarperDBApiService(String url, String user, String password) {
this.url = url;
this.base64auth = Base64.getEncoder()
.encodeToString(new StringBuilder().append(user)
.append(":")
.append(password)
.toString()
.getBytes());
}
public void createSchema(String schema) throws IOException {
String requestBody = "{\"operation\":\"create_schema\", \"" + "schema\":\"" + schema + "\"" + "}";
executeHttpPostRequest(requestBody);
}
public void createTable(String schema, String table, String ... attributes) throws IOException {
String createTableReq = "{\"operation\":\"create_table\","
+ "\"schema\":\"" + schema
+ "\",\"table\":\"" + table
+ "\",\"hash_attribute\":\"id\""
+ "}";
executeHttpPostRequest(createTableReq);
LOGGER.info("created table:" + table);
for (String attribute : attributes) {
String createAttrReq = "{\"operation\":\"create_attribute\",\"schema\":\""
+ schema + "\",\"table\":\""
+ table + "\",\"attribute\":\""
+ attribute + "\""
+ "}";
executeHttpPostRequest(createAttrReq);
LOGGER.info("created attribute:" + attribute + " in table:" + table);
}
}
public void insertRecords(String schema, String table, String records) throws IOException {
String requestBody = "{\"table\":" + "\"" + table + "\","
+ "\"schema\":" + "\"" + schema + "\"" + ","
+ "\"operation\":" + "\"" + "insert" + "\"" + ","
+ "\"records\":" + records
+ "}";
executeHttpPostRequest(requestBody);
}
private void executeHttpPostRequest(String httpRequest) throws IOException {
LOGGER.info("Post request body:" + httpRequest);
try (CloseableHttpClient closeableHttpClient = HttpClientBuilder.create()
.build()) {
HttpPost request = new HttpPost(this.url);
request.addHeader("Authorization", "Basic " + this.base64auth);
request.addHeader("Content-Type", "application/json");
request.setEntity((HttpEntity) new StringEntity(httpRequest));
CloseableHttpResponse response = closeableHttpClient.execute((ClassicHttpRequest) request);
LOGGER.info("REST API response:" + response.toString());
assert (200 == response.getCode());
}
}
}
@@ -0,0 +1,36 @@
package com.baeldung.harperdb;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
public class HarperDBContainer {
private final static Logger LOGGER = LoggerFactory.getLogger(HarperDBContainer.class);
private static final Map<String, String> DEFAULT_ENV_MAP = Map.of("HDB_ADMIN_USERNAME", "admin", "HDB_ADMIN_PASSWORD", "password",
"OPERATIONSAPI_NETWORK_PORT", "9925", "ROOTPATH", "/home/harperdb/hdb", "LOGGING_STDSTREAMS", "true");
private static final Integer[] DEFAULT_EXPOSED_PORTS = { 9925, 9926 };
private static final String HARPER_DOCKER_IMAGE = "harperdb/harperdb:latest";
public void stop() {
harperDBContainer.stop();
}
GenericContainer harperDBContainer;
public GenericContainer installHarperDB() {
harperDBContainer = new GenericContainer(HARPER_DOCKER_IMAGE).withEnv(DEFAULT_ENV_MAP)
.withExposedPorts(DEFAULT_EXPOSED_PORTS);
return harperDBContainer;
}
public GenericContainer installHarperDB(String dockerImgage, final Map<String, String> envMap, final Integer... exposedPorts) {
return new GenericContainer(dockerImgage).withEnv(envMap)
.withExposedPorts(exposedPorts);
}
}
@@ -0,0 +1,5 @@
{
"View_Teacher_Details": {
"query": "SELECT t.name as teacher_name, t.joining_date as joining_date, s.name as subject_name from Demo.Teacher_Details AS td INNER JOIN Demo.Teacher AS t ON t.id = td.teacher_id INNER JOIN Demo.Subject AS s on s.id = td.subject_id"
}
}
+1
View File
@@ -817,6 +817,7 @@
<module>pdf</module>
<module>performance-tests</module>
<module>persistence-modules</module>
<module>persistence-modules/java-harperdb</module>
<module>persistence-modules/spring-data-neo4j</module>
<module>protobuffer</module>
<module>quarkus-modules</module>