27.6.1.1 KEYS_AS_IDS Strategy
KEYS_AS_IDS strategy implies that IDs are based on
user-provided keys. In this case, IDs must be globally unique across the entire
graph.
The following example creates a partitioned graph using the
createGraphBuilder API with USER_IDS generation
strategy for vertices and edges. Also, the graph is configured to retain vertex and edge
IDs without partitioning them.
opg4j> var builder = session.createGraphBuilder()
opg4j> builder.setVertexIdGenerationStrategy(IdGenerationStrategy.USER_IDS). // Using user-provided IDs for vertices
setEdgeIdGenerationStrategy(IdGenerationStrategy.USER_IDS). // Using user-provided IDs for edges
setRetainVertexIds(true). // Retain user-provided vertex IDs (enabled by default).
setRetainEdgeIds(true). // Retain user-provided edge IDs (enabled by default).
partitionedVertexIds(false). // Do not partition vertex IDs (disabled by default).
partitionedEdgeIds(false) // Do not partition edge IDs (disabled by default).
opg4j> builder.addVertex(1).addLabel("Persons").setProperty("name", "Alice")
opg4j> builder.addVertex(2).addLabel("Persons").setProperty("name", "Bob")
opg4j> builder.addEdge(1, 1, 2).setLabel("Knows") // (edge ID, source vertex ID, destination vertex ID)
opg4j> var graph = builder.build("Graph_KeysAsIds")
GraphBuilder<Integer> builder = session.createGraphBuilder();
builder.setVertexIdGenerationStrategy(IdGenerationStrategy.USER_IDS) // Using user-provided IDs for vertices
.setEdgeIdGenerationStrategy(IdGenerationStrategy.USER_IDS) // Using user-provided IDs for edges
.setRetainVertexIds(true) // Retain user-provided vertex IDs (enabled by default)
.setRetainEdgeIds(true) // Retain user-provided edge IDs (enabled by default)
.partitionedVertexIds(false) // Do not partition vertex IDs (disabled by default)
.partitionedEdgeIds(false); // Do not partition edge IDs (disabled by default)
builder.addVertex(1).addLabel("Person").setProperty("name", "Alice");
builder.addVertex(2).addLabel("Person").setProperty("name", "Bob");
builder.addEdge(1, 1, 2).setLabel("Knows"); // (edge ID, source vertex ID, destination vertex ID)
PgxGraph graph = builder.build("Graph_KeysAsIds");>>> builder = session.create_graph_builder(vertex_id_generation_strategy='user_ids',
... edge_id_generation_strategy='user_ids')
>>> builder.set_retain_vertex_ids(True)
>>> builder.set_retain_edge_ids(True)
>>> builder.partitioned_edge_ids(False)
>>> builder.partitioned_vertex_ids(False)
>>> builder.add_vertex(1).add_label("Persons").set_property("name", "Alice")
>>> builder.add_vertex(2).add_label("Persons").set_property("name", "Bob")
>>> builder.add_edge(1, 2, 1).set_label("knows") #(source vertex ID, destination vertex ID, edge ID)
>>> graph = builder.build(name="Graph_KeysAsIds")As seen in the preceding example, the vertex IDs 1 and
2 are globally unique across the graph. Also, the edge ID is
1, which is unique in the graph.
The following shows an example for querying the graph created in the previous example:
opg4j> var alice = graph.getVertex(1) // Retrieves the vertex with ID 1 (Alice)
opg4j> var knows = graph.getEdge(1) // Retrieves the edge with ID 1PgxVertex<Integer> alice = graph.getVertex(1); // Retrieves the vertex with ID 1 (Alice)
PgxEdge<Integer> knows = graph.getEdge(1); // Retrieves the edge with ID 1graph.get_vertex(1) # Retrieves the vertex with ID 1 (Alice)
graph.get_edge(1) # Retrieves the edge with ID 1