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(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 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 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 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 __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 __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 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_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 __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 __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 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 __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 __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 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 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 login_neo4j_cloud(): """ connect to neo4j and login """ log.info('Here is where we use the connect to neo4j.') log.info('') config.read(config_file) graphenedb_user = config['neo4j_cloud']['user'] graphenedb_pass = config['neo4j_cloud']['pw'] graphenedb_url = 'bolt://hobby-mdmnpcemepongbkegnfnnfbl.dbs.graphenedb.com:24786' driver = GraphDatabase.driver(graphenedb_url, auth=basic_auth(graphenedb_user, graphenedb_pass)) return driver
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_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 LOAD_Data(username, passw, filename): # Open cypher file cypher = open(filename, 'r') # Set Database uri = 'bolt://localhost:7687' driver = GraphDatabase.driver(uri, auth=basic_auth(username, passw)) # Begin Session session = driver.session() # Create Indices and Constraints session.run( "CREATE CONSTRAINT ON (airport:Airport) ASSERT airport.name IS UNIQUE") session.run( "CREATE CONSTRAINT ON (user:User) ASSERT user.user_id IS UNIQUE") session.run("CREATE CONSTRAINT ON (loc:Loc) ASSERT loc.tweet_id IS UNIQUE") session.run("CREATE INDEX ON :Loc(loc_id)") session.run("CREATE INDEX ON :Loc(datetime)") session.run("CREATE INDEX ON :Tweet(twitter_string)") # Import Green Data File into Neo4j session.run(cypher.read()) session.close() # Building -[:NEXT]-> relation among consecutive locations # Get Number of unique User ids numberids = session.run("MATCH (user:User) RETURN user.user_id AS ids") # Loop through subgraphs for a in numberids: # Connect consecutive locations session.run( "MATCH (loc:Loc) " + "WHERE loc.loc_id = " + str(a['ids']) + " " + "WITH loc ORDER BY loc.datetime WITH collect(loc) as locs " + "FOREACH(i in RANGE(0, size(locs)-2) | " + "FOREACH(L1 in [locs[i]] | " + "FOREACH(L2 in [locs[i+1]] | " + "CREATE UNIQUE (L1)-[:NEXT]->(L2))))") 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), encrypted=config.ENCRYPTED_CONNECTION, max_pool_size=config.MAX_POOL_SIZE) self.refresh_connection()
def create_con(): """ Use address and port number connect to Neo4j,everytime you use it,it create a new instance with session :return:A connection object """ cfg = ConfigParser() cfg.read(CONFIG_PATH) try: # Fetch db info address = cfg.get(DB_SECTION, 'address') port = cfg.get(DB_SECTION, 'port') user = cfg.get(DB_SECTION, 'user') passwd = cfg.get(DB_SECTION, 'passwd') # Connect driver = GraphDatabase.driver( "bolt://%s:%s" % (address, port), auth=basic_auth(user, passwd)) return driver except Exception as err: print(err)
def login_neo4j_cloud(): """ connect to neo4j and login """ log.info('Here is where we use the connect to neo4j.') log.info('') config.read(config_file) graphenedb_user = config["neo4j_cloud"]["user"] graphenedb_pass = config["neo4j_cloud"]["pw"] graphenedb_url = 'bolt://hobby-ieelacemepongbkenpdmofbl.dbs.graphenedb.com:24786' driver = GraphDatabase.driver(graphenedb_url, auth=basic_auth(graphenedb_user, graphenedb_pass)) return driver
def process_tag(self, tags, start_date, end_date): tag = ";".join(tags) print("Processing projects from {0} to {1}. Tag: [{2}]".format( start_date, end_date, tag)) print(tag, start_date, end_date) with GraphDatabase.driver(self.neo4j_url, auth=basic_auth(self.neo4j_user, self.neo4j_pass)) as driver: with driver.session() as session: page = 1 items = 100 has_more = True while has_more: api_url = self.construct_uri(page, items, tag, start_date, end_date, self.so_key) response = requests.get( api_url, headers={"accept": "application/json"}) print(response.status_code) if response.status_code != 200: print(response.text) json = response.json() print("has_more", json.get("has_more", False), "quota", json.get("quota_remaining", 0)) if json.get("items", None) is not None: print(len(json["items"])) result = session.write_transaction( self.so_import, json) print(result) print(result.consume().counters) page = page + 1 has_more = json.get("has_more", False) print("has_more: {more} page {page}".format(page=page, more=has_more)) if json.get('quota_remaining', 0) <= 0: time.sleep(10) if json.get('backoff', None) is not None: print("backoff", json['backoff']) time.sleep(json['backoff'] + 5)
def load_decks_to_neo4j(neo4juri=NEO4J_URI, username=None, password=None): driver = GraphDatabase.driver(neo4juri, auth=basic_auth(username, password)) with driver.session() as session: # card name and set uniqueness session.run("create constraint on (d:MtgDeck) assert d.id is unique") LOGGER.info('bulk loading to neo4j') for deckchunk in _chunks(1000, S.scg_decks()): jsonchunk = [] for d in deckchunk: try: jsonchunk.append(d.to_dict()) except S.ScgDeckParseError: continue with driver.session() as session: session.run(SCG_INSERT_DECKS_QRY, parameters={'decks': jsonchunk}) session.run(SCG_INSERT_BOARDS_QRY, parameters={'decks': jsonchunk})
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 login_neo4j_cloud(): """ connect to neo4j and login """ log.info('Here is where we use the connect to neo4j.') log.info('') config.read(config_file) graphenedb_user = config["neo4j_cloud"]["user"] graphenedb_pass = config["neo4j_cloud"]["pw"] graphenedb_url = "bolt://hobby-hegikgbadkkkgbkedcfoiacl.dbs.graphenedb.com:24787" driver = GraphDatabase.driver(graphenedb_url, auth=basic_auth(graphenedb_user, graphenedb_pass)) return driver
def __init__(self): super().__init__(Donor) self.min_col_width = 12 self.def_pad = 7 self.col_sep = ' | ' self.cols = ['Donor Name', 'Total Given', 'Num Gifts', 'Average Gift'] try: config.read(config_file) user = config['neo4j_cloud']['user'] psw = config['neo4j_cloud']['pw'] url = 'bolt://hobby-abbpgihliahcgbkebpafdlbl.dbs.graphenedb.com:24786' except KeyError as e: print('Unable to read config file: ', e) raise e self.db_name = 'Mailroom' self._driver = GraphDatabase.driver(url, auth=basic_auth(user, psw)) self._update_dict()
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 login_neo4j_cloud(): """ connect to neo4j and login """ log.info('Here is where we use the connect to neo4j.') log.info('') config.read(config_file) graphenedb_user = config["neo4j_cloud"]["user"] graphenedb_pass = config["neo4j_cloud"]["pw"] graphenedb_url = config["neo4j_cloud"]["connection"] driver = GraphDatabase.driver(graphenedb_url, auth=basic_auth(graphenedb_user, graphenedb_pass)) return driver
def __call_neo4j_api(self, cypher,parameters): """Calls the neo4j driver for query a result raises an exception for network errors Returns a dict of the JSON 'data' attribute in the response """ driver = GraphDatabase.driver( NEO4J_URL, auth=basic_auth(USERNAME, PASSWORD)) session = driver.session() response={} results = session.run(cypher,parameters) for record in results: res=record.items() for k,v in res: response[k]=v return response
def main(): db_config = json.load(open('db-config.json', 'r')) print(db_config['port']) driver = GraphDatabase.driver('bolt://{0}:{1}'.format( db_config['host'], db_config['port']), auth=basic_auth(db_config['user'], db_config['pass'])) session = driver.session() print("Gathering data..") x = pandas.read_csv("data/reddit_info.csv", delimiter="\t", header=None, names=[ 'rank', 'download_time', 'timestamp', 'subreddit', 'id_art', 'ups', 'downs', 'title', 'num_comments', 'score', 'over_18' ], error_bad_lines=False) x_df = pandas.DataFrame(x) print("Grouping..") xj = x.groupby(['subreddit', 'id_art']) for xx in xj: rank, subreddit, id_art = xx[1]['rank'].values[0], xx[0][0], xx[0][1] print(rank, subreddit, id_art) # pdb.set_trace() query = ''' MERGE (primary:Rank {name:{Rank}}) MERGE (secondary:Sub {name:{Subreddit}}) MERGE (tertiary:ID {name:{ID}}) MERGE (secondary)-[:HASRANK]->(primary) MERGE (tertiary)-[:BELONGSTO]->(secondary) ''' print(query) session.run(query, { 'Rank': rank, 'Subreddit': subreddit, 'ID': id_art }) session.close()
def login_neo4j_cloud(): """ connect to neo4j and login """ log.info('Here is where we use the connect to neo4j.') log.info('') config.read(config_file) graphenedb_user = config["neo4j_cloud"]["user"] graphenedb_pass = config["neo4j_cloud"]["pw"] # graphenedb_url = 'bolt://hobby-opmhmhgpkdehgbkejbochpal.dbs.graphenedb.com:24786' graphenedb_url = config["neo4j_cloud"]["url"] driver = GraphDatabase.driver(graphenedb_url, auth=basic_auth(graphenedb_user, graphenedb_pass)) return driver
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 get_attack_nodes(): """ Searches the database for all nodes with 'attack' in their cmdline. :return: A tuple of (nodes, edges). nodes is a Dictionary of node_id -> node, edges is a Dictionary of edge_id -> edge """ driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "neo4j")) session = driver.session() query = """ MATCH path=(n) WHERE n.cmdline =~ '.*attack.*' RETURN path """ results = session.run(query) session.close() nodes, edges = clean_data_raw(results) return nodes, edges
def checkTheDb(): returnableSet = [] #confim we can connect and if so, export the stats driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password")) session = driver.session() try: results = session.run("match (a)-[c]-(b) return count(c) as relcount") for record in results: returnableSet.append(record['relcount']) results = session.run("match (a) return count(a) as nodecount") for record in results: returnableSet.append(record['nodecount']) except: print("Trouble executing the query") raise RuntimeError finally: session.close() return returnableSet
def __init__(self): self.idx_ = 0 # odom_index ## 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:SESSTURTLE) DETACH DELETE n" ) # REMOVE ALL SESSTURTLE 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 connect(self): neo4j_url = conf.get('graphdb', 'neo4j_url') neo4j_user = conf.get('graphdb', 'neo4j_user') neo4j_pass = conf.get('graphdb', 'neo4j_pass') if self.attempts > 10: raise DatabookException("Attempted to connect > 10 times") try: log.info("Connecting {0} {1} {2}".format(neo4j_url, neo4j_user, neo4j_pass)) self.driver = GraphDatabase.driver(neo4j_url, auth=basic_auth(neo4j_user, neo4j_pass)) session = self.driver.session() self.connected = True self.attempts = 0 return session except ServiceUnavailable as su: log.error("Neo4j is not available") self.connected = False self.attempts += 1 raise
def get_neo4j_connection(host, port, user, auth): """ Returns a neo4j driver and a neo4j session with the requested params :param host: Neo4J Host IP/URL :param port: Neo4J Bolt Port, default 7687 :param user: Neo4J User, default is neo4j :param auth: Neo4J Password :return: Neo4j Driver object Neo4j Session object """ driver = GraphDatabase.driver("bolt://%s:%s" % (host, str(port)), auth=basic_auth(user, auth)) session = driver.session() return driver, session
def create_nodes(papers): print("Creating NEO4j nodes") driver = GraphDatabase.driver(URI, auth=basic_auth(NEO4J_USER, NEO4J_PASSWORD)) count = 0 with driver.session() as session: for key, value in papers.items(): session.run( "CREATE (p:Paper {id: {id}, title: {title}, author: {author}, date: {date}})", { "id": key, "title": value.title, "author": value.author, "date": value.date }) count += 1 if count % 1000 == 0: print("Already processed {0} papers".format(count))
def connect(self, authentication_options): if authentication_options is None: raise InvalidOptionsException() user_name = authentication_options.get("user_name") user_pass = authentication_options.get("user_password") host = authentication_options.get("host") if user_name is None or user_pass is None or host is None: raise InvalidOptionsException() try: self.driver = GraphDatabase.driver("bolt://{}".format(host), auth=basic_auth( user_name, user_pass)) except ProtocolError as e: raise InvalidOptionsException(e) self._create_session()
def get_all_successor_nodes(root_id): """ Returns all successor nodes given a root node. :param root_id: The id of the root node :return: A BoltStatementResult describing the raw result returned by Neo4j """ driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "neo4j")) session = driver.session() query = """ MATCH path=(n)-[r*]->(m) WHERE Id(m) = $root_id RETURN path """ results = session.run(query, {"root_id": root_id}) session.close() return results
def get_n_degree_interacting_genes(self, tx, gene, n): driver = GraphDatabase.driver("bolt://127.0.0.1:7687", auth=basic_auth("neo4j", "optiplex")) session = driver.session() #session.run("CREATE (a:Person {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": "Arthur"}) #for record in result: # print("%s %s" % (record["title"], record["name"])) result = tx.run( "MATCH (b:Gene) " "WHERE b.entrez_id = {gene_id} " "RETURN b", {'gene_id': gene}) #for record in result: # print(record['entrez_id']) # #print("{}".format(record['entrez_id'])) return result
def login_neo4j_cloud(): """ connect to neo4j and login """ print('Here is where we use the connect to neo4j.') print('') config.read(config_file) graphenedb_user = config["neo4j_cloud"]["user"] graphenedb_pass = config["neo4j_cloud"]["pw"] graphenedb_url = 'bolt://hobby-oklmfkohpmongbkebkgbdgbl.dbs.graphenedb.com:24786' driver = GraphDatabase.driver(graphenedb_url, auth=basic_auth(graphenedb_user, graphenedb_pass)) return driver
def setup_db(): """ sets up the connection to the neo4j DB configuration (username and password) is read from a config file in a .config dir in the dir specified in CONFIG """ config = ConfigParser() config.read(CONFIG) graphenedb_user = config["configuration"]["neo4juser"] graphenedb_pass = config["configuration"]["neo4jpw"] graphenedb_url = 'bolt://hobby-fcojboiekhacgbkekjijmpal.dbs.graphenedb.com:24786' driver = GraphDatabase.driver(graphenedb_url, auth=basic_auth(graphenedb_user, graphenedb_pass)) return driver
def configure_constraints(neo4j_url, neo4j_user, neo4j_pass): with GraphDatabase.driver(neo4j_url, auth=basic_auth(neo4j_user, neo4j_pass)) as driver: with driver.session() as session: 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 (g:GitHubAccount) ASSERT g.id IS UNIQUE") session.run( "CREATE CONSTRAINT ON (s:StackOverflowAccount) ASSERT s.id IS UNIQUE" ) session.run( "CREATE CONSTRAINT ON (q:Question) ASSERT q.id IS UNIQUE") session.run( "CREATE CONSTRAINT ON (a:Answer) ASSERT a.id IS UNIQUE") session.run("CREATE INDEX ON :Tag(name)") session.run("CREATE INDEX ON :Link(url)") session.run("CREATE INDEX ON :Tweet(created)")
def connect_to (self, code, **kwargs): """ usage: connect-to <url> [options] <url> Neo4j server URL --user STRING Neo4j server username (optional) --password STRING Neo4j server password (optional) Note that --user and --password, if provided, will override any credential found in the URL itself. """ # parse the URL, setting aside credentials (if any) url = urlparse.urlparse(kwargs["<url>"]) neo4j_url = "bolt://%s%s" % (url.netloc, url.path) _logger.debug("connecting to Neo4j server at %s" % neo4j_url) # authenticate the connection only if credentials are provided neo4j_username = kwargs.get("--user", url.username) neo4j_password = kwargs.get("--password", url.password) if (neo4j_username is not None) and (neo4j_password is not None): _logger.debug("authenticating with user '%s'" % neo4j_username) token = neo4j.basic_auth( neo4j_username, neo4j_password) else: token = None # connect to the server try: neo4j_driver = neo4j.GraphDatabase.driver(neo4j_url, encrypted = True, auth = token, trust = neo4j.TRUST_ON_FIRST_USE) neo4j_session = neo4j_driver.session() except neo4j.CypherError as exception: raise Exception("from Neo4j: %s" % exception) self._connection = neo4j_session _logger.debug("connecting: done (Neo4j server %s)" % \ '.'.join(map(str, self._connection.neo4j_version)))
def main(): # Get access to the database. driver = neo.GraphDatabase.driver(conf.databaseURI, auth=neo.basic_auth( conf.username, conf.password)) # Clear the database. session = driver.session() session.run("MATCH (n) DETACH DELETE n") session.close() # ------------------------------ # # Create Constraints and Indices # # ------------------------------ # session = driver.session() constraintTransaction = session.begin_transaction() constraintTransaction.run( "CREATE CONSTRAINT ON (c:Condition) ASSERT c.name IS UNIQUE") constraintTransaction.run( "CREATE CONSTRAINT ON (m:Measurement) ASSERT m.name IS UNIQUE") constraintTransaction.run( "CREATE CONSTRAINT ON (r:Reference) ASSERT r.id IS UNIQUE") constraintTransaction.run( "CREATE CONSTRAINT ON (t:Treatment) ASSERT t.name IS UNIQUE") constraintTransaction.commit() session.close() # -------------- # # Add Conditions # # -------------- # fid = open("Conditions.json", 'r') conditionData = json.load(fid) fid.close() query = "WITH {json} AS data " \ "UNWIND data as cond " \ "CREATE (c:Condition {name: cond.name})" result = session.run(query, {"json": conditionData}) session.close() print([i for i in result])
def inject_recommendations_into_neo4j(username, records): to_return = {} nb_relationships_created = 0 socket.setdefaulttimeout(10) try: logger.debug("Trying to connect to bolt://" + neo4j_node + ":" + neo4j_bolt_port + " ...") # os.system("sudo rm -r /home/xnet/.neo4j/known_hosts") driver = GraphDatabase.driver( "bolt://" + neo4j_node + ":" + neo4j_bolt_port, auth=basic_auth(neo4j_username, neo4j_password)) session = driver.session() for record in records.decode("utf-8").split("\n"): values = record.split(",") track_id = values[len(values) - 1] if track_id: result = session.run("MATCH (u:Utilisateur {nomUtilisateur:'" + username + "'}), (t:Titre {idTitre:'" + track_id + "'}) CREATE (u)-[:RECO]->(t)") summary = result.consume() logger.debug( str(summary.counters.relationships_created) + " recommendation added (" + username + " --RECO--> " + track_id + ")") nb_relationships_created += summary.counters.relationships_created session.close() logger.info( str(nb_relationships_created) + " RECO relationships have been created") return True, to_return except socket.timeout as toe: to_return[ "error"] = "There was a problem while trying to connect to Neo4j" to_return["details"] = str(toe) logger.debug(str(to_return)) return False, to_return return False, None
def edit_SHG(old_name, new_name, curr_hash): with GraphDatabase.driver(neo4j_uri, auth=basic_auth("neo4j", "123456")) as c_driver: cypher_cmd = "match (n:IHashNode{hashCode:'" + curr_hash + "'}) return n.file_list" try: with c_driver.session() as session: shg_ret = session.run(cypher_cmd) for row in shg_ret: file_str = row[0] file_str = file_str.replace(old_name, new_name) cypher_cmd = "match (n:IHashNode{hashCode:'" + curr_hash + "'}) set n.file_list='" + file_str + "'" try: session.run(cypher_cmd) except Exception as e: print(e) return 1 except Exception as e: print(e) return 1 return 0
def process_item(self, item, spider): authorName = item.get('name') imageUrl = item.get('img_url') researchInterests = item.get('research_interests') homepageUrl = item.get('homepage_url') driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "123456")) session = driver.session() session.run( "MATCH (a:AUTHOR {name: {name}}) SET a.img_url = {img_url} SET a.research_interests = {research_interests} SET a.homepage_url = {homepage_url} RETURN a", { "name": authorName[0], "img_url": imageUrl[0], "research_interests": researchInterests[0], "homepage_url": homepageUrl[0] }) session.close() return item
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()
#!/usr/bin/env python # -*- coding: utf-8 -*- import os from neo4j.v1 import GraphDatabase, basic_auth os.environ["neo4jpass"] = "******" graph_db = GraphDatabase.driver("bolt://datathree.csse.rose-hulman.edu", auth=basic_auth("neo4j", os.environ["neo4jpass"])) def escapeSpecialCharacters ( text ): text = text.replace("'", "\\" + "'" ) text = text.replace('"', "\\" + '"' ) #text = text.replace("'", "\\" + "'" ) return text def neoAddSong(songID, title): print(str(songID) + " " + title) session = graph_db.session() session.run("CREATE (node:Song {ID : " + "'%s'"% str(songID) + ", Title : \'" +escapeSpecialCharacters(title)+ "\'})") session.close() return def neoAddTag(songID, tag): session = graph_db.session() session.run("MATCH (a:Song) WHERE a.ID = '%s'"%str(songID) + " " + "MERGE (b:Tag {Name : \'" + escapeSpecialCharacters(tag.lower()) + "\'})"+ "CREATE UNIQUE (a)-[:TAGGED]->(b);") session.close() return
# # my_model = PCA(n_components=5) # my_model.fit_transform(my_matrix) # # print my_model.explained_variance_ # print my_model.explained_variance_ratio_ # print my_model.explained_variance_ratio_.cumsum() from neo4j.v1 import GraphDatabase, basic_auth from sklearn import metrics from sklearn.decomposition import PCA, TruncatedSVD import numpy as np import pandas as pd driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo")) session = driver.session() rows = session.run(""" MATCH (e:Episode)<-[:APPEARED_IN]-(c) WITH c, COUNT(*) AS apps WHERE apps > 1 WITH c MATCH (e:Episode) OPTIONAL MATCH (c)-[appearance:APPEARED_IN]->(e) RETURN e, c, appearance ORDER BY e.id, c.id """) episodes = {} seasons = {}
def import_from_xml(csv_filename, database_url = "bolt://localhost", neo_user = "******",neo_pass = "******"): # Log into the database driver = GraphDatabase.driver(database_url, auth=basic_auth(neo_user, neo_pass)) # Parse CSV print "Parsing xml file into a DOM..." data = minidom.parse(csv_filename) sources = [] print "Onto the data entry..." # Run through the different types of entry in a V3 Apple Health export and import them # ...creating nodes and relationships to input sources. for entry_type in ["Record", "Workout", "WorkoutEntry", "Correlation"]: entry_collection = data.getElementsByTagName(entry_type) record_number = 0 for entry in entry_collection: record_number += 1 node_name = entry_type + "_" + str(record_number) cypher_command = "create (" + node_name + ":" + entry_type + " { entry_name: \'" + node_name + "\', " source = "" for attr in entry.attributes.keys(): if attr is not u'type': if str(attr) == "sourceName": source = str(entry.attributes[attr].value) if entry.attributes[attr].value not in sources: sources.append(entry.attributes[attr].value) # Create the input source as a :Source node session = driver.session() session.run("create (" + source + ":Source {sourceName: \"" + source + "\"}) ") session.close() print "create (" + source + ":Source {sourceName: \"" + source + "\"})" try: if type(entry.attributes[attr].value) is unicode: val = str(entry.attributes[attr].value) else: val = str(entry.attributes[attr].value()) cypher_command += str(attr) + ": \"" + val + "\", " except TypeError: print type(entry.attributes[attr].value), "is a format in the xml that I can't process! Sorry. Bombing out now, but be aware that changes to the database have been commited!" print "here's the string version of the entry I failed on:" print str(entry) print "\nGood luck!\n" exit(0) cypher_command = cypher_command[:-2] + "})" print cypher_command session = driver.session() session.run(cypher_command) session.close() cypher_command = "MATCH (source:Source {sourceName: \"" + source + "\"}), (entry {entry_name: \"" + node_name + "\"}) create (entry)-[:ENTERED_THROUGH]->(source) " print cypher_command session = driver.session() session.run(cypher_command) session.close() if record_number == 1000: exit()
# pip install neo4j-driver from neo4j.v1 import GraphDatabase, basic_auth NEO4J_USER = "******" NEO4J_PASS = "******" driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth(NEO4J_USER, NEO4J_PASS)) session = driver.session() # Insert data insert_query = ''' UNWIND {pairs} as pair MERGE (p1:Person {name:pair[0]}) MERGE (p2:Person {name:pair[1]}) MERGE (p1)-[:KNOWS]-(p2); ''' data = [["Jim", "Mike"], ["Jim", "Billy"], ["Anna", "Jim"], ["Anna", "Mike"], ["Sally", "Anna"], ["Joe", "Sally"], ["Joe", "Bob"], ["Bob", "Sally"]] session.run(insert_query, parameters={"pairs": data}) # Friends of a friend foaf_query = ''' MATCH (person:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf) WHERE person.name = {name} AND NOT (person)-[:KNOWS]-(foaf)
def __init__(self, id, njuser, njpwd): self.id = id self.driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth(njuser, njpwd)) self.session = self.driver.session()