插入操作

4.1 插入点

  • Connection 对象提供了 insertNode方法来插入点。

  • 使用 uql 插入点

    # 普通插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    ret = conn.insertNode(ULTIPA_REQUEST.InsertNode(nodes = [{'name':'test'}]))
    print(ret.toJSON())
    #插入成功 会返回点的id
    
    # 覆盖插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    ret = conn.insertNode(ULTIPA_REQUEST.InsertNode(nodes=[{'_id': 1,'name':'overwrite'}],overwrite=True))
    print(ret.toJSON())
    #插入成功 会返回点的id
    
    # 更新插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    ret = conn.insertNode(ULTIPA_REQUEST.InsertNode(nodes=     [{'_id':1,'name':'upsert'}],upsert=True))
    print(ret.toJSON())
    #插入成功 会返回点的id
    
  • 批量插入点

    # 普通插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    rows = [{"name": 'test'}]
    # 注意:批量导入必须保证每个字典的[键]都是一样的
    ret = conn.insertNodesBulk(ULTIPA_REQUEST.InsertNodeBulk(rows=rows))
    print(ret.toJSON())
    #插入成功会返回点的id
    
    # 使用静默插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    rows = [{"name": 'test'}]
    # 注意:批量导入必须保证每个字典的[键]都是一样的
    ret = conn.insertNodesBulk(ULTIPA_REQUEST.InsertNodeBulk(rows=rows))
    print(ret.toJSON())
    #插入成功不会返回点的id
    
    # 覆盖插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    rows = [{'_id':1,"name": 'overwrite'}]
    ret = conn.insertNodesBulk(ULTIPA_REQUEST.InsertNodeBulk(rows=rows,overwrite=True))
    print(ret.toJSON())
    
    # 更新插入
    from ultipa import Connection,ULTIPA_REQUEST
    rows = [{'_id':1,"name": 'upsert'}]
    ret = conn.insertNodesBulk(ULTIPA_REQUEST.InsertNodeBulk(rows=rows,upsert=True))
    print(ret.toJSON())
    

4.2 插入边

  • Connection 对象提供了 insertEdge 方法来插入边。插入边的时候需要指定 _from_id 和 _to_id 。

  • 使用 uql 插入边

    # 普通插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    ret = conn.insertEdge(ULTIPA_REQUEST.InsertEdge(
      edges =[{'_from_id':1,'_to_id':2,'name':'test'}]))
    print(ret.toJSON())
    
    # 覆盖插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    ret = conn.insertEdge(ULTIPA_REQUEST.InsertEdge(
      edges=[{"_from_id": 1,"_to_id":2,'name':'overwrite'}],overwrite=True))
    print(ret.toJSON())
    
    # 更新插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    ret = conn.insertEdge(ULTIPA_REQUEST.InsertEdge(
      edges=[{"_from_id": 1,"_to_id":2,'name':'overwrite'}],upsert=True))
    print(ret.toJSON())
    
  • 批量插入边

    # 普通插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    results = [{"_from_id": 1,"_to_id":2,"name": 'Help'}]
    # 注意:批量导入必须保证每个字典的[键]都是一样的
    ret = conn.insertEdgesBulk(ULTIPA_REQUEST.InsertEdgeBulk(rows=results))
    print(ret.toJSON())
    #插入成功会返回边的id
    
    # 使用静默插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    results = [{"_from_id": 1,"_to_id":2,"name": 'Help'}]
    # 注意:批量导入必须保证每个字典的[键]都是一样的
    ret = conn.insertEdgesBulk(ULTIPA_REQUEST.InsertEdgeBulk(rows=results,silent=True))
    print(ret.toJSON())
    #插入成功不会返回边的id
    
    # 覆盖插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    results = [{"_from_id": 1,"_to_id":2,"name": 'overwrite'}]
    # 注意:批量导入必须保证每个字典的[键]都是一样的
    ret = conn.insertEdgesBulk(ULTIPA_REQUEST.InsertEdgeBulk(rows=results,overwrite=True))
    print(ret.toJSON())
    
    #更新插入
    from ultipa import Connection,ULTIPA_REQUEST
    conn = Connection(host='host:port', username="root", password="root")
    results = [{"_from_id": 1,"_to_id":2,"name": 'upsert'}]
    # 注意:批量导入必须保证每个字典的[键]都是一样的
    ret = conn.insertEdgesBulk(ULTIPA_REQUEST.InsertEdgeBulk(rows=results,upsert=True))
    print(ret.toJSON())