apache / incubator-hugegraph

A graph database that supports more than 100+ billion data, high performance and scalability (Include OLTP Engine & REST-API & Backends)
https://hugegraph.apache.org
Apache License 2.0
2.58k stars 512 forks source link

怎么访问边和顶点的属性? #2539

Open SkrDrag opened 1 month ago

SkrDrag commented 1 month ago

Feature Description (功能描述)

通过Iterator edges = edgesOfVertex(source, dir,label, degree);得到边迭代器后,怎么访问每一条边和顶点的属性值,是字符类型吗

dosubot[bot] commented 1 month ago

Thank you for your question. Please keep the issue title in English at least and maintain the basic [Question] xxx format. Thank you for your understanding

To access the properties of edges and vertices after obtaining an edge iterator with Iterator<Edge> edges = edgesOfVertex(source, dir, label, degree);, follow these steps:

  1. Edge Attributes:

    • To set a property, use edge.property("key", "value"); where "key" is the property name and "value" is the property value.
    • To get a property, if there's a method to access properties, use it to retrieve the value by the property name, e.g., String value = (String) edge.properties().get("propertyName");.
  2. Vertex Attributes:

    • Accessing vertex attributes directly from an edge requires you to first get the vertex object. The edge object provides methods like sourceId() and targetId() to get vertex identifiers, but to access vertex properties, you'll need to use the vertex object itself, which is not detailed in your snippet.
  3. Attribute Types:

    • Attributes can be of any type, not just String. When you set or get a property, it's treated as an Object. Ensure to cast it to the correct type when retrieving.

This approach assumes you're working with the HugeGraph toolchain. For specific implementations or methods to access properties directly, refer to the documentation or source code of the HugeGraph client library you're using.

#### Details **Sources** - [hugegraph-client/src/main/java/org/apache/hugegraph/structure/graph/Edge.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-client/src/main/java/org/apache/hugegraph/structure/graph/Edge.java) - [hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeEdge.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeEdge.java) - [hugegraph-client/src/main/java/org/apache/hugegraph/structure/graph/Graph.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-client/src/main/java/org/apache/hugegraph/structure/graph/Graph.java) - [hugegraph-client/src/main/java/org/apache/hugegraph/example/BatchExample.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-client/src/main/java/org/apache/hugegraph/example/BatchExample.java) - [content/en/docs/language/hugegraph-example.md](https://github.com/apache/incubator-hugegraph-doc/blob/master/content/en/docs/language/hugegraph-example.md) - [hugegraph-client/src/main/java/org/apache/hugegraph/example/SingleExample.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-client/src/main/java/org/apache/hugegraph/example/SingleExample.java) - [content/en/docs/quickstart/hugegraph-client.md](https://github.com/apache/incubator-hugegraph-doc/blob/master/content/en/docs/quickstart/hugegraph-client.md) - [hugegraph-server/hugegraph-example/src/main/java/org/apache/hugegraph/example/Example1.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-example/src/main/java/org/apache/hugegraph/example/Example1.java) - [hugegraph-client/src/main/java/org/apache/hugegraph/structure/graph/Vertex.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-client/src/main/java/org/apache/hugegraph/structure/graph/Vertex.java) - [hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeVertex.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeVertex.java) - [hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/schema/PropertyKeyService.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/schema/PropertyKeyService.java)
**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.

SkrDrag commented 1 month ago

Thank you for your question. Please keep the issue title in English at least and maintain the basic [Question] xxx format. Thank you for your understanding

To access the properties of edges and vertices after obtaining an edge iterator with Iterator<Edge> edges = edgesOfVertex(source, dir, label, degree);, follow these steps:

  1. Edge Attributes:

    • To set a property, use edge.property("key", "value"); where "key" is the property name and "value" is the property value.
    • To get a property, if there's a method to access properties, use it to retrieve the value by the property name, e.g., String value = (String) edge.properties().get("propertyName");.
  2. Vertex Attributes:

    • Accessing vertex attributes directly from an edge requires you to first get the vertex object. The edge object provides methods like sourceId() and targetId() to get vertex identifiers, but to access vertex properties, you'll need to use the vertex object itself, which is not detailed in your snippet.
  3. Attribute Types:

    • Attributes can be of any type, not just String. When you set or get a property, it's treated as an Object. Ensure to cast it to the correct type when retrieving.

This approach assumes you're working with the HugeGraph toolchain. For specific implementations or methods to access properties directly, refer to the documentation or source code of the HugeGraph client library you're using.

Details

Sources

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.

根据点Id是否能够同时查到点的邻边和另一端顶点的属性

dosubot[bot] commented 1 month ago

要根据点ID同时查到点的邻边和另一端顶点的属性,可以使用HugeGraph工具链中的HugeVertex类提供的edgesvertices方法。以下是一个示例代码:

import org.apache.hugegraph.structure.HugeEdge;
import org.apache.hugegraph.structure.HugeVertex;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Property;

import java.util.Iterator;

public class HugeGraphExample {

