Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt java-client: support cpyher query api #272

Merged
merged 4 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2017 HugeGraph Authors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use 2022?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original codes are also 2017, we can update them uniformly in the future.

*
* 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);
}
}
jadepeng marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017 HugeGraph Authors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

*
* 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -129,6 +131,10 @@ public GremlinManager gremlin() {
return this.gremlin;
}

public CypherManager cypher() {
return this.cypher;
}

public TraverserManager traverser() {
return this.traverser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public enum HugeType {
VERSION(230, "versions"),

// Metrics
METRICS(240, "metrics");
METRICS(240, "metrics"),

// Cypher
CYPHER(250, "cypher");
jadepeng marked this conversation as resolved.
Show resolved Hide resolved

private int code;
private String name = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017 HugeGraph Authors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

*
* 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());
}
}