Home > Software design >  How to use Spring Boot for neptune database?
How to use Spring Boot for neptune database?

Time:10-28

I am using Amazon Neptune for my application. So I want to know if there is a way to use Spring Boot (Java) to connect and use data from queries in Neptune workbench for my app. I have successfully connected to Neptune database using Java core with Visual Studio Code but I can't find any documentation that clearly describes this procedure for Spring Boot. Can some help me in this case? Thanks in advance!

CodePudding user response:

To use Neptune with springboot you have to add the configuration file like this below.

@Configuration
public class NeptuneConfigExample {


@Bean
public Cluster cluster() {
    return Cluster.build()
            .addContactPoint("your-neptune-instance-here.xxxxxxxxxxxx.us-east-1.neptune.amazonaws.com")
            .port(8182)
            .enableSsl(true)
            .keyCertChainFile("./cert/SFSRootCAG2.pem") // download it from https://www.amazontrust.com/repository/SFSRootCAG2.pem 
            .maxConnectionPoolSize(5)
            .maxInProcessPerConnection(1)
            .maxSimultaneousUsagePerConnection(1)
            .create();
    }
}

For reference - click

CodePudding user response:

To connect to Neptune

Install Apache Maven on your EC2 instance. First, enter the following to add a repository with a Maven package:

sudo wget https://repos.fedorapeople.org/repos/dchen/apache- 
maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache- 
maven.repo

Enter the following to set the version number for the packages:

sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache- 
maven.repo

Then you can use yum to install Maven:

 sudo yum install -y apache-maven

This example was tested with Java 8 only. Enter the following to install Java 8 on your EC2 instance:

sudo yum install java-1.8.0-devel

Enter the following to set Java 8 as the default runtime on your EC2 instance:

sudo /usr/sbin/alternatives --config java

When prompted, enter the number for Java 8.

Enter the following to set Java 8 as the default compiler on your EC2 instance:

sudo /usr/sbin/alternatives --config javac

When prompted, enter the number for Java 8.

In a new directory, create a pom.xml file, and then open it in a text editor.

Copy the following into the pom.xml file and save it (you can usually adjust the version numbers to the latest stable version):

<project xmlns="https://maven.apache.org/POM/4.0.0" 
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 
 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
 <groupId>com.amazonaws</groupId>
 <artifactId>RDFExample</artifactId>
<packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
<name>RDFExample</name>
<url>https://maven.apache.org</url>
<dependencies>
<dependency>
  <groupId>org.eclipse.rdf4j</groupId>
  <artifactId>rdf4j-runtime</artifactId>
  <version>3.6</version>
 </dependency>
 </dependencies>
 <build>
<plugins>
  <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
      <configuration>
        <mainClass>com.amazonaws.App</mainClass>
      </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
       </configuration>
      </plugin>
    </plugins>
    </build>
 </project>    

Note If you are modifying an existing Maven project, the required dependency is highlighted in the preceding code.

To create subdirectories for the example source code (src/main/java/com/amazonaws/), enter the following at the command line:

mkdir -p src/main/java/com/amazonaws/

In the src/main/java/com/amazonaws/ directory, create a file named App.java, and then open it in a text editor.

Copy the following into the App.java file. Replace your-neptune-endpoint with the address of your Neptune DB instance.

package com.amazonaws;

import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.http.HTTPRepository;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;

import java.util.List;
import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryLanguage;
import org.eclipse.rdf4j.model.Value;

public class App
{
public static void main( String[] args )
{
    String sparqlEndpoint = "https://your-neptune- 
  endpoint:port/sparql";
    Repository repo = new SPARQLRepository(sparqlEndpoint);
    repo.initialize();

    try (RepositoryConnection conn = repo.getConnection()) {
       String queryString = "SELECT ?s ?p ?o WHERE { ?s ?p ?o } 
    limit 10";

       TupleQuery tupleQuery = 
 conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);

       try (TupleQueryResult result = tupleQuery.evaluate()) {
          while (result.hasNext()) {  // iterate over the result
               BindingSet bindingSet = result.next();

               Value s = bindingSet.getValue("s");
               Value p = bindingSet.getValue("p");
               Value o = bindingSet.getValue("o");

               System.out.print(s);
               System.out.print("\t");
               System.out.print(p);
               System.out.print("\t");
               System.out.println(o);
          }
       }
    }
}
}

Use the following Maven command to compile and run the sample:

 mvn compile exec:java
  • Related