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

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

Open
SkrDrag opened this issue May 17, 2024 · 5 comments
Open

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

SkrDrag opened this issue May 17, 2024 · 5 comments
Labels
feature New feature question Further information is requested

Comments

@SkrDrag
Copy link

SkrDrag commented May 17, 2024

Feature Description (功能描述)

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

@SkrDrag SkrDrag added the feature New feature label May 17, 2024
Copy link

dosubot bot commented May 17, 2024

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.

@dosubot dosubot bot added the question Further information is requested label May 17, 2024
@SkrDrag
Copy link
Author

SkrDrag commented May 28, 2024

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是否能够同时查到点的邻边和另一端顶点的属性

Copy link

dosubot bot commented May 28, 2024

要根据点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
Copy link
Author

SkrDrag commented May 31, 2024

要根据点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对象。

Copy link

dosubot bot commented May 31, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant