Spring Data Gremlin provides initial Spring Data support for those databases using Gremlin query language. With annotation oriented programming model, it simplified the mapping to the database entity. It also provides supports for basic and custom query.
This project works with any Gremlin-compatible data store, and also with Azure Cosmos DB. Cosmos is a globally-distributed database service that allows developers to work with data using a variety of standard APIs, such as Graph, MongoDB, and SQL. Spring Data Gremlin provides a delightful experience to interact with Azure Cosmos DB Graph API.
- Welcome to Contribute
- Sample Code
- Spring data version support
- Feature List
- Quick Start
- Filing Issues
- Code of Conduct
Contribution is welcome. Please follow this instruction to contribute code.
Please refer to sample project here.
This repository only supports Spring Data 2.x.
- Spring Data CRUDRepository basic CRUD functionality
- save
- findAll
- findById
- deleteAll
- deleteById
- Spring Data @Id annotation.
There're 2 ways to map a field in domain class to
id
field of a database entity.- annotate a field in domain class with
@Id
- set name of this field to
id
- annotate a field in domain class with
- Default annotaion
@Vertex
maps anObject
to aVertex
@VertexSet
maps a set ofVertex
@Edge
maps anObject
to anEdge
@EdgeSet
maps to a set ofEdge
@EdgeFrom
maps to the headVertex
of anEdge
@EdgeTo
maps to the tailVertex
of anEdge
@Graph
maps to anObject
to aGraph
- Supports advanced operations
<T> T findVertexById(Object id, Class<T> domainClass);
<T> T findEdgeById(Object id, Class<T> domainClass);
<T> boolean isEmptyGraph(T object)
long vertexCount()
long edgeCount()
- Supports Spring Data custom query find operation, e.g.,
findByAFieldAndBField
- Supports any class type in domain class including collection and nested type.
spring-data-gremlin
is published on Maven Central Repository.
If you are using Maven, add the following dependency.
<dependency>
<groupId>com.microsoft.spring.data.gremlin</groupId>
<artifactId>spring-data-gremlin</artifactId>
<version>2.0.0</version>
</dependency>
Setup application.yml
file.(Use Azure Cosmos DB Graph as an example.)
gremlin:
endpoint: url-of-endpoint
port: 443
username: /dbs/your-db-name/colls/your-collection-name
password: your-password
telemetryAllowed: true # set false to disable telemetry
Define a simple Vertex entity with @Vertex
.
@Vertex
public class Person {
@Id
private String id;
private String name;
private String age;
...
}
Define a simple Edge entity with @Edge
.
@Edge
public class Relation {
@Id
private String id;
private String name;
@EdgeFrom
private Person personFrom;
@EdgeTo
private Person personTo;
...
}
Define a simple Graph entity with @Graph
.
@Graph
public class Network {
@Id
private String id;
public Network() {
this.edges = new ArrayList<Object>();
this.vertexes = new ArrayList<Object>();
}
@EdgeSet
private List<Object> edges;
@VertexSet
private List<Object> vertexes;
...
}
Extends DocumentDbRepository interface, which provides Spring Data repository support.
import GremlinRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends GremlinRepository<Person, String> {
List<Person> findByName(String name);
}
findByName
method is custom query method, it will find the person with the name
property.
Here create an application class with all the components
@SpringBootApplication
public class SampleApplication implements CommandLineRunner {
@Autowired
private PersonRepository repository;
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
public void run(String... var1) throws Exception {
private final Person testUser = new Person("PERSON_ID", "PERSON_NAME", "PERSON_AGE");
repository.deleteAll();
repository.save(testUser);
...
}
}
Autowired UserRepository interface, then can do save, delete and find operations. Spring Data Azure Cosmos DB uses the DocumentTemplate to execute the queries behind find, save methods. You can use the template yourself for more complex queries.
If you encounter any bug, please file an issue here.
To suggest a new feature or changes that could be made, file an issue the same way you would for a bug.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
This project collects usage data and sends it to Microsoft to help improve our products and services. Read our privacy statement to learn more.