def __init__(self, host, port, username=None, password=None, ssl=False, timeout=None): port = "7687" if port is None else port bolt_uri = "bolt://{host}".format(host=host, port=port) self.http_uri = "http://{host}:{port}/db/data/".format(host=host, port=port) if username and password: driver = GraphDatabase.driver(bolt_uri, auth=basic_auth(username, password), encrypted=False) else: driver = GraphDatabase.driver(bolt_uri, encrypted=False) self.session = driver.session()
def connect(self, url=None, user=None, password=None, **kw): """ Parse a Neo4J URL and attempt to connect using Bolt Note: If the user and password arguments are provided, they will only be used in case no auth information is provided as part of the connection URL. """ if url is None: url = 'bolt://localhost' if user is None: user = '******' if password is None: password = '******' try: protocol, url = url.split('://') if protocol.lower() != 'bolt': warnings.warn('Switching protocols. Only Bolt is supported.') except ValueError: pass try: credentials, url = url.split('@') except ValueError: kw['auth'] = basic_auth(user, password) else: kw['auth'] = basic_auth(*credentials.split(':', 1)) self.driver = GraphDatabase.driver('bolt://%s' % url, **kw)
def main(): parser = argparse.ArgumentParser(description=""" Insert a Terraform state file into neo4j """) parser.add_argument('-d','--db', required=True, help="Neo4j host") parser.add_argument('-u','--username', required=True, help="Neo4j user") parser.add_argument('-p','--password', required=True, help="Neo4j password") parser.add_argument('state_file', help="Terraform state file") args = parser.parse_args() print args with open(args.state_file, 'r') as f: state = json.load(f) driver = GraphDatabase.driver("bolt://{}".format(args.db), auth=basic_auth(args.username, args.password)) session = driver.session() # Reduce all the modules and resouces to a single array of objects resources = reduce( lambda a,b: a+b, map(lambda m: m['resources'].values(), state['modules'])) # Run actions for resources and capture hooks hooks = set() for resource in resources: hooks.add(insert_item(resource, session)) # Run hooks for hook in hooks: if hook: hook(session)
def run_graph(neo4j_conf, args): opts, args = getopt.getopt(args, "rcsa", ["related", "cluster", "similar", "all"]) if len(args) < 1: raise getopt.GetoptError("Invalid graph arguments") query = args[0] stmt = '' for o, v in opts: if o in ["-r", "--related"]: stmt = ('match (q:Query)<-[r:RELATED]-(a:Query) where q.query={query}' 'return a.query, r.norm_weight order by r.norm_weight desc') elif o in ["-c", "--cluster"]: stmt = ('match (q:Query)<-[r:CLUSTER_REP]-(a:Query) where q.query={query}' 'return r.rank, a.query, r.query_terms order by r.rank') elif o in ["-s", "--similar"]: stmt = ('match (q:Query)-[r:SIMILAR]-(a:Query) where q.query={query}' 'return a.query, r.score order by r.score desc') elif o in ["-a", "--all"]: stmt = ('match (q:Query)<-[r]-() where q.query={query}' 'return q.query, type(r) as rel_type, count(r) as rel_count') if not stmt: raise getopt.GetoptError("Invalid graph arguments") graph = GraphDatabase.driver(neo4j_conf.uri, auth=basic_auth(neo4j_conf.username, neo4j_conf.password)) session = graph.session() rs = session.run(stmt, parameters={'query': query}) for r in rs: pprint.pprint(r)
def main(argv=None): """Import all data in JSON file into Neo4j database.""" parser = argparse.ArgumentParser(description="Load articles into Neo4j") parser.add_argument("file", help="File to read", type=str, nargs="?", metavar="FILE") parser.add_argument("--no-execute", action="store_true") parse_result = parser.parse_args(argv or sys.argv[1:]) with open_or_default(parse_result.file, sys.stdin) as fileobj: data = json.load(fileobj) commands = list(commands_from_data(data)) if parse_result.no_execute: sys.stdout.write(json.dumps(commands)) elif len(commands): if all(var in os.environ for var in ["DATABASE_URL", "DATABASE_PASS"]): url = os.environ["DATABASE_URL"] pwd = os.environ["DATABASE_PASS"] usr = os.environ.get("DATABASE_USER", "") else: raise ValueError("Ensure environment variables DATABASE_URL, " "DATABASE_PASS and DATABASE_USER set.") driver = GraphDatabase.driver(url, auth=basic_auth(usr, pwd)) session = driver.session() for command in commands: session.run(command) session.close()
def test_construct_dwpc_query(): """ Test dwpc query construction and computation on the metapath from https://doi.org/10.1371/journal.pcbi.1004259.g002 """ directory = pathlib.Path(__file__).parent.absolute() path = directory.joinpath('data/hetionet-v1.0-metagraph.json') metagraph = hetio.readwrite.read_metagraph(path) compound = 'DB01156' # Bupropion disease = 'DOID:0050742' # nicotine dependency damping_exponent = 0.4 metapath = metagraph.metapath_from_abbrev('CbGpPWpGaD') query = hetio.neo4j.construct_dwpc_query(metapath, property='identifier', unique_nodes=True) assert len(query) > 0 driver = GraphDatabase.driver("bolt://neo4j.het.io") params = { 'source': compound, 'target': disease, 'w': damping_exponent, } with driver.session() as session: results = session.run(query, params) results = results.single() assert results dwpc = results['DWPC'] assert dwpc == pytest.approx(0.03287590886921623)
def export_to_neo4j(): driver = GraphDatabase.driver("bolt://localhost:7687", encrypted=False, auth=basic_auth("neo4j", "asdzxc")) session = driver.session() for article in db.Article.objects.all(): if article['links']: # session.run("CREATE (a:Article {name: {name}})", # {"name": article['title']}) for link in article['links']: to_article = db.Article.objects.get(id=link) print(to_article['title']) session.run("CREATE (a:Article {name: {name}})", {"name": article['title']}) # # result = session.run("MATCH (a:Person) WHERE a.name = {name} " # "RETURN a.name AS name, a.title AS title", # {"name": "Arthur"}) # for record in result: # print("%s %s" % (record["title"], record["name"])) # session.close()
def __new__(cls, *args, **kwargs): """ Return neo4j-driver ou neo4jrestclient object """ _auth = None if kwargs and ('user' or 'password') in list(kwargs.keys()): user = kwargs['user'] password = kwargs['password'] if 'bolt://' in cls._default_host: _auth = basic_auth(user, password) else: _url = 'http://{0}:{1}@localhost:7474'.format(user, password) cls.host = _url if 'bolt://' in cls._default_host: driver = Neo4j3.driver(cls._default_host) if _auth: driver.auth = _auth cls._graph = Cypher(driver) return cls._graph elif cls.host is not None and type(cls.host) is str: cls._graph = Neo4j2(cls.host) return cls._graph else: cls._graph = Neo4j2(cls._default_host) return cls._graph
def get_session(warehouse_home, server_name, password=None, encrypted=DEFAULT_ENCRYPTED, silence_loggers=DEFAULT_SILENCE_LOGGERS): if silence_loggers: logging.getLogger('neo4j.bolt').setLevel(logging.WARNING) server = neokit.Warehouse(warehouse_home).get(server_name) address = server.config('dbms.connector.bolt.address', 'localhost:7687') server_url = 'bolt://' + address if password: driver = GraphDatabase.driver(server_url, encrypted=encrypted, auth=basic_auth(DEFAULT_USER, password)) else: driver = GraphDatabase.driver(server_url, encrypted=encrypted) session = driver.session() return session
def __init__(self): config = configparser.ConfigParser() config.read('config.ini') user_name = config.get('neo4j credentials', 'user_name') password = config.get('neo4j credentials', 'password') bolt_host = config.get('neo4j credentials', 'bolt_host') self.driver = GraphDatabase.driver(bolt_host, auth=basic_auth(user_name, password))
def init_neo4j_connection(app): server_url = app.config.get('NEO4J_URL', 'bolt://localhost:7687') encrypted = app.config.get('NEO4J_ENCRYPTED', True) user = app.config.get('NEO4J_USER', 'neo4j') password = app.config.get('NEO4J_PASSWORD') auth = basic_auth(user, password) if password else None driver = GraphDatabase.driver(server_url, encrypted=encrypted, auth=auth) app.config['NEO4J_DRIVER'] = driver
def server(ctx, host, port, debug): from . server import app config = ctx.obj.config from neo4j.v1 import GraphDatabase, basic_auth auth = basic_auth(config.neo4j.user, config.neo4j.password) driver = GraphDatabase.driver(config.neo4j.address, auth=auth) from attrdict import AttrDict app.minos = AttrDict({ 'config': config, 'driver': driver }) app.run(host, port, debug)
def __init__(self, **kwargs): #super(Neo4JConn, self).__init__() config = { 'host': kwargs['db_addr'], 'port': kwargs['db_port'], 'user': kwargs['username'], 'password': kwargs['password'] } driver = GraphDatabase.driver( "bolt://%s:%d" % (config['host'], config['port']), auth=basic_auth(config['user'], config['password'])) self.__session = driver.session()
def neo4j(): from neo4j.v1 import GraphDatabase, basic_auth driver = GraphDatabase.driver("bolt://localhost:7474", auth=basic_auth("neo4j", "neo4j")) session = driver.session() session.run("CREATE (a:Person {name:'Arthur', title:'King'})") result = session.run("MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title") for record in result: print("%s %s" % (record["title"], record["name"])) session.close()
def set_connection(self, url): self.url = url u = urlparse(url) if u.netloc.find('@') > -1 and u.scheme == 'bolt': credentials, hostname = u.netloc.rsplit('@', 1) username, password, = credentials.split(':') else: raise ValueError("Expecting url format: bolt://user:password@localhost:7687" " got {}".format(url)) self.driver = GraphDatabase.driver('bolt://' + hostname, auth=basic_auth(username, password)) self.refresh_connection()
def __init__(self, adress, user, password): """ Creates the session to the database """ self.driver = GraphDatabase.driver(adress, \ auth=basic_auth(user, password)) try: self.session = self.driver.session() except ProtocolError: print("Cannot connect to neo4j. Aborting.") exit() print("Connected to neo4j.")
def __init__(self, username = None, password = None, server = None): if username == None or password == None: username, password, server = self.loadAuthCredentials() print(username, password, server) uri = "bolt://{}:{}@{}".format(username, password, server) print("Connecting to " + uri) try: self.conn = GraphDatabase.driver(uri, auth = (username, password)) self.session = self.conn.session() except ServiceUnavailable as e: raise Exception(str(e))
def create_app(): app = Flask(__name__) app.debug = True app.config['SECRET_KEY'] = config['auth_secret'] app.config['JWT_BLACKLIST_ENABLED'] = False app.config['JWT_BLACKLIST_STORE'] = simplekv.memory.DictStore() app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = 'all' app.config['JWT_ACCESS_TOKEN_EXPIRES'] = datetime.timedelta(minutes=15) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER driver = GraphDatabase.driver(config['database_url'], auth=basic_auth(config['database_user'],config['database_pass'])) db_session = driver.session() # start jwt service jwt = JWTManager(app) # Import blueprints from auth import auth_blueprint from banner import banner_blueprint from people import people_blueprint from organizations import organizations_blueprint from repos import repositories_blueprint from schema import schema_blueprint from data import data_blueprint from search import search_blueprint from upload import upload_blueprint from export import export_blueprint from list import list_blueprint from .sockets import sockets as socket_blueprint # register API modules app.register_blueprint(banner_blueprint) app.register_blueprint(auth_blueprint) app.register_blueprint(people_blueprint) app.register_blueprint(organizations_blueprint) app.register_blueprint(repositories_blueprint) app.register_blueprint(schema_blueprint) app.register_blueprint(search_blueprint) app.register_blueprint(data_blueprint) app.register_blueprint(upload_blueprint) app.register_blueprint(socket_blueprint) app.register_blueprint(export_blueprint) app.register_blueprint(list_blueprint) x_socketio.init_app(app) return app, jwt
def set_connection(self, url): self.url = url u = urlparse(url) if u.netloc.find('@') > -1 and u.scheme == 'bolt': credentials, hostname = u.netloc.rsplit('@', 1) username, password, = credentials.split(':') else: raise ValueError("Expecting url format: bolt://user:password@localhost:7687" " got {}".format(url)) self.driver = GraphDatabase.driver('bolt://' + hostname, auth=basic_auth(username, password), encrypted=config.ENCRYPTED_CONNECTION, max_pool_size=config.MAX_POOL_SIZE) self._pid = os.getpid() self._active_transaction = None
def __init__(self, authfile, session_name): self._authfile = authfile self._session_name = session_name self._num_variables = 0 self._num_landmarks = 50 self._pose_to_odometry_or_prior = {} self._pose_to_measurements = {} self._landmark_to_prior = {} self._pose_ids = [] self._max_factor_id = 0 # initialize Neo4j session self.username, self.password, self.DB_address = open(self._authfile).read().splitlines() print self.username, self.password, self.DB_address self.driver = GraphDatabase.driver(self.DB_address, auth=basic_auth(self.username, self.password)) self.session = self.driver.session() self.session.run("MATCH (n:" + self._session_name + ") DETACH DELETE n")
def __init__(self, driver=None, uri=None, user=None, password=None, node_label="node", edge_label="edge", unique_node_ids=True): """Initialize Neo4jGraph object. Parameters ---------- driver : neo4j.v1.direct.DirectDriver, optional Driver providing connection to a Neo4j database uri : str, optional Uri for a new Neo4j database connection (bolt) user : str, optional Username for the Neo4j database connection password : str, optional Password for the Neo4j database connection node_label : optional Label of nodes inducing the subgraph to scope. By default `"node"`. edge_label : optional Type of relations inducing the subgraph to scope. By default `"edge"`. unique_node_ids : bool, optional Flag, if True the uniqueness constraint on the property 'id' of nodes is imposed, by default True If database driver is provided, uses it for connecting to database, otherwise creates a new driver object using provided credentials. """ if driver is None: self._driver = GraphDatabase.driver( uri, auth=(user, password)) else: self._driver = driver self._node_label = node_label self._edge_label = edge_label self.unique_node_ids = unique_node_ids if unique_node_ids: try: self.set_constraint('id') except: warnings.warn( "Failed to create id uniqueness constraint")
def __init__(self, settings_file_name = None, working_directory = None): super().__init__(settings_file_name, working_directory = working_directory) # Read secret data file secret_data_file_name = self.get_setting("secret_data_file_name") with open(os.path.join(self.working_directory, os.path.normpath(secret_data_file_name)), "r") as file_: fileData = file_.read() secret_data = json.loads(fileData) # Initialize the graph database self._db = GraphDatabase.driver("bolt://localhost", auth=basic_auth(secret_data["neo4j_user_name"], secret_data["neo4j_password"])) self.orion_ns = "http://www.orion-research.se/ontology#" # Initialize proxies self.authentication_proxy = self.create_proxy(self.get_setting("authentication_service")) self.ontology = None
def _get_cand_spec(mass, tol): '''Gets candidate spectra.''' cand_spec = [] query = 'MATCH (s:Spectrum)-[]-(c:Chemical)' + \ ' WHERE c.monoisotopic_mass > {start_mass}' + \ ' AND c.monoisotopic_mass < {end_mass}' + \ ' RETURN c, s' driver = GraphDatabase.driver("bolt://localhost") session = driver.session() result = session.run(query, {'start_mass': mass - tol, 'end_mass': mass + tol}) for record in result: cand_spec.append([record['c'], record['s']]) return cand_spec
def handle(self, *args, **options): driver = GraphDatabase.driver(settings.NEO4J_BOLT_URL, auth=basic_auth( settings.NEO4J_USER, settings.NEO4J_PASSWORD) ) session = driver.session() # TODO: Figure out a way to not delete the whole graph db every time # session.run("MATCH (n) DETACH DELETE n") # FIXED by using MERGE/SET statements user_list = [ user[0] for user in User.objects.all().values_list('username') ] # user_list = [ # 'aprilchomp', 'jsatt', 'mrmakeit', 'jgmize', 'groovecoder' # ] repo_types = { 'repositories': 'OWNER', 'starredRepositories': 'STARRED', 'contributedRepositories': "CONTRIBUTED" } for username in user_list: if username == u'admin': continue try: gh_user = ghUser.get(login=username)['user'] neo4j_merge_user(gh_user, session) for repo_type in repo_types.keys(): repos = RepoList(type=repo_type, login=username) for repo in repos: repo_values = repo['node'] neo4j_merge_repo(repo_values, session) neo4j_match_repo_relationship( gh_user['id'], repo_values['id'], repo_types[repo_type], session ) except Exception as e: logger.error("load_user_github_graph, error: %s" % e) session.close()
def setup_gdatabase_conn(): """Function to setup the database connection to the active Neo4j project meant to contain the ODIN data. """ try: database_uri = config_section_map("GraphDatabase")["uri"] database_user = config_section_map("GraphDatabase")["username"] database_pass = config_section_map("GraphDatabase")["password"] click.secho("[*] Attempting to connect to your Neo4j project using {}:{} @ {}." .format(database_user,database_pass,database_uri),fg="yellow") neo4j_driver = GraphDatabase.driver(database_uri,auth=(database_user,database_pass)) click.secho("[+] Success!",fg="green") return neo4j_driver except Exception: neo4j_driver = None click.secho("[!] Could not create a database connection using the details provided in \ your database.config! Please check the URI, username, and password. Also, make sure your Neo4j \ project is running. Note that the bolt port can change.",fg="red") exit()
def _get_db_driver(uri, username=None, password=None, encrypted=True, max_pool_size=50, trust=TRUST_DEFAULT): """ :param uri: Bolt uri :type uri: str :param username: Neo4j username :type username: str :param password: Neo4j password :type password: str :param encrypted: Use TLS :type encrypted: Boolean :param max_pool_size: Maximum number of idle sessions :type max_pool_size: Integer :param trust: Trust cert on first use (0) or do not accept unknown cert (1) :type trust: Integer :return: Neo4j driver :rtype: neo4j.v1.session.Driver """ return GraphDatabase.driver(uri, auth=basic_auth(username, password), encrypted=encrypted, max_pool_size=max_pool_size, trust=trust)
def __init__(self, sessname): self.idx_ = 0 # odom_index self.sessname = sessname ## Authentication and Setup for Neo4j authfile = '/home/dehann/neo_authfile.txt' # username on one line, password on next (for database) un,pw, addr = open(authfile).read().splitlines() self.driver = GraphDatabase.driver(addr, auth=basic_auth(un, pw)) self.session = self.driver.session() self.session.run("MATCH (n:"+self.sessname+") DETACH DELETE n") # REMOVE ALL "+self.sessname+" NODES self.odom_diff = None self.old_odom = None self.odom_node_id = None # neo4j node id ## Authentication/Setup for Mongo mongo_authfile = "/home/dehann/mongo_authfile.txt" maddr = open(mongo_authfile).read().splitlines() print maddr client = MongoClient(maddr) # Default is local for now self.db = client.CloudGraphs # test is the name of the data base
def get_db_client(dbhost, dbuser, dbpass, bolt=False): """Return a Neo4j DB session. bolt=True uses bolt driver""" if verbose > 4: print("DB Creds", dbhost, dbuser, dbpass) if bolt: bolt_url = "bolt://" + dbhost auth_token = basic_auth(dbuser, dbpass) try: driver = GraphDatabase.driver(bolt_url, auth=auth_token, max_pool_size=5) bolt_session = driver.session() return bolt_session except Exception as e: print("Database connection/authentication error:", e) sys.exit(1) else: login = "******".format(dbuser, dbpass, dbhost) py2neo_session = Graph(login) return py2neo_session
def create_database(infile): driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "password1!")) session = driver.session() print "Infile from ingester.py: %s" % infile #cypher query to create unique source MAC address nodes session.run("USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM 'http://localhost:8080/" + infile + "' AS row FIELDTERMINATOR '|' WITH row WHERE row.`eth.src` IS NOT NULL MERGE (n:Node {eth: row.`eth.src`}) ON CREATE SET n.eth=row.`eth.src`") #cypher query to create unique source IP address nodes session.run("USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM 'http://localhost:8080/" + infile + "'AS row FIELDTERMINATOR '|' WITH row WHERE row.`ip.src` IS NOT NULL MERGE (n:IP {ip: row.`ip.src`}) ON CREATE SET n.ip=row.`ip.src`") #cypher query to create unique destination MAC address nodes session.run("USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM 'http://localhost:8080/" + infile + "' AS row FIELDTERMINATOR '|' WITH row WHERE row.`eth.dst` IS NOT NULL MERGE (n:Node {eth: row.`eth.dst`}) ON CREATE SET n.eth=row.`eth.dst`") #cypher query to create unique destination IP address nodes session.run("USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM 'http://localhost:8080/" + infile + "' AS row FIELDTERMINATOR '|' WITH row WHERE row.`ip.dst` IS NOT NULL MERGE (n:IP {ip: row.`ip.dst`}) ON CREATE SET n.ip=row.`ip.dst`") #cypher query to create relationship between source MAC address and source IP address in the PCAP file session.run("USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM 'http://localhost:8080/" + infile + "' AS row FIELDTERMINATOR '|' WITH row WHERE row.`eth.src` IS NOT NULL AND row.`ip.src` IS NOT NULL MATCH (n:Node) MATCH (m:IP) WHERE n.eth=row.`eth.src` AND m.ip=row.`ip.src` MERGE (m)-[:HAS_MAC]->(n)") #cypher query to create relationship between destination MAC address and destination IP address in PCAP file session.run("USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM 'http://localhost:8080/" + infile + "' AS row FIELDTERMINATOR '|' WITH row WHERE row.`eth.src` IS NOT NULL AND row.`ip.src` IS NOT NULL MATCH (n:Node) MATCH (m:IP) WHERE n.eth=row.`eth.dst` AND m.ip=row.`ip.dst` MERGE (m)-[:HAS_MAC]->(n)") #cypher query to draw relationship between source and destination nodes in the PCAP file session.run("USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM 'http://localhost:8080/" + infile + "' AS row FIELDTERMINATOR '|' WITH row WHERE row.`eth.src` IS NOT NULL AND row.`eth.dst` IS NOT NULL MATCH (n:Node) WHERE n.eth=row.`eth.src` MATCH (m:Node) WHERE m.eth=row.`eth.dst` CREATE (n)-[:TALKS_TO {protocol: row.`_ws.col.Protocol`, info: row.`_ws.col.Info`, data: row.`data`, length: row.`frame.len`, srcport: row.`tcp.srcport`, dstport: row.`tcp.dstport`}]->(m)") session.close()
#bob = gdb.nodes.create(name="Bob", age=30) #alice.relationships.create("Knows", bob, since=1980) #people = gdb.labels.create("Person") #============================================================== LOAD CSV ======================= #LOAD CSV WITH HEADERS FROM 'file:///Users/kushal/Desktop/Nepal Data/Municipalities_of_nepal.csv' AS line #MERGE(d:District{name:line.District}) #CREATE (m:Municipality { name: line.Municipality}) #CREATE (d-[:MUNICIPALITY]->(m) #return d,m; #================================== WORKING CODE ++++++++++++++++++++++++++++++++++++# from neo4j.v1 import GraphDatabase, basic_auth driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "test")) # user_name and pwd session = driver.session() session.run("CREATE (a:Person1 {name: {name}, title: {title}})", { "name": "donald", "title": "trump" }) session.run("CREATE (b:Person2 {name: {name}, title: {title}})", { "name": "arthur", "title": "king" }) result = session.run( "MATCH (a:Person) WHERE a.name = {name} " "RETURN a.name AS name, a.title AS title", {"name": "donald"})
from neo4j.v1 import GraphDatabase, basic_auth, Node as neoNode, Transaction from neo4j.v1.exceptions import * driver = GraphDatabase.driver("bolt://192.168.2.1", auth=basic_auth("neo4j", "oglop"), encrypted=False) class Node(neoNode): def __init__(self, labels=None, properties=None, **kwproperties): if type(labels) == str: labels = set(labels.split(', ')) super().__init__(labels, properties, **kwproperties) class Neo: @staticmethod def run(query, params=None, print_query=False, execute=True): """ :param params: dictionary of parameters :param query: cypher query str :param print_query: bool, to print query, default False :param execute: bool, to execute query, default True :return: Cypher result """ if print_query: print(query) print(params) if execute:
#!/usr/bin/env python # Standard Imports import sys from pprint import pprint from neo4j.v1 import GraphDatabase # Object Loader sys.path.insert(0, '../WebScrapper') from obj_loader import read_armor_files uri = "bolt://433-06.csse.rose-hulman.edu:7688" driver = GraphDatabase.driver(uri, auth=("neo4j", "huntallthemonsters247")) allArmor = read_armor_files() #allArmor[0][0]['Skills'][0]['Description'] = 'None' def add_armor(): with driver.session() as session: with session.begin_transaction() as tx: for armor in allArmor[0]: tx.run( "MERGE (a: Armor {id: $id, Name: $name, Part: $part, Slots: $slots})" "RETURN a", id=armor['id'], name=armor['Name'], part=armor['Part'], slots=armor['Slot']) #for skill in attribute['Skills']:
def __init__(self, uri, user, password): self.neo4_driver = GraphDatabase.driver(uri, auth=(user, password)) self.neo_session = self.neo4_driver.session() # self.txn = Transaction(session=self.neo_session) self.exec_qry()
"filter=required:[none]&" \ "filter=ontologies:[{ontology:s}]".format(value=value, ontology=ontology) response = requests.get(url) mapping = None if response.status_code == 200: resource = json.loads(response.content) if resource: result = resource[0] mapping = Mapping(value, result.annotadedProperty.propertyValue, result.semanticTags[0], result.confidence, False) return mapping if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--ontology", "-o", type=str, default="all") parser.add_argument("--darasource", "-ds", type=str, default="all") parser.add_argument("--hostname", default="neo4j-server-local") parser.add_argument("--attr_number", "-n", type=int, default=100) args = parser.parse_args() driver = GraphDatabase.driver("bolt://" + args.hostname) # attr_types = get_most_common_attribute_types(driver, args.attr_number) attr_types = [('Organism', 1), ("Sex", 2)] for (attr, usage) in attr_types: print "{} - {:d}".format(attr, usage) mapping = get_ols_annotation(attr, "EFO") print "{!s}".format(mapping)
import getNgramPairs import runQuery from neo4j.v1 import GraphDatabase, basic_auth def getFeaturedOrGood(qh): res = qh.runQuery("MATCH (n:FeaturedPage) RETURN n.title UNION ALL MATCH (n:GoodPage) RETURN n.title", {}) arr = [] for x in res: arr.append(x['n.title']) #res.close() return arr qh = runQuery.QueryHelper(GraphDatabase.driver("bolt://localhost:10001", encrypted=False, auth=basic_auth("neo4j", "12345"))) finder = getNgramPairs.pairFinder(qh) all_featured_res = qh.runQuery("match (a:FeaturedPage) return a.title as title", {}) all_featured = [] for record in all_featured_res: all_featured.append(record["title"]) all_featured_len = len(all_featured) print("Got all featured articles " + str(all_featured_len)) #featuredOrGoodList = set(getFeaturedOrGood(qh)) #print("Got all good and featured articles " + str(len(featuredOrGoodList))) #with open("positives", "w", encoding="utf-8") as positives: with open("featured->all_only_negatives.csv", "w", encoding="utf-8") as negatives: for i, title in enumerate(all_featured): print("Progress: {}/{}".format(i+1,all_featured_len))
import atexit from flask import Flask, request, Response from flask_cors import CORS from neo4j.v1 import GraphDatabase, basic_auth kgraph_driver = GraphDatabase.driver("bolt://*****:*****@app.route("/") def index(): return "Natmed Knowledge Based Agent API v1.0.0" @app.route("/perceive", methods=['POST']) def perceive(): """ The Agent's reaction API to external enviromental stimuli. """ stimuli = request.json action = agent.perceive(stimuli) return Response(action.to_json(), mimetype='application/json') def close_connections(): kgraph.close()
def server_version_info(cls): with GraphDatabase.driver(cls.bolt_uri, auth=cls.auth_token) as driver: with driver.session() as session: full_version = session.run("RETURN 1").summary().server.version return ServerVersion.from_str(full_version)
def connect(): global neo_driver neo_driver = GraphDatabase.driver("bolt://433-06.csse.rose-hulman.edu:7688",auth=basic_auth("neo4j","huntallthemonsters247")) session = neo_driver.session() return session
def __init__(self, uri, user, password): self._driver = GraphDatabase.driver(uri, auth=(user, password), connection_timeout=15)
from neo4j.v1 import GraphDatabase from sys import argv # Chunk size of each transaction. CHUNK_SIZE = 10000 if __name__ == '__main__': # We need to be passed the URI, username, password, and location of the catalog. if len(argv) != 5: print( 'Usage: python3 load_supp.py [uri] [username] [password] [catalog]' ) exit(1) # Connect to the database, and start a session. driver = GraphDatabase.driver(argv[1], auth=(argv[2], argv[3])) session = driver.session() # Create our indices. session.run('CREATE INDEX ON :Star(TYC1, TYC2, TYC3)') session.run('CREATE INDEX ON :Region(TYC1)') # Commit counter, and the start of the first transaction. tx = session.begin_transaction() commit_i = 0 # Load our file. We are going to read line by line (ughghghghghghhggh). with open(argv[4], 'r') as c_f: for entry in c_f: try: if commit_i >= CHUNK_SIZE:
from neo4j.v1 import GraphDatabase, basic_auth import os import csv driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", os.environ["NEO4J_PASSWORD"])) session = driver.session() # # create data scaffold for Trib Pot of Roman emperors # input_file = open('scaffold_imperial_tribunicia_potestas.tsv', "r") reader = csv.reader(input_file, delimiter='\t') next(reader) # skip first line for row in reader: query = """ MATCH (root:Timeline) MERGE (root)-[:hasYearReferenceSystem]->(yrs:YearReferenceSystem {type:'Titulature of Roman Emperors'}) MERGE (yrs)-[:hasCalendarPartial]->(cp1:CalendarPartial {type:'name', value:'%s', uri:'%s'}) MERGE (cp1)-[:hasCalendarPartial]->(cp2:CalendarPartial {type:'Titulature Part', value:'Tribunicia Potestas'}) MERGE (cp2)-[:hasCalendarPartial]->(cp3:CalendarPartial {type:'number', value:'%s'}) MERGE (cp3)-[:hasGodotUri]->(g:GODOT {uri:'%s', type:'standard', not_before:'%s', not_after:'%s'}) """ % (row[0], row[1], row[2], row[5], row[3], row[4]) session.run(query) # add trib pot attestations from ocre (coin types) input_file = open('attestations_trib_pot_ocre.tsv', "r") reader = csv.reader(input_file, delimiter='\t') next(reader) # skip first line for row in reader: query = """
import os import time import requests import urllib from neo4j.v1 import GraphDatabase, basic_auth neo4jUrl = os.environ.get('NEO4J_URL', "bolt://localhost") neo4jUser = os.environ.get('NEO4J_USER', "neo4j") neo4jPass = os.environ.get('NEO4J_PASSWORD', "test") bearerToken = os.environ.get('TWITTER_BEARER', "") if len(bearerToken) == 0: raise ("No Twitter Bearer token configured") driver = GraphDatabase.driver(neo4jUrl, auth=basic_auth(neo4jUser, neo4jPass)) session = driver.session() # Add uniqueness constraints. session.run("CREATE CONSTRAINT ON (t:Tweet) ASSERT t.id IS UNIQUE;") session.run("CREATE CONSTRAINT ON (u:User) ASSERT u.screen_name IS UNIQUE;") session.run("CREATE CONSTRAINT ON (h:Tag) ASSERT h.name IS UNIQUE;") session.run("CREATE CONSTRAINT ON (l:Link) ASSERT l.url IS UNIQUE;") # Build query. importQuery = """ UNWIND {tweets} AS t WITH t ORDER BY t.id
import numpy as np import math from celery import shared_task from text_labeled.tl_settings import * from itertools import chain from functools import reduce from neo4j.v1 import GraphDatabase import jieba import logging import fileinput import random from text_labeled.util.mail import send_mail import hashlib from aip import AipNlp _driver = GraphDatabase.driver(**NEO4J_CONFIG) userdict_path = os.path.join(os.path.dirname(__file__), "userdict.txt") jieba.load_userdict(userdict_path) #APP_ID = '15031267' #API_KEY = '0rk98S2i4HCgGqT3cBIlryU6' #SECRET_KEY = 'n2uC0RjLgtbC7OZvaKRvaCIT1QeEUDF1' APP_ID = '15033684' API_KEY = 'HSLKfpVrdXAejRj3rSCbe1wj' SECRET_KEY = 'P6uL850ORiIpMeRx4IX1NEj8r4TZfAAG' client = AipNlp(APP_ID, API_KEY, SECRET_KEY) client.setConnectionTimeoutInMillis=2000 client.setSocketTimeoutInMillis=1000
from neo4j.v1 import GraphDatabase, basic_auth import csv driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo")) tokenized_ingredients_query = """\ MATCH (ingredient:Ingredient {labelling_done: true}) RETURN ingredient.value AS ingredient, apoc.coll.sortNodes([(ingredient)-[:HAS_TOKEN]->(token) | token], "index") AS tokens """ with driver.session() as session, open("/tmp/data.txt", "w") as file: writer = csv.writer(file, delimiter=" ") result = session.run(tokenized_ingredients_query) for row in result: for token in row["tokens"]: writer.writerow([token["value"], token["label"]]) writer.writerow([])
import csv from neo4j.v1 import GraphDatabase, basic_auth # host = "bolt://localhost" # password = "******" host = "bolt://54.197.87.95:32952" password = "******" driver = GraphDatabase.driver(host, auth=basic_auth("neo4j", password)) with open("movies.emb", "r") as movies_file, driver.session() as session: next(movies_file) reader = csv.reader(movies_file, delimiter=" ") params = [] for row in reader: movie_id = row[0] params.append({ "id": int(movie_id), "embedding": [float(item) for item in row[1:]] }) session.run( """\ UNWIND {params} AS param MATCH (m:Movie) WHERE id(m) = param.id SET m.embedding = param.embedding
from neo4j.v1 import GraphDatabase from reasoner.knowledge_graph.Config import Config def get_connection_count(session, a, b): counts = {} result = session.run( 'match (a:%s)-->(b:%s) return count(*) as n;' % (a, b)) counts[a + '2' + b] = [record['n'] for record in result][0] result = session.run( 'match (a:%s)<--(b:%s) return count(*) as n;' % (a, b)) counts[b + '2' + a] = [record['n'] for record in result][0] return(counts) config = Config().config driver = GraphDatabase.driver( config['neo4j']['host'], auth=(config['neo4j']['user'], config['neo4j']['password'])) path = ['Drug', 'Target', 'Pathway', 'Cell', 'Tissue', 'Symptom', 'Disease'] with driver.session() as session: for i in range(len(path) - 1): print(get_connection_count(session, path[i], path[i + 1]))
def __init__(self, size, batch_size): super(Runner, self).__init__() self.size = size self.batch_size = batch_size self.driver = GraphDatabase.driver("bolt://localhost:7687/", auth=("neo4j", "password"))
from neo4j.v1 import GraphDatabase uri = "bolt://localhost:7687" driver = GraphDatabase.driver(uri, auth=("neo4j", "katierocks")) session = driver.session() #John Wick session.run("MATCH(m:Movie{title:{title}}) MERGE(d:Director { name:'Chad Stahelski' }) MERGE(d)-[:DIRECTED]->(m)", {"title": "John Wick"}) session.run("MATCH(m:Movie{title:{title}}) MERGE(d:Director { name:'David Leitch' }) MERGE(d)-[:DIRECTED]->(m)", {"title": "John Wick"}) #Speed session.run("MATCH(m:Movie{title:{title}}) MERGE(d:Director { name:'Jan de Bont' }) MERGE(d)-[:DIRECTED]->(m)", {"title": "Speed"}) #The Devil's Advocate session.run("MATCH(m:Movie{title:{title}}) MERGE(d:Director { name:'Taylor Hackford' }) MERGE(d)-[:DIRECTED]->(m)", {"title": "The Devil's Advocate"}) #The Lake House session.run("MATCH(m:Movie{title:{title}}) MERGE(d:Director { name:'Alejandro Agresti' }) MERGE(d)-[:DIRECTED]->(m)", {"title": "The Lake House"}) #The Matrix session.run("MATCH(m:Movie{title:{title}}) MERGE(d:Director { name:'Lana Wachowski' }) MERGE(d)-[:DIRECTED]->(m)", {"title": "The Matrix"}) session.run("MATCH(m:Movie{title:{title}}) MERGE(d:Director { name:'Lilly Wachowski ' }) MERGE(d)-[:DIRECTED]->(m)", {"title": "The Matrix"}) session.close()
def get_driver(): return GraphDatabase.driver(URI, auth=(USERNAME, PASSWD))
def __init__(self,uri,username,password,session_id_input): self._uri = uri self._username = username self._password = password self._driver = GraphDatabase.driver(self._uri,auth=(self._username,self._password)) self._session_id = session_id_input
def __init__(self, uri, user, password): self._driver = GraphDatabase.driver(uri, auth=(user, password))
from collections import namedtuple from neo4j.v1 import GraphDatabase, basic_auth import requests_cache import os import sys requests_cache.install_cache('orangeboard') sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../code") # code directory from RTXConfiguration import RTXConfiguration rtxConfig = RTXConfiguration() # Connection information for the neo4j server, populated with orangeboard driver = GraphDatabase.driver(rtxConfig.neo4j_bolt, auth=basic_auth(rtxConfig.neo4j_username, rtxConfig.neo4j_password)) session = driver.session() # Connection information for the ipython-cypher package connection = "http://" + rtxConfig.neo4j_username + ":" + rtxConfig.neo4j_password + "@" + rtxConfig.neo4j_database DEFAULT_CONFIGURABLE = { "auto_limit": 0, "style": 'DEFAULT', "short_errors": True, "data_contents": True, "display_limit": 0, "auto_pandas": False, "auto_html": False, "auto_networkx": False, "rest": False,
from neo4j.v1 import GraphDatabase uri = "bolt://localhost:7687" driver = GraphDatabase.driver(uri)
#!/usr/bin/env python # coding: utf-8 from flask import Flask, abort, render_template, request from neo4j.v1 import GraphDatabase app = Flask(__name__) # Set up a driver for the local graph database. driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password")) def match_movies(tx, q): return tx.run( "MATCH (movie:Movie) WHERE toLower(movie.title) CONTAINS toLower($term) " "RETURN movie", term=q).value() def match_movie(tx, title): return tx.run( "MATCH (movie:Movie) WHERE movie.title = $title " "OPTIONAL MATCH (person)-[:ACTED_IN]->(movie) " "RETURN movie, collect(person) AS actors", title=title).single() def match_person(tx, name): return tx.run( "MATCH (person:Person) WHERE person.name = $name "
def main(): driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "MH4j")) session = driver.session() analyzer = SentimentIntensityAnalyzer() rawlistZaydman = [ "abusive", "addict", "addiction", "alzheimers", "asylum", "autism", "bipolar", "bonkers", "brain damage", "brain dead", "breakdown", "coping", "crazy", "distressed", "distressing", "disturbed", "disturbing", "eating disorder", "escaped from an asylum", "few sandwiches short of a picnic basket", "freak", "gone in the head", "halfwit", "hallucinating", "hallucinations", "hand fed", "lived experience", "living with addiction", "living with alcoholism", "living with depression", "living with ptsd", "loony", "loony bin", "lunatic", "madness", "manic depression", "mass murderers", "mental", "mental health", "no-one upstairs", "not all there", "not quite there", "not the sharpest knife in the drawer", "numscull", "nutcase", "nuts", "nutter", "nutty as a fruitcake", "OCD", "off their rocker", "operational stress", "out of it", "psycho", "psychopath", "ptsd", "recovery", "retard", "schizo", "schizophrenia", "screw loose", "screwed", "self-control", "self-determination", "self-harm", "stressed", "suicide", "therapist", "therapy", "wheelchair jockey", "window licker", "you belong in a home", "demented", "depressed", "depression", "deranged", "difficulty learning", "dignity", "disabled", "handicapped", "head case", "hurting yourself", "in recovery", "insane", "intellectually challenged", "learning difficulties", "mental health challenges", "mental hospital", "mental illness", "mental institution", "mentally challenged", "mentally handicapped", "mentally ill", "padded cells", "paranoid", "pedophile", "perverted", "psychiatric", "psychiatric health", "psychiatrist", "shock syndrome", "sick in the head", "simpleton", "split personality", "stigma", "strait jacket", "stress", "#abuse", "#addiction", "#alzheimers", "#anxiety", "#bipolar", "#bpd", "#Operationalstress", "#mhsm", "#trauma", "#spsm", "#alcoholism", "#depressed", "#depression", "#eatingdisorders", "#endthestigma", "#IAmStigmaFree", "#mentalhealth", "#pts", "#anxiety", "#therapy", "#endthestigma", "#AA", "#mentalhealthmatters", "#mentalhealthawareness", "#mentalillness", "#MH", "#nostigma", "#nostigmas", "#1SmallAct", "#psychology", "#mhchat", "#schizophrenia", "#ptsd", "#psychology", "#presspause", "#mentalhealthmatters", "#ocd", "#suicideprevention", "#therapy", "#trauma", "#WMHD2017", "#worldmentalhealthday" ] rawlistZaydmanOrig = [ "#mentalhealth", "#depression", "#worldmentalhealthday", "#WMHD2015", "#nostigma", "#nostigmas", "#eatingdisorders", "#suicide", "#ptsd", "#mentalhealthawareness ", "#mentalillness", "#stopsuicide", "#IAmStigmaFree", "#suicideprevention", "#MH", "#addiction", "#bipolar", "#stigma", "Mental health", "depression", "stigma", "eating disorder", "suicide", "ptsd", "mental illness", "addiction", "bipolar" ] wordlistZaydman = map(lambda str: tuple(unicode(str.upper()).split()), rawlistZaydman) wordlistZaydmanOrig = map(lambda str: tuple(unicode(str.upper()).split()), rawlistZaydmanOrig) while True: start_time = time.time() result = session.run( "MATCH (u:SEEDUSER)-[:TWEETED]-(s:SEEDTWEET) " "WITH u, COUNT(DISTINCT s) AS n_Tweets " "LIMIT 10000 " "MATCH (u)-[:TWEETED]-(s2:STATUS) " "WHERE NOT EXISTS(s2.SentNeu) " "RETURN s2.ID, s2.Text, s2.Created_at_L LIMIT 15000 ") try: result.peek() except ResultError: break breakdowns = {} for record in result: record_str = record[1].encode('utf-8') postDate = time.localtime(record[2] / 1000) #print("%s" % record_str) vs = analyzer.polarity_scores(record_str) breakdowns[record[0]] = { 'OZ1': num_words_in_string(wordlistZaydmanOrig, record_str), 'OZ2': num_words_in_string(wordlistZaydman, record_str), 'SC': vs.get('compound'), 'SP': vs.get('pos'), 'SN': vs.get('neg'), 'SZ': vs.get('neu'), 'CA_y': time.strftime('%y', postDate).lstrip('0') or '0', 'CA_m': time.strftime('%m', postDate).lstrip('0') or '0', 'CA_d': time.strftime('%d', postDate).lstrip('0') or '0', 'CA_j': time.strftime('%j', postDate).lstrip('0') or '0', 'CA_H': time.strftime('%H', postDate).lstrip('0') or '0', 'CA_M': time.strftime('%M', postDate).lstrip('0') or '0', 'CA_S': time.strftime('%S', postDate).lstrip('0') or '0' } #print("{}: {}".format(record[0],breakdowns[record[0]])) print("... {} tweets analysed".format(len(breakdowns))) #print(breakdowns); print("... updating {} records".format(len(breakdowns))) for key in breakdowns.keys(): session.run( "MATCH (s:STATUS {{ID: {} }}) SET s+= {{OZ1: {}, OZ2: {}, SentCompound: {}, SentPos: {}, SentNeg: {}, SentNeu: {}, CA_y: {}, CA_m: {}, CA_d: {}, CA_j: {}, CA_H: {}, CA_M: {}, CA_S: {} }} RETURN s" .format( key, breakdowns[key]['OZ1'], breakdowns[key]['OZ2'], breakdowns[key]['SC'], breakdowns[key]['SP'], breakdowns[key]['SN'], breakdowns[key]['SZ'], breakdowns[key]['CA_y'], breakdowns[key]['CA_m'], breakdowns[key]['CA_d'], breakdowns[key]['CA_j'], breakdowns[key]['CA_H'], breakdowns[key]['CA_M'], breakdowns[key]['CA_S'], )) print("... records updated in {} seconds".format(time.time() - start_time)) session.close()
#!/usr/bin/env python3 """ Description goes here """ __author__ = "jupp" __license__ = "Apache 2.0" __date__ = "03/10/2018" from efficient_apriori import apriori from neo4j.v1 import GraphDatabase, basic_auth import json driver = GraphDatabase.driver("bolt://scrappy.ebi.ac.uk:7687") cypher_query = """ MATCH (a)-[HAS_PROVENANCE]->(s:Source) WHERE s.name = 'pdx-finder' WITH a MATCH (be:BiologicalEntity)<-[:HAS_BIO_ENTITY]-(a:Annotation)-[:HAS_PROPERTY]->(p:Property) OPTIONAL MATCH (a)-[:HAS_SEMANTIC_TAG]->(st:SemanticTag) RETURN distinct be.bioEntity, p.propertyType, p.propertyValue, st.semanticTag ORDER BY be.bioEntity ;""" session = driver.session() results = session.run (cypher_query) annotations = {} for result in results:
from neo4j.v1 import GraphDatabase driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "Nic180319")) def add_node(tx, name1, relation, name2): tx.run("MERGE (a:Node {name: $name1}) " "MERGE (b:Node {name: $name2}) " "MERGE (a)-[:" + relation + "]-> (b)", name1=name1, name2=name2) with driver.session() as session: lines = open('./triples.txt', 'r').readlines() print(len(lines)) pattern = '' for i, line in enumerate(lines): arrays = line.split('$$') name1 = arrays[0] relation = arrays[1].replace(':', '').replace(':', '').replace( ' ', '').replace(' ', '').replace('【', '').replace('】', '') name2 = arrays[2] print(str(i)) try: session.write_transaction(add_node, name1, relation, name2) except Exception as e: print(name1, relation, name2, str(e))
# encoding: utf-8 from neo4j.v1 import GraphDatabase uri = "bolt://192.168.0.106:7687" driver = GraphDatabase.driver(uri, auth=("neo4j", "1qaz2wsx")) def _get_department(tx, keyword, limit=2): query = """ MATCH (n:Keyword {keyword: {keyword}})-[r:DepartmentLabel]-(x:Department) RETURN n,x, toInt(r.score) as score order by score desc LIMIT %s """ % limit rst = [] for record in tx.run(query, keyword=keyword): rst.append((record["x"]["department"], record["score"])) return rst def get_department(keyword, limit=2): with driver.session() as session: return session.read_transaction(_get_department, keyword, limit) if __name__ == '__main__': print(get_department("肾结石", 2))
def connect(self): self.driver = GraphDatabase.driver( uri=self.uri, auth=self.auth, encrypted=self.encrypted, trust=self.trust ) self.session = self.driver.session() return self
def setUp(self): from neo4j.v1 import GraphDatabase self.driver = GraphDatabase.driver(self.bolt_routing_uri, auth=self.auth_token)