<jupyter_start><jupyter_text>Running Azure Cosmos GremlinI've built a lot of my own helper functions to make queries and manipulate data. I'll document them hereIt isim.  First, I'm only using `nest_asyncio` to run the queries in cells. This is a requirement of how gremlinpython manages requests.<jupyter_code>import sys
import pandas as pd
sys.path.append('..')
import helpers.dbquery as db

import yaml, ssl, asyncio

ssl._create_default_https_context = ssl._create_unverified_context
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
import nest_asyncio
# this is required for running in a Jupyter Notebook. 
nest_asyncio.apply()
db.run_query()<jupyter_output><empty_output><jupyter_text>the method `run_query` takes a query string and returns the json response. It manages the opening and closing of objects so you don't have to.<jupyter_code>res = db.run_query("g.V().hasLabel('system').has('username','userbill').limit(4).in().inE('orbits').limit(2)")
res<jupyter_output><empty_output><jupyter_text>This makes a great `dataframe`<jupyter_code>df = pd.DataFrame(db.run_query("g.V().hasLabel('system').has('username','userbill').in().inE('orbits')"))
df.head()<jupyter_output><empty_output><jupyter_text>Adding Vertices and Edges

I've added some functions that create the `g.addV()` command with all of the properties I want. This also forces some properties like `username` and `objtype` so I don't have to think about them. The function `create_vertex` just returns the string.

**Note:** I'm using objtype as an extra lable as I found the query to be slightly cheaper when querying that value (e.g. `valueMap()`)than returning the actual lable maps (e.g. `valueMap(true)`).

In the game, I'm adding `username` for the user's account so that I can easily filter data for that user. However in notebooks I've replaced the user account with `notebook` so that I can cleanup notebook runs easily.  This `username` is forced to `notebook`, so I can quickly cleanup. 

The node must include a label<jupyter_code>nodes = [{"label":"example","objid":db.uuid(),"property1":"foo","property2":"bar"},
        {"label":"example","objid":db.uuid(),"property1":"foo","property2":"bar"}]
[db.create_vertex(node) for node in nodes]<jupyter_output><empty_output><jupyter_text>edges reuqure a `node1` and a `node2` object.<jupyter_code>edge = {"label":"isExample","node1":nodes[0]["objid"],"node2":nodes[1]["objid"]}
db.create_edge(edge)<jupyter_output><empty_output><jupyter_text>The method `upload_data` requires a dict of both edges and nodes<jupyter_code>data = {'nodes':nodes,'edges':[edge]}
db.upload_data(data)<jupyter_output><empty_output><jupyter_text>Once you have the values you can query them back, to confirm they are in the API.<jupyter_code>res = db.run_query("g.V().has('username','notebook').valueMap()")
res<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12690707350901526, 'fraction_numerical': 0.004160887656033287, 'mean_word_length': 6.632275132275132, 'pattern_counts': {'":': 11, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '25971', 'license': 'MIT', 'n_tokens_mistral': 941, 'n_tokens_neox': 916, 'n_words': 279, 'path': 'notebooks/How I do gremlin.ipynb', 'repo_name': 'BillmanH/opensocial', 'size': '2884'}