microsoft / spring-data-gremlin

We are in the process of deprecating Spring Data Gremlin. -- Provide generic annotation oriented programming form based on gremlin for graph database
Other
129 stars 76 forks source link
deprecating

MIT License Build Status codecov

:exclamation::exclamation::exclamation:

We have deprecated Spring Data Gremlin. We recommend that you use the TinkerPop driver to query Cosmos DB with Gremlin API or use azure-spring-boot-starter-cosmos to query Cosmos DB with Spring Data SQL API. If you would like us to continue supporting Spring Data Gremlin, please tell us how you are using Spring Data Gremlin for Cosmos DB and how important it is to you, by voting on this issue with the thumbs up emoji.

Spring Data Gremlin

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.

Spring Data Version Support

Version mapping between spring boot and spring-data-gremlin:

Spring boot version spring-data-gremlin version
version Maven Central
version Maven Central
version Maven Central
version Maven Central

TOC

Welcome To Contribute

Contribution is welcome. Please follow this instruction to contribute code.

Sample Code

Please refer to sample project here.

Spring data version support

This repository only supports Spring Data 2.x.

Feature List

Quick Start

Add the dependency

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.1.7</version>
</dependency>

Setup Configuration

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 an entity

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;

    ...
}

Create repositories

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.

Create an Application class

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.

Filing Issues

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.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Data/Telemetry

This project collects usage data and sends it to Microsoft to help improve our products and services. Read our privacy statement to learn more.