Implementation Spring Data MarkLogic :
An implementation of the Spring Data MarkLogic NoSQL database that supports the following features:
- Spring configuration support using Java-based @Configuration classes
- MarkLogicTemplate helper class for common MarkLogic operations. Integrated object mapping between documents and POJOs (using Jackson by Default).
- Exception translation into Spring’s portable Data Access Exception hierarchy
- Automatic implementation of Repository interfaces including support for custom finder methods.
How to connect the Spring Data to Marklogic :
Spring MarkLogic support requires MarkLogic 8 or above and Java 8 or above. We will need a running MarkLogic server in order to get started.
First of all, we will need Marklogic to be installed.
Spring Data Marklogic connects to Marklogic using the DatabaseClient, so you need a DatabaseClient server up and ready.
Then add the Spring Data Marklogic dependency to your project.
Include the following dependency in your project’s pom.xml
dependencies section:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.7.3</version>
</dependency>
Create a simple Person class that you can use to save person information:
package com.knoldus.springdata.SpringDatawithMarklogic;
import org.springframework.data.annotation.Id;
public class Person {
@Id
private int id;
private String name;
private int age;
public Person() {}
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
And an application:
package com.knoldus.springdata.SpringDatawithMarklogic; import com.marklogic.client.DatabaseClientFactory.DigestAuthContext; import com.marklogic.client.query.StructuredQueryBuilder; import com.knoldus.springdata.SpringDatawithMarklogic.MarkLogicOperations; import com.knoldus.springdata.SpringDatawithMarklogic.MarkLogicTemplate; import static com.marklogic.client.DatabaseClientFactory.newClient; public class MarkLogicApp { private static final StructuredQueryBuilder qb = new StructuredQueryBuilder(); public static void main(String[] args) throws Exception { MarkLogicOperations ops = new MarkLogicTemplate(newClient("localhost", 8000, new DigestAuthContext("admin", "admin"))); ops.write(new Person(1,"Bobby", 23)); Person bobby = ops.searchOne(qb.value(qb.jsonProperty("name"), "Bobby"), Person.class); System.out.println(bobby); ops.deleteAll("Person"); } }
This will produce output similar to the following:
Person [id=1, name=Bobby, age=23]
There are a few things of note in this example:
- By default, this uses Jackson to serialize your POJOs for persistence in the database, and deserialize them when pulling data out of the database. There is an empty default constructor because Jackson creates the POJO instance first before setting the fields from the data
- An
@Id
annotation is required to identify the field that will hold the document identifier. Internally documents are stored under a URI, so a person with an id of “1” would be saved under the URI “/Person/1.json” - You can create the template class for MarkLogic,
MarkLogicTemplate
, by passing in aDatabaseClient
that you get from theDatabaseClientFactory
Connecting to MarkLogic with Spring
The simplest way to connect to MarkLogic is to create a @Bean that creates a template object:
package com.knoldus.springdata.SpringDatawithMarklogic;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory.DigestAuthContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.knoldus.springdata.SpringDatawithMarklogic;.MarkLogicOperations;
import com.knoldus.springdata.SpringDatawithMarklogic;.MarkLogicTemplate;
import static com.marklogic.client.DatabaseClientFactory.newClient;
@Configuration
public class RepositoryConfiguration {
@Bean
public MarkLogicOperations marklogicTemplate() {
DatabaseClient client = newClient("localhost", 8000, new
DigestAuthContext("admin", "root"));
return new MarkLogicTemplate(client);
}
}
This will automatically be wired into any of your @Repository
annotated classes (or ones that extend MarkLogicRepository
). You can also explicitly auto-wired it in your other Spring classes:
@Autowired
private MarkLogicOperations ops;
References :
https://github.com/knoldus/springboot-marklogic–sample-app