    public static void main(String[] args) {
        // 假设你已经有一个HugeGraph实例和一个点ID
        HugeGraph graph = ExampleUtil.loadGraph();
        String vertexId = "your-vertex-id";

        // 根据点ID获取点
        HugeVertex vertex = (HugeVertex) graph.traversal().V(vertexId).next();

        // 获取与该点相连的边
        Iterator<Edge> edges = vertex.edges(Direction.BOTH);

        while (edges.hasNext()) {
            HugeEdge edge = (HugeEdge) edges.next();

            // 获取边的属性
            Iterator<Property<Object>> edgeProperties = edge.properties();
            while (edgeProperties.hasNext()) {
                Property<Object> property = edgeProperties.next();
                System.out.println("Edge Property: " + property.key() + " = " + property.value());
            }

            // 获取相连的顶点
            Iterator<Vertex> connectedVertices = edge.vertices(Direction.BOTH);
            while (connectedVertices.hasNext()) {
                HugeVertex connectedVertex = (HugeVertex) connectedVertices.next();

                // 获取相连顶点的属性
                Iterator<Property<Object>> vertexProperties = connectedVertex.properties();
                while (vertexProperties.hasNext()) {
                    Property<Object> property = vertexProperties.next();
                    System.out.println("Vertex Property: " + property.key() + " = " + property.value());
                }
            }
        }

        graph.close();
        HugeFactory.shutdown(30L);
    }
}

在这个示例中,ExampleUtil.loadGraph()方法用于加载HugeGraph实例。代码通过点ID获取点,遍历与该点相连的边,并获取边和相连顶点的属性。请确保将"your-vertex-id"替换为你要查询的实际点ID。HugeVertex类中的edgesvertices方法用于获取边和相连顶点[1][2][3][4]

#### 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.

SkrDrag commented 1 month ago

要根据点ID同时查到点的邻边和另一端顶点的属性,可以使用HugeGraph工具链中的HugeVertex类提供的edgesvertices方法。以下是一个示例代码:

import org.apache.hugegraph.structure.HugeEdge;
import org.apache.hugegraph.structure.HugeVertex;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Property;

import java.util.Iterator;

public class HugeGraphExample {

    public static void main(String[] args) {
        // 假设你已经有一个HugeGraph实例和一个点ID
        HugeGraph graph = ExampleUtil.loadGraph();
        String vertexId = "your-vertex-id";

        // 根据点ID获取点
        HugeVertex vertex = (HugeVertex) graph.traversal().V(vertexId).next();

        // 获取与该点相连的边
        Iterator<Edge> edges = vertex.edges(Direction.BOTH);

        while (edges.hasNext()) {
            HugeEdge edge = (HugeEdge) edges.next();

            // 获取边的属性
            Iterator<Property<Object>> edgeProperties = edge.properties();
            while (edgeProperties.hasNext()) {
                Property<Object> property = edgeProperties.next();
                System.out.println("Edge Property: " + property.key() + " = " + property.value());
            }

            // 获取相连的顶点
            Iterator<Vertex> connectedVertices = edge.vertices(Direction.BOTH);
            while (connectedVertices.hasNext()) {
                HugeVertex connectedVertex = (HugeVertex) connectedVertices.next();

                // 获取相连顶点的属性
                Iterator<Property<Object>> vertexProperties = connectedVertex.properties();
                while (vertexProperties.hasNext()) {
                    Property<Object> property = vertexProperties.next();
                    System.out.println("Vertex Property: " + property.key() + " = " + property.value());
                }
            }
        }

        graph.close();
        HugeFactory.shutdown(30L);
    }
}

在这个示例中,ExampleUtil.loadGraph()方法用于加载HugeGraph实例。代码通过点ID获取点,遍历与该点相连的边,并获取边和相连顶点的属性。请确保将"your-vertex-id"替换为你要查询的实际点ID。HugeVertex类中的edgesvertices方法用于获取边和相连顶点[1][2][3][4]

Details

To continue the conversation, mention @dosu.

Iterator connectedVertices = edge.vertices(Direction.BOTH);这里获取与边相连的顶点,为什么返回的是一个迭代器对象,而不是一个vertex对象。

dosubot[bot] commented 1 month ago

edge.vertices(Direction.BOTH); 返回一个迭代器对象而不是单个顶点对象,因为它可能会返回多个顶点。具体来说,当方向是 BOTH 时,它包括边的源顶点和目标顶点。该方法设计用于处理不同的方向(OUTINBOTH),在 BOTH 的情况下,需要返回两个顶点,因此使用迭代器来容纳多个结果。

@Watched(prefix = "vertex")
@Override
public Iterator<Vertex> vertices(Direction direction, String... edgeLabels) {
    Iterator<Edge> edges = this.edges(direction, edgeLabels);
    return this.graph().adjacentVertices(edges);
}

这个代码片段来自 HugeVertex 类,显示了对于 BOTH 方向,该方法检索两个方向的边,然后返回相邻顶点的迭代器。这种设计确保了方法能够高效地处理多个顶点[1][2]

To continue talking to Dosu, mention @dosu.