def run(): csvpath = "equinox.csv" key = os.environ['TERMINUSDB_ACCESS_TOKEN'] endpoint = os.environ['TERMINUSDB_ENDPOINT'] team = os.environ['TERMINUSDB_TEAM'] team_quoted = urllib.parse.quote(team) #client = WOQLClient(f"https://cloud.terminusdb.com/{team_quoted}/") if endpoint == 'http://127.0.0.1:6363': client = WOQLClient(f"{endpoint}") else: client = WOQLClient(f"{endpoint}/{team_quoted}/") # make sure you have put the token in environment variable # https://docs.terminusdb.com/v10.0/#/terminusx/get-your-api-key # print(f"key: {key}") use_token = True if key == 'false': use_token = False client.connect(user='******', team=team, use_token=use_token) else: client.connect(team=team, use_token=use_token) # use when not recreating # client.connect(db=dbid,team=team,use_token=use_token) exists = client.get_database(dbid) if exists: client.delete_database(dbid, team=team) client.create_database(dbid, team, label=label, description=description, prefixes=prefixes) schema = infer_schema(csvpath) schema_objects = basic_schema + schema.dump_schema() #print(json.dumps(schema_objects, indent=4)) import_schema(client,schema_objects) objects = load_data(csvpath,schema) # print(json.dumps(objects, indent=4)) import_data(client,objects) # If you need to start over with connections... # delete_relationships(client) connect_polities(client,csvpath)
#!/usr/bin/python3 import os import time import datetime from terminusdb_client import WOQLClient from terminusdb_client import WOQLQuery as WOQL # Connect to the server server_url = "https://127.0.0.1:6363" db = "dbpedia_1" user = "******" account = "admin" key = "root" client = WOQLClient(server_url) client.connect(user=user, account=account, key=key, db=db) # Example of a single hop query = WOQL().limit( 1, WOQL.woql_and( WOQL().path("doc:Whitesnake", "scm:bandMember", "v:Member", "v:Path"), )) result = client.query(query) print(f"Band member: {result['bindings'][0]['Member']}") query = WOQL().limit( 1, WOQL.woql_and( WOQL().path("doc:Whitesnake", "scm:bandMember,scm:birthPlace", "v:Place", "v:Path"), ))
description="The Document object in SpaCy") token_dt = (WOQLQuery().doctype( "SpaCyToken", label="SpaCy Tokens").property("lemma", "string").property( "pos", "string").property("tag", "string").property( "dep", "string").property("shape", "string").property( "is_alpha", "boolean").property("is_stop", "boolean").property( "head", "SpaCyToken").property("doc", "SpaCyDoc")) schema = spacy_doc + token_dt return schema.execute(client, "Create a schema for SpaCy Tokens") if __name__ == "__main__": db_id = "nlp_spacy" client = WOQLClient(server_url="http://localhost:6363") client.connect(key="root", account="admin", user="******") existing = client.get_metadata(db_id, client.uid()) if not existing: client.create_database( db_id, accountid="admin", label="Spacy Tokens", description="Storing tokenization result form SpaCy") else: client.delete_database( db_id) # delete the original database if the sechema is updated client.create_database( db_id, accountid="admin", label="Spacy Tokens",
from terminusdb_client import WOQLClient from terminusdb_client.woqlschema import WOQLSchema # For Terminus X, use the following # client = WOQLClient("https://cloud.terminusdb.com/<Your Team>/") # client.connect(db="demo_workshop", team="<Your Team>", use_token=True) client = WOQLClient("http://127.0.0.1:6363/") client.connect(db="getting_started", branch="contractors") data_schema = WOQLSchema() data_schema.from_db(client) Employee = data_schema.object.get("Employee") Address = data_schema.object.get("Address") Team = data_schema.object.get("Team") # Contractor 1 rhys_address = Address(postcode="DG4 2ZQ", street="Helland Bridge", street_num=1, town="Ulzieside") rhys = Employee( _id="Employee/006", name="Rhys Arnold", title="UX Designer", team=Team.it, contact_number="078 3951 7569", address=rhys_address,
from terminusdb_client import WOQLClient from terminusdb_client import WOQLQuery as Q DB = WOQLClient("https://127.0.0.1:6363", insecure=True) DB.connect(user="******", account="admin", key="root", db="TodoMVC", insecure=True) from fastapi import FastAPI, HTTPException, Response, status from fastapi.responses import JSONResponse from typing import List, Dict from pydantic import BaseModel app = FastAPI() class Todo(BaseModel): id: str title: str completed: bool links: Dict class State(BaseModel): todos: List[Todo] links: Dict
# Now I want to change up item 1 new_item_1 = { "_id": "U1IT00001", "item_name": "Blender", "max_discount": "50%", "batch_number": "RR450020FRG", "price": 450, "category": "kitchen appliance" } # But before we update it in Mongo, I want to review the changes first # Create a TerminusX client (see https://dashboard.terminusdb.com/profile) tbd_endpoint = WOQLClient("http://localhost:6363/") # Find the item back from database in case someone already changed it item_1 = collection_name.find_one({"item_name": "Blender"}) patch = tbd_endpoint.diff(item_1, new_item_1) pprint(patch.content) # If we apprive, then proceed collection_name.update_one(patch.before, {"$set": patch.update}) # Working on more complicated objects expiry_date = '2021-07-15T00:00:00.000' expiry = dt.datetime.fromisoformat(expiry_date) new_item_3 = {
commit_res = client.squash('This is a squash commit message!',"username") # reset to the squash commit client.reset(commit_res, use_path=True) # Rebase the current branch onto the specified remote branch # more info: https://terminusdb.github.io/terminusdb-client-python/woqlClient.html#terminusdb_client.WOQLClient.rebase client.rebase("main") if __name__ == "__main__": db_id = "Netflix" url = "netflix.csv" # TODO: change the team name team = "<TEAM_NAME>" client = WOQLClient("https://cloud.terminusdb.com/"+team) try: client.connect(team=team, use_token=True) client.create_database(db_id, label = "Netflix Graph", description = "Create a graph with Netflix data") except Exception: client.connect(db=db_id, team=team, use_token=True) schema.commit(client, commit_msg = "Adding Netflix Schema") insert_content_data(client, url) contents = client.query_document({"@type" : "Content"}, count=50) insert_user_data(list(contents))
from terminusdb_client import WOQLQuery as WQ # Assumes database already exists. # openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt server_url = "https://127.0.0.1:6363" #server_url = "https://195.201.12.87:6366" db = "dbpedia" db_label = "DBpedia" db_comment = "Mapped object, property and types from DBpedia's core dataset" user = "******" account = "admin" key = "root" branches = ["literals", "types", "objects", "geo"] client = WOQLClient(server_url, insecure=True) client.connect(user=user, account=account, key=key, db=db, insecure=True) try: client.delete_database(db) except Exception as E: print(E) client.create_database(db, account, label=db_label, description=db_comment, include_schema=False) for branch in branches: # Load the perties branch
#!/usr/bin/python3 from terminusdb_client import WOQLClient from terminusdb_client import WOQLQuery as WQ import csv # Place the snippet from TerminusX here: # TerminusX db = "stock_index" client = WOQLClient(endpoint) client.connect(account=team,user=user,key=key) exists = client.get_database(db) if not exists: client.create_database(db, team, "Stock Exchange Index Data", "Data From Indexes", { '@base' : "terminusdb:///stock_index/", '@schema' : "terminusdb:///stock_index/schema#" }, True) schema = [ { '@type' : 'Class', '@id' : 'IndexRecord', '@key' : { '@type' : 'ValueHash' }, 'index' : 'xsd:string', 'date' : 'xsd:date', 'open' : 'xsd:decimal', 'high' : 'xsd:decimal', 'low' : 'xsd:decimal',
"Year", "v:year_clean").property( "Runtime", "v:runtime_clean").property( "Rating", "v:rating_clean").property( "Votes", "v:votes_clean")) data_prep = WOQLQuery().woql_and(read_csv, prepare_genre_obj, prepare_actor_obj, prepare_director_obj, wangles) return WOQLQuery().woql_and(data_prep, insert).execute( client, "Loading data for the movie graph") db_id = "movie_graph" client = WOQLClient("https://localhost:6363", insecure=True) client.connect(key="root", account="admin", user="******") existing = client.get_metadata(db_id, client.uid()) if not existing: client.create_database(db_id, accountid="admin", label="Graph of IMDB Movies Data", description="Create a graph with IMDB movies data") else: client.set_db(db_id) create_schema(client) result = loading_data( client, "https://raw.githack.com/terminusdb/terminus-tutorials/master/movies-data/IMDB-Movie-Data.csv"
from pprint import pprint from terminusdb_client import WOQLClient, DocumentTemplate, WOQLSchema # create a client endpoint client = WOQLClient("http://localhost:6363/") ### Comparing a document object ### class Person(DocumentTemplate): name: str age: int jane = Person(name="Jane", age=18) janine = Person(name="Janine", age=18) result_patch = client.diff(jane, janine) pprint(result_patch.content) # apply result patch to get back final document after_patch = client.patch(jane, result_patch) pprint(after_patch) assert after_patch == janine._obj_to_dict() ### Comapring document objects in json (dict) formats
index = 0 for code in codes: matches.append(generateMatchClause(code, type, index)) inserts.append(generateInsertClause(code, type, index)) index = index + 1 return WOQLQuery().when(WOQLQuery().woql_and(*matches), WOQLQuery().woql_and(*inserts)) def load_data(client): """Load the Sample Data Parameters ========== client : a WOQLClient() connection """ codes = ["DUB", "LHR", "ETC", "XXX"] q = generateMultiInsertQuery(codes, "Airport") #print(json.dumps(q.json(), indent=4)) q.execute(client) client = WOQLClient(server_url="https://127.0.0.1:6363") client.connect(key="root", account="admin", user="******") existing = client.get_metadata(dbId, client.uid()) if not existing: client.create_database(dbId, "admin", label="Airplane Graph") else: client.db(dbId) create_schema(client) load_data(client)
.property("Director", "v:director_objid") .property("Cast", "v:actors_objid") .property("MovieGenre", "v:genre_objid") .property("Year", "v:year_clean") .property("Runtime", "v:runtime_clean") .property("Rating", "v:rating_clean") .property("Votes", "v:votes_clean") ) data_prep = WOQLQuery().woql_and(read_csv, prepare_genre_obj, prepare_actor_obj, prepare_director_obj, wangles) return WOQLQuery().woql_and(data_prep, insert).execute(client, "Loading data for the movie graph") db_id = "movie_graph" client = WOQLClient("https://127.0.0.1:6363") client.connect(key="root", account="admin", user="******") existing = client.get_metadata(db_id, client.uid()) if not existing: client.create_database(db_id, accountid="admin", label = "Graph of IMDB Movies Data", description = "Create a graph with IMDB movies data") else: client.set_db(db_id) create_schema(client) result = loading_data(client, "https://raw.githack.com/terminusdb/terminus-tutorials/master/movies-data/IMDB-Movie-Data.csv") # result_df = query_to_df(result) # print(result) # print(result_df.columns) # print("----------------") # print(result_df["genre_objid"])