def upsert_edge(t, row, **kwargs): mappings = kwargs['mappings'] label = kwargs['label'] if 'label' in kwargs else mappings.get_label(row) on_upsert = kwargs.get('on_upsert', None) #updateSingleCardinalityProperties #updateAllProperties #replaceAllProperties create_traversal = __.addE(label).from_(V(mappings.get_from(row))).to( V(mappings.get_to(row))).property(id, mappings.get_id(row)) if not on_upsert: for key, value in row.items(): mapping = mappings.mapping_for(key) if not mapping.is_token(): create_traversal = create_traversal.property( mapping.name, mapping.convert(value)) t = t.V(mappings.get_from(row)).outE(label).hasId( mappings.get_id(row)).fold().coalesce(__.unfold(), create_traversal) if on_upsert and on_upsert in [ 'updateSingleCardinalityProperties', 'updateAllProperties' ]: for key, value in row.items(): mapping = mappings.mapping_for(key) if not mapping.is_token(): t = t.property(mapping.name, mapping.convert(value)) return t
from gremlin_python.process.graph_traversal import __ from gremlin_python.structure.graph import Vertex graph_name = 'modern' ep_schema = GraphExecutionProfile(graph_options=GraphOptions(graph_name=graph_name)) ep = DseGraph.create_execution_profile(graph_name) cluster = Cluster(execution_profiles={'schema': ep_schema, EXEC_PROFILE_GRAPH_DEFAULT: ep}) session = cluster.connect() # Define schema session.execute_graph("system.graph(name).create()", { 'name': graph_name }, execution_profile = EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT) session.execute_graph("schema.propertyKey('neighborhood').Bigint().create()", execution_profile = 'schema') session.execute_graph("schema.propertyKey('name').Text().create()", execution_profile = 'schema') session.execute_graph("schema.propertyKey('age').Bigint().create()", execution_profile = 'schema') session.execute_graph("schema.propertyKey('weight').Float().create()", execution_profile = 'schema') session.execute_graph("schema.vertexLabel('person').partitionKey('neighborhood').clusteringKey('name').properties('age').create()", execution_profile = 'schema') session.execute_graph("schema.edgeLabel('knows').properties('weight').connection('person', 'person').create()", execution_profile = 'schema') # Execute batch batch = DseGraph.batch() batch.add(__.addV('person').property('neighborhood', 0).property('name', 'bob').property('age', 23)) batch.add(__.addV('person').property('neighborhood', 0).property('name', 'alice').property('age', 21)) batch.add(__.addE('knows') .from_(Vertex({ 'neighborhood': 0, 'name': 'bob', '~label' : 'person' })) .to(Vertex({ 'neighborhood': 0, 'name': 'alice', '~label' : 'person' })) .property('weight', 2.3)) session.execute_graph(batch.as_graph_statement()) cluster.close()
def get_or_create_edge(self, v1, v2, label: str): return self.g.V(v1).as_('v1').V(v2).coalesce( __.inE(label).where(__.outV().as_('v1')), __.addE(label).from_('v1'))
def _get_or_created_edge_to(self, node: GraphTraversal, other: int, label: str): return node.coalesce( __.outE(label).filter(__.inV().hasId(other)), __.addE(label).to(self.g.V(other)))