incubator-hugegraph icon indicating copy to clipboard operation
incubator-hugegraph copied to clipboard

[Question] How to use the gremlin statement to trigger oltp query

Open dongma opened this issue 2 years ago • 4 comments

Problem Type (问题类型)

gremlin (结果不合预期)

Before submit

  • [X] 我已经确认现有的 Issues 与 FAQ 中没有相同 / 重复问题 (I have confirmed and searched that there are no similar problems in the historical issue and documents)

Environment (环境信息)

  • OS: xx CPUs, xx G RAM, Ubuntu 2x.x / CentOS 7.x
  • Data Size: xx vertices, xx edges

Server Version: 1.0.0 (Apache Release Version) Backend: Hbase

Your Question (问题描述)

如下是从官方文档找到功能对应的PR,能提供一个gremlin触发的示例? https://github.com/apache/incubator-hugegraph/pull/1289

Vertex/Edge example (问题点 / 边数据举例)

No response

Schema [VertexLabel, EdgeLabel, IndexLabel] (元数据结构)

No response

dongma avatar May 29 '24 02:05 dongma

To trigger an OLTP query in HugeGraph using a Gremlin statement, you can use the executeQuery method from the GremlinQueryService class. Here is an example of how you can use this method:

public GremlinResult executeQuery(int connId, GremlinQuery query) {
    HugeClient client = this.getClient(connId);
    log.debug("The original gremlin ==> {}", query.getContent());
    String gremlin = this.optimize(query.getContent());
    log.debug("The optimized gremlin ==> {}", gremlin);
    // Execute gremlin query
    ResultSet resultSet = this.executeGremlin(gremlin, client);
    // Scan data, vote the result type
    TypedResult typedResult = this.parseResults(resultSet);
    // Build json view
    JsonView jsonView = new JsonView(typedResult.getData());
    // Build table view
    TableView tableView = this.buildTableView(typedResult);
    // Build graph view
    GraphView graphView = this.buildGraphView(typedResult, client);
    return GremlinResult.builder()
                        .type(typedResult.getType())
                        .jsonView(jsonView)
                        .tableView(tableView)
                        .graphView(graphView)
                        .build();
}

To use this method, create an instance of GremlinQuery with your Gremlin statement and pass it along with the connection ID to the executeQuery method. The method will execute the query and return the results in a GremlinResult object.

Additionally, you can use the REST API to execute Gremlin queries in HugeGraph. Here is an example of how to post a Gremlin query using the REST API:

@Test
public void testPost() {
    String body = "{" +
                  "\"gremlin\":\"g.V()\"," +
                  "\"bindings\":{}," +
                  "\"language\":\"gremlin-groovy\"," +
                  "\"aliases\":{\"g\":\"__g_hugegraph\"}}";
    assertResponseStatus(200, client().post(path, body));
}

In this example, the testPost method sends a POST request to the /gremlin endpoint with a Gremlin query to retrieve all vertices (g.V()). The response status is then checked to ensure it is 200 (OK) [1][2][3].

Details

About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

dosubot[bot] avatar May 29 '24 02:05 dosubot[bot]

@dosu 在源代码中没有找到 GremlinQueryService这个类

dongma avatar May 29 '24 03:05 dongma

The GremlinQueryService class is used in the GremlinQueryController class, which is located in the hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/controller/query/GremlinQueryController.java file. You can find the GremlinQueryService class in the hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/query/GremlinQueryService.java file [1].

Here is an example of how you can use the executeQuery method from the GremlinQueryService class:

import org.apache.hugegraph.controller.query.GremlinQueryController;
import org.apache.hugegraph.entity.query.GremlinQuery;
import org.apache.hugegraph.entity.query.GremlinResult;
import org.apache.hugegraph.service.query.GremlinQueryService;
import org.springframework.beans.factory.annotation.Autowired;

public class GremlinQueryExecutor {

    @Autowired
    private GremlinQueryService queryService;

    public GremlinResult executeGremlinQuery(int connId, String gremlinQueryContent) {
        GremlinQuery query = new GremlinQuery();
        query.setContent(gremlinQueryContent);

        GremlinResult result = queryService.executeQuery(connId, query);
        // Process the result as needed
        return result;
    }
}

In this example, you need to create a GremlinQuery object, set the Gremlin query content, and then call the executeQuery method with the connection ID and the query. The result will be a GremlinResult object that you can process as needed [1].

Details

About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

dosubot[bot] avatar May 29 '24 03:05 dosubot[bot]

Due to the lack of activity, the current issue is marked as stale and will be closed after 20 days, any update will remove the stale label

github-actions[bot] avatar Jun 13 '24 21:06 github-actions[bot]