hugegraph 支持sparql 与cypher

2020-11-24 15:38:30 浏览数 (1)

hugegraph 是百度开源的基于tinkerpop的图数据库,支持通过gremlin进行查询。

这里,我们来扩展支持sparql 与cypher。

sparql支持

github上有SparqlToGremlinCompiler,可以支持将sparql转GraphTraversal,集成该工具库即可:

代码语言:javascript复制
@Path("graphs/{graph}/sparql")
@Singleton
public class SparqlAPI extends API {

    private static final Logger LOG = Log.logger(SparqlAPI.class);

    @GET
    @Timed
    @CompressInterceptor.Compress
    @Produces(APPLICATION_JSON_WITH_CHARSET)
    public String query(@Context GraphManager manager,
                        @PathParam("graph") String graph,
                        @QueryParam("sparql") String sparql) {
        LOG.debug("Graph [{}] query by sparql: {}", graph, sparql);

        E.checkArgument(sparql != null && !sparql.isEmpty(),
                "The sparql parameter can't be null or empty");

        HugeGraph g = graph(manager, graph);
        GraphTraversal<Vertex, ?> traversal = SparqlToGremlinCompiler.convertToGremlinTraversal(g, sparql);
        List<?> result = traversal.toList();
        if (result.size() > 0) {
            Object item = result.get(0);
            if (item instanceof Vertex) {
                return manager.serializer(g).writeVertices((Iterator<Vertex>) result.iterator(), false);
            }
            if (item instanceof Map) {
                return manager.serializer(g).writeMap((Map) item);
            }
        }

        return result.toString();
    }
}

cypher 支持

opencypher 提供了translation包,支持将cypher转为gremlin:

代码语言:javascript复制
        <dependency>
            <groupId>org.opencypher.gremlin</groupId>
            <artifactId>translation</artifactId>
            <version>1.0.4</version>
        </dependency>

转换代码:

代码语言:javascript复制
 TranslationFacade cfog = new TranslationFacade();
 String gremlin = cfog.toGremlinGroovy(cypher);

增加api代码

代码语言:javascript复制
     @GET
    @Timed
    @CompressInterceptor.Compress
    @Produces(APPLICATION_JSON_WITH_CHARSET)
    public Response query(@Context GraphManager manager,
                          @PathParam("graph") String graph,
                          @Context HugeConfig conf,
                          @Context HttpHeaders headers,
                          @QueryParam("cypher") String cypher) {
        LOG.debug("Graph [{}] query by cypher: {}", graph, cypher);

        return getResponse(graph, headers, cypher);
    }

    private Response getResponse(@PathParam("graph") String graph, @Context HttpHeaders headers, @QueryParam("cypher") String cypher) {
        E.checkArgument(cypher != null && !cypher.isEmpty(),
                "The cypher parameter can't be null or empty");

        TranslationFacade cfog = new TranslationFacade();
        String gremlin = cfog.toGremlinGroovy(cypher);
        gremlin = "g = "   graph   ".traversal()n"   gremlin;
        String auth = headers.getHeaderString(HttpHeaders.AUTHORIZATION);
        MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
        params.put("gremlin", Arrays.asList(gremlin));
        Response response = this.client().doGetRequest(auth, params);
        return transformResponseIfNeeded(response);
    }

0 人点赞