def __init__(self, server_uri=None): """Instantiate a new RheingoldGraph Session.""" if server_uri is None: server_uri = DEFAULT_GREMLIN_URI self.graph = Graph() self.g = self.graph.traversal().withRemote( DriverRemoteConnection(server_uri, 'g'))
def go(): conn = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g', pool_size=4) g = Graph().traversal().withRemote(conn) yield gen.sleep(0) assert len(g.V().toList()) == 6 conn.close()
def test_dsl(remote_connection): social = Graph().traversal(SocialTraversalSource).withRemote( remote_connection) assert social.persons("marko").knows("josh").next() assert social.persons("marko").youngestFriendsAge().next() == 27 assert social.persons().count().next() == 4 assert social.persons("marko", "josh").count().next() == 2 assert social.persons().filter_(__.createdAtLeast(2)).count().next() == 1
def __init__(self, uri): """ :param uri: example 'ws://54.89.143.194:8182/gremlin' """ self.graph = Graph() self.g = self.graph.traversal().withRemote( DriverRemoteConnection(uri, 'g'))
def list_users(): graph = Graph() remote = DriverRemoteConnection('ws://' + os.environ['DB_ENDPOINT'] + ':8182/gremlin', 'g') g = graph.traversal().withRemote(remote) print(g.V().hasLabel('user').valueMap(True).by(__.unfold()).next()) return jsonify(user='******'), 200
def __init__(self): graph = Graph() # ip_pools = ['192.168.50.5', '192.168.50.6', '192.168.50.7'] ip_pools = ['192.168.50.5'] request_ip = random.sample(ip_pools, 1)[0] self.g = graph.traversal().withRemote( DriverRemoteConnection('ws://' + request_ip + ':8182/gremlin', 'g'))
def main(): graph = Graph() remote = DriverRemoteConnection('ws://' + os.environ['DB_ENDPOINT'] + ':8182/gremlin', 'g') g = graph.traversal().withRemote(remote) seed_users(g) remote.close()
def test_client_bytecode_options(client): # smoke test to validate serialization of OptionsStrategy. no way to really validate this from an integration # test perspective because there's no way to access the internals of the strategy via bytecode g = Graph().traversal() t = g.withStrategies(OptionsStrategy(options={"x": "test", "y": True})).V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}) result_set = client.submit(message) assert len(result_set.all().result()) == 6
def __create_lookup_e(remote): g = Graph().traversal().withRemote(remote) # hold a map of the "name"/edge for use in asserting results - "name" in this context is in the form of # outgoingV-label->incomingV return g.E().group(). \ by(lambda: ("it.outVertex().value('name') + '-' + it.label() + '->' + it.inVertex().value('name')", "gremlin-groovy")). \ by(tail()).next()
def prepare_traversal_source(scenario): # some tests create data - create a fresh remote to the empty graph and clear that graph prior to each test remote = DriverRemoteConnection( 'ws://localhost:45940/gremlin', "ggraph", message_serializer=serializer.GraphSONSerializersV3d0()) scenario.context.remote_conn["empty"] = remote g = Graph().traversal().withRemote(remote) g.V().drop().iterate()
def __init__(self, connection_string: str, remote_traversal_source): """ TODO support for authenticated requests :param connection_string: :param remote_traversal_source: """ self.__connection_str = connection_string self.__remote_traversal_source = remote_traversal_source self.__graph = Graph()
def test_connection(connection): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) results_set = connection.write(message).result() future = results_set.all() results = future.result() assert len(results) == 6 assert isinstance(results, list)
def test_client_bytecode_with_int(client): g = Graph().traversal() t = g.V().has('age', 851401972585122).count() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}) result_set = client.submit(message) results = [] for result in result_set: results += result assert len(results) == 1
def traversal(self): if self.remote_connection is None: raise AttributeError( "The Graph is not connected to any Remote Graph. Use connect() first." ) g = Graph().traversal().withRemote(self.remote_connection) return g
def get_graph_traversal(location: str, port: int=80): """Get graph traversal handle for your experiments. :param location: A location to the graph database instance. Recommended to be used with :func:`thoth.lab.utils.obtain_location` :param port: a port number on which the gremlin listens on :return: a graph traversal object "g" """ return Graph().traversal().withRemote(DriverRemoteConnection(f'ws://{location}:{port}/gremlin', 'g'))
def test_enforce_anonymous_child_traversal(self): g = traversal().withGraph(Graph()) g.V(0).addE("self").to(__.V(1)) try: g.V(0).addE("self").to(g.V(1)) assert False except TypeError: pass
def test_iterate_result_set(client): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}) result_set = client.submit(message) results = [] for result in result_set: results += result assert len(results) == 6
def actors(request): graph = Graph() remoteConn = DriverRemoteConnection('ws://<neptune endpoint>:8182/gremlin', 'g') g = graph.traversal().withRemote(remoteConn) myList = g.V().has( 'title', request.POST['movie_name']).in_().limit(40).values().toList() remoteConn.close() context = {'movie': request.POST['movie_name'], 'actors': myList} return render(request, 'polls/movie-results.html', context)
def graphTraversal(self, neptune_endpoint=None, neptune_port=None, show_endpoint=True, connection=None): if connection is None: connection = self.remoteConnection(neptune_endpoint, neptune_port, show_endpoint) graph = Graph() return graph.traversal().withRemote(connection)
def get_janus_traversal(): """ :return: """ graph = Graph() conn_str = 'ws://' + curr_config['JANUS_HOST'] + ':8182/gremlin' connection = DriverRemoteConnection(conn_str, 'g') g = graph.traversal().withRemote(connection) return g
def parall(blockNumbers): blockNumbers = blockNumbers.tolist() g = Graph().traversal().withRemote( DriverRemoteConnection(gremlin_connection, traversal_source)) for blockNumber in blockNumbers: # blockE = web3.eth.getBlock(blockNumber, True) block = web3._requestManager.request_blocking( "trace_block", [web3.fromDecimal(blockNumber)]) count = len(block) if count > 0: blockEx = g.V().hasLabel("block").has('number', blockNumber).toList() if len(blockEx) == 0: blockV = g.addV('block').property('number', blockNumber).next() else: blockV = blockEx[0] # Add transactions subtraces = 0 prevTx = None for index in range(len(block)): tx = block[index] if 'error' in tx: error = True else: error = False # Internal transaction if subtraces > 0: subtraces -= 1 temp = g.addV('intx').property( 'hash', tx['transactionHash']).property('type', tx['type']) if error: temp.property('error', True) edge = g.V().hasId(prevTx).addE('internal').to( temp).toList() else: temp = g.addV('tx').property( 'hash', tx['transactionHash']).property('type', tx['type']) if error: temp.property('error', tx['error']) edge = g.V().hasId( blockV.id).addE('include').to(temp).toList() decide_on_action(g, tx, edge[0].inV.id, error) if tx['subtraces'] > 0: subtraces = tx['subtraces'] prevTx = edge[0].inV.id
def lambda_handler(event, context): print(event) graph = Graph() table = dynamodb_client.Table('user') tmp = table.scan() dict1 = {} for item in tmp['Items']: dict1[item['UID']] = [] pair = (item['firstName'], item['lastName'], item['pic_url']) dict1[item['UID']].append(pair) print(dict1) remoteConn = DriverRemoteConnection( 'ws://neptunedbinstance-3f8bwqre3vsy.cft44vxyghsh.us-east-1.neptune.amazonaws.com:8182/gremlin', 'g') g = graph.traversal().withRemote(remoteConn) # a=g.V().hasLabel('User').has('uid', '1834389').next() # b=g.V().hasLabel('User').has('uid', '594112').next() # g.V(a).addE('Friend').to(b).iterate() key = event["userId"] friends= g.V().hasLabel('User').has('uid', key).\ both('FRIEND').aggregate('friends'). \ valueMap().toList() list2 = [] for item in friends: tmplist = [] uid = item["uid"] tmplist.append(uid[0]) for tmp in dict1[uid[0]][0]: tmplist.append(tmp) list2.append(tmplist) return {'statusCode': 200, 'body': list2} count = 0 recommend_list = { k: v for k, v in sorted(recommend.items(), key=lambda item: -item[1]) } list1 = [] for item in recommend_list: if item != key: data = dynamodb.get_item(TableName='user', Key={'UID': { 'S': str(item) }}) pair = (str(item), data['Item']['firstName']['S'], data['Item']['lastName']['S'], data['Item']['pic_url']['S']) list1.append(pair) count += 1 if (count == 3): break print(list1) remoteConn.close() # TODO implement return {'statusCode': 200, 'body': list1}
def write_gremlin_df(client: NeptuneClient, df: pd.DataFrame, mode: WriteDFType, batch_size: int) -> bool: """Write the provided dataframe using Gremlin. Parameters ---------- client : NeptuneClient The Neptune client to write the dataframe df : pd.DataFrame The dataframe to write mode : WriteDFType The type of dataframe to write batch_size : int The size of the batch to write Returns ------- bool True if the write operation succeeded """ g = Graph().traversal() # Loop through items in the DF for (index, row) in df.iterrows(): # build up a query if mode == WriteDFType.EDGE: g = _build_gremlin_edges(g, row.to_dict()) elif mode == WriteDFType.VERTEX: g = _build_gremlin_vertices(g, row.to_dict()) else: g = _build_gremlin_update(g, row.to_dict()) # run the query if index > 0 and index % batch_size == 0: res = _run_gremlin_insert(client, g) if res: g = Graph().traversal() else: _logger.debug(res) raise exceptions.QueryFailed( """Failed to insert part or all of the data in the DataFrame, please check the log output.""" ) return _run_gremlin_insert(client, g)
def test_connection(connection): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode, 'aliases': {'g': 'gmodern'}}) results_set = connection.write(message).result() future = results_set.all() results = future.result() assert len(results) == 6 assert isinstance(results, list) assert results_set.done.done() assert 'host' in results_set.status_attributes
def setup_graph(): try: graph = Graph() connstring = "ws://10.84.86.123:8182/gremlin" logging.info('Trying To Login') g = graph.traversal().withRemote(DriverRemoteConnection(connstring, 'g2')) logging.info('Successfully Logged In') except Exception as e: # Shouldn't really be so broad logging.error(e, exc_info=True) raise BadRequestError('Could not connect to Neptune') return g
def test_uuid(self, remote_connection): g = Graph().traversal().withRemote(remote_connection) uid = uuid.UUID("41d2e28a-20a4-4ab0-b379-d810dede3786") resp = g.addV('test_vertex').property('uuid', uid).toList() vid = resp[0].id try: uid_prop = g.V(vid).properties('uuid').toList()[0] assert isinstance(uid_prop.value, uuid.UUID) assert uid_prop.value == uid finally: g.V(vid).drop().iterate()
def test_datetime(self, remote_connection): g = Graph().traversal().withRemote(remote_connection) dt = datetime.datetime.fromtimestamp(1481750076295 / 1000) resp = g.addV('test_vertex').property('dt', dt).toList() vid = resp[0].id try: dt_prop = g.V(vid).properties('dt').toList()[0] assert isinstance(dt_prop.value, datetime.datetime) assert dt_prop.value == dt finally: g.V(vid).drop().iterate()
def get_traversal(self): graph = Graph() try: # take only non deleted resources return graph.traversal().withRemote( DriverRemoteConnection( 'ws://%s/gremlin' % self.gremlin_server, 'g')).withStrategies( SubgraphStrategy(vertices=__.has('deleted', 0))) except (HTTPError, socket.error) as e: raise CommandError('Failed to connect to Gremlin server: %s' % e)
def test_promise(self, remote_connection): g = Graph().traversal().withRemote(remote_connection) future = g.V().aggregate('a').promise() t = future.result() assert len(t.toList()) == 6 a, = t.side_effects.keys() assert a == 'a' results = t.side_effects.get('a') assert results results = t.side_effects.close() assert not results
def test_client_bytecode(client): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', { 'gremlin': t.bytecode, 'aliases': { 'g': 'gmodern' } }) result_set = client.submit(message) assert len(result_set.all().result()) == 6