From b1d0b62dafa1d09d19d6fddc416af90921b8115f Mon Sep 17 00:00:00 2001 From: jadepeng Date: Tue, 10 May 2022 17:28:34 +0800 Subject: [PATCH 1/3] feat: support query by cypher --- .../hugegraph/api/gremlin/CypherAPI.java | 46 ++++++++++++++ .../baidu/hugegraph/driver/CypherManager.java | 44 ++++++++++++++ .../baidu/hugegraph/driver/HugeClient.java | 8 ++- .../structure/constant/HugeType.java | 5 +- .../com/baidu/hugegraph/BaseClientTest.java | 6 ++ .../baidu/hugegraph/api/CypherApiTest.java | 60 +++++++++++++++++++ 6 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java create mode 100644 hugegraph-client/src/main/java/com/baidu/hugegraph/driver/CypherManager.java create mode 100644 hugegraph-client/src/test/java/com/baidu/hugegraph/api/CypherApiTest.java diff --git a/hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java b/hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java new file mode 100644 index 000000000..83616a7fc --- /dev/null +++ b/hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api.gremlin; + +import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.client.RestClient; +import com.baidu.hugegraph.rest.RestResult; +import com.baidu.hugegraph.structure.constant.HugeType; +import com.baidu.hugegraph.structure.gremlin.Response; + +public class CypherAPI extends API { + + private static final String PATH = "graphs/%s/cypher"; + + public CypherAPI(RestClient client, String graph) { + super(client); + this.path(PATH, graph); + } + + @Override + protected String type() { + return HugeType.CYPHER.string(); + } + + public Response post(String cypher) { + RestResult result = this.client.post(this.path(), cypher); + return result.readObject(Response.class); + } +} \ No newline at end of file diff --git a/hugegraph-client/src/main/java/com/baidu/hugegraph/driver/CypherManager.java b/hugegraph-client/src/main/java/com/baidu/hugegraph/driver/CypherManager.java new file mode 100644 index 000000000..a39333ce4 --- /dev/null +++ b/hugegraph-client/src/main/java/com/baidu/hugegraph/driver/CypherManager.java @@ -0,0 +1,44 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.driver; + +import com.baidu.hugegraph.api.gremlin.CypherAPI; +import com.baidu.hugegraph.client.RestClient; +import com.baidu.hugegraph.structure.gremlin.Response; +import com.baidu.hugegraph.structure.gremlin.ResultSet; + +public class CypherManager { + + private final GraphManager graphManager; + private final CypherAPI cypherAPI; + + public CypherManager(RestClient client, String graph, + GraphManager graphManager) { + this.graphManager = graphManager; + this.cypherAPI = new CypherAPI(client, graph); + } + + public ResultSet execute(String cypher) { + Response response = this.cypherAPI.post(cypher); + response.graphManager(this.graphManager); + // TODO: Can add some checks later + return response.result(); + } +} diff --git a/hugegraph-client/src/main/java/com/baidu/hugegraph/driver/HugeClient.java b/hugegraph-client/src/main/java/com/baidu/hugegraph/driver/HugeClient.java index 43e5f67ae..3425cd084 100644 --- a/hugegraph-client/src/main/java/com/baidu/hugegraph/driver/HugeClient.java +++ b/hugegraph-client/src/main/java/com/baidu/hugegraph/driver/HugeClient.java @@ -40,6 +40,7 @@ public class HugeClient implements Closeable { private SchemaManager schema; private GraphManager graph; private GremlinManager gremlin; + private CypherManager cypher; private TraverserManager traverser; private VariablesManager variable; private JobManager job; @@ -97,6 +98,7 @@ private void initManagers(RestClient client, String graph) { this.schema = new SchemaManager(client, graph); this.graph = new GraphManager(client, graph); this.gremlin = new GremlinManager(client, graph, this.graph); + this.cypher = new CypherManager(client, graph, this.graph); this.traverser = new TraverserManager(client, this.graph); this.variable = new VariablesManager(client, graph); this.job = new JobManager(client, graph); @@ -108,7 +110,7 @@ private void initManagers(RestClient client, String graph) { private void checkServerApiVersion() { VersionUtil.Version apiVersion = VersionUtil.Version.of( this.version.getApiVersion()); - VersionUtil.check(apiVersion, "0.38", "0.68", + VersionUtil.check(apiVersion, "0.38", "0.70", "hugegraph-api in server"); this.client.apiVersion(apiVersion); } @@ -129,6 +131,10 @@ public GremlinManager gremlin() { return this.gremlin; } + public CypherManager cypher() { + return this.cypher; + } + public TraverserManager traverser() { return this.traverser; } diff --git a/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java b/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java index f505dc92a..e6015000a 100644 --- a/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java +++ b/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java @@ -60,7 +60,10 @@ public enum HugeType { VERSION(230, "versions"), // Metrics - METRICS(240, "metrics"); + METRICS(240, "metrics"), + + // Cypher + CYPHER(250, "cypher"); private int code; private String name = null; diff --git a/hugegraph-client/src/test/java/com/baidu/hugegraph/BaseClientTest.java b/hugegraph-client/src/test/java/com/baidu/hugegraph/BaseClientTest.java index 94d5c4d73..c2da43b1a 100644 --- a/hugegraph-client/src/test/java/com/baidu/hugegraph/BaseClientTest.java +++ b/hugegraph-client/src/test/java/com/baidu/hugegraph/BaseClientTest.java @@ -10,6 +10,7 @@ import org.junit.BeforeClass; import com.baidu.hugegraph.driver.AuthManager; +import com.baidu.hugegraph.driver.CypherManager; import com.baidu.hugegraph.driver.GraphManager; import com.baidu.hugegraph.driver.GraphsManager; import com.baidu.hugegraph.driver.GremlinManager; @@ -77,6 +78,11 @@ public static GremlinManager gremlin() { return client.gremlin(); } + public static CypherManager cypher() { + Assert.assertNotNull("Not opened client", client); + return client.cypher(); + } + public static TraverserManager traverser() { Assert.assertNotNull("Not opened client", client); return client.traverser(); diff --git a/hugegraph-client/src/test/java/com/baidu/hugegraph/api/CypherApiTest.java b/hugegraph-client/src/test/java/com/baidu/hugegraph/api/CypherApiTest.java new file mode 100644 index 000000000..589b769b2 --- /dev/null +++ b/hugegraph-client/src/test/java/com/baidu/hugegraph/api/CypherApiTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baidu.hugegraph.structure.gremlin.ResultSet; +import com.baidu.hugegraph.testutil.Assert; + +public class CypherApiTest extends BaseApiTest { + + @BeforeClass + public static void prepareSchema() { + BaseApiTest.initPropertyKey(); + BaseApiTest.initVertexLabel(); + BaseApiTest.initEdgeLabel(); + } + + @Before + public void prepareData() { + BaseApiTest.initVertex(); + BaseApiTest.initEdge(); + } + + @Test + public void testCreate() { + String cypher = "CREATE (n:person { name : 'test', age: 20, city: 'Hefei' }) return n"; + ResultSet resultSet = cypher().execute(cypher); + Assert.assertEquals(1, resultSet.size()); + } + + @Test + public void testRelationQuery() { + String cypher = "MATCH (n:person)-[r:knows]->(friend:person)\n" + + "WHERE n.name = 'marko'\n" + + "RETURN n, friend.name AS friend"; + + ResultSet resultSet = cypher().execute(cypher); + Assert.assertEquals(2, resultSet.size()); + } +} From 63e77caa6ac57545488b0d76a55e6a3ac787539f Mon Sep 17 00:00:00 2001 From: jadepeng Date: Wed, 11 May 2022 14:28:18 +0800 Subject: [PATCH 2/3] add a blank line at the end of file --- .../main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java b/hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java index 83616a7fc..a87ce2b7b 100644 --- a/hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java +++ b/hugegraph-client/src/main/java/com/baidu/hugegraph/api/gremlin/CypherAPI.java @@ -43,4 +43,4 @@ public Response post(String cypher) { RestResult result = this.client.post(this.path(), cypher); return result.readObject(Response.class); } -} \ No newline at end of file +} From e5c282ad8adcf19500be42bc4ff97d0f145abb24 Mon Sep 17 00:00:00 2001 From: jadepeng Date: Mon, 16 May 2022 19:52:58 +0800 Subject: [PATCH 3/3] change Cypher type code --- .../com/baidu/hugegraph/structure/constant/HugeType.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java b/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java index e6015000a..cdfb1818d 100644 --- a/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java +++ b/hugegraph-client/src/main/java/com/baidu/hugegraph/structure/constant/HugeType.java @@ -54,16 +54,16 @@ public enum HugeType { // Gremlin GREMLIN(201, "gremlin"), + // Cypher + CYPHER(202, "cypher"), + GRAPHS(220, "graphs"), // Version VERSION(230, "versions"), // Metrics - METRICS(240, "metrics"), - - // Cypher - CYPHER(250, "cypher"); + METRICS(240, "metrics"); private int code; private String name = null;