From 1f8da94a976ef26cc36ae91644b93d7ed00a6072 Mon Sep 17 00:00:00 2001 From: Karandeep Singh <35473399+kdsgambhir@users.noreply.github.com> Date: Fri, 22 Jun 2018 00:41:06 -0400 Subject: [PATCH 1/2] Update simple.py Updated methods in example to find uid of a node and Deleting it. --- examples/simple/simple.py | 83 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/examples/simple/simple.py b/examples/simple/simple.py index 20663c7..375653d 100644 --- a/examples/simple/simple.py +++ b/examples/simple/simple.py @@ -23,6 +23,7 @@ def drop_all(client): def set_schema(client): schema = """ name: string @index(exact) . + friend: uid @reverse . age: int . married: bool . loc: geo . @@ -78,11 +79,45 @@ def create_data(client): print('All created nodes (map from blank node names to uids):') for uid in assigned.uids: print('{} => {}'.format(uid, assigned.uids[uid])) - print() finally: # Clean up. Calling this after txn.commit() is a no-op # and hence safe. txn.discard() + print('\n') + + +#Deleting a data +def delete_data(client): + # Create a new transaction. + txn = client.txn() + try: + query1 = """query all($a: string) + { + all(func: eq(name, $a)) + { + uid + } + }""" + variables1 = {'$a': 'Bob'} + res1 = client.query(query1, variables=variables1) + ppl1 = json.loads(res1.json) + for person in ppl1['all']: + print('Query to find Uid for Bob :') + print(query1) + print('\n') + print("Bob's UID : ") + print(person) + print('\n') + print('Bob deleted') + print('\n') + + + assigned = txn.mutate(del_obj= person) + + txn.commit() + + finally: + txn.discard() # Query for data. @@ -112,8 +147,48 @@ def query_data(client): # Print results. print('Number of people named "Alice": {}'.format(len(ppl['all']))) + print('\n') for person in ppl['all']: + print('Query for Alice : \n' +query) + print('\n') + print('Result :') + print(person) + print('\n') + +#Query to check for deleted node +def query_data01(client): + query01 = """query all($b: string) + { all(func: eq(name, $b)) + { uid, + name, + age + friend + { + uid, + name, + age + } + ~friend + { + uid, + name, + age + } + } + }""" + + variables01 = {'$b': 'Bob'} + res01 = client.query(query01, variables=variables01) + ppl01 = json.loads(res01.json) + + print('Number of people named "Bob": {}'.format(len(ppl01['all']))) + print('\n') + for person in ppl01['all']: + print('Query for Bob :\n' + query01) + print('\n') + print('Result :') print(person) + print('\n') def main(): @@ -122,7 +197,11 @@ def main(): drop_all(client) set_schema(client) create_data(client) - query_data(client) + query_data(client) # query for Alice + query_data01(client) # query for Bob + delete_data(client) # delete Bob + query_data(client) # query for Alice + query_data01(client) # query for Bob # Close the client stub. client_stub.close() From 7164512ea55ab7fe19677893a42f5dbe36c5e45d Mon Sep 17 00:00:00 2001 From: Karandeep Singh <35473399+kdsgambhir@users.noreply.github.com> Date: Fri, 22 Jun 2018 16:26:23 -0400 Subject: [PATCH 2/2] Update README.md Doc Update for Delete Node --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index a7a1f30..fa93cdb 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,27 @@ txn.mutate(set_obj=p) # txn.mutate(set_nquads='_:alice "Alice"') ``` +```python +# Delete data. + +query1 = """query all($a: string) + { + all(func: eq(name, $a)) + { + uid + } + }""" + +variables1 = {'$a': 'Bob'} + +res1 = client.query(query1, variables=variables1) + +ppl1 = json.loads(res1.json) + +#For mutation to delete node, use this: +txn.mutate(del_obj= person) +``` + For a more complete example with multiple fields and relationships, look at the [simple] project in the `examples` folder.