def test_spark_analytic_query(self): generate_classic(self.session) spark_master = find_spark_master(self.session) # Run multipltle times to ensure we don't round robin for i in range(3): to_run = SimpleGraphStatement("g.V().count()") rs = self.session.execute_graph(to_run, execution_profile=EXEC_PROFILE_GRAPH_ANALYTICS_DEFAULT) self.assertEqual(rs[0].value, 6) self.assertEqual(rs.response_future._current_host.address, spark_master)
def test_profile_graph_options(self): s = self.session statement = SimpleGraphStatement("true") ep = self.session.execution_profile_clone_update(EXEC_PROFILE_GRAPH_DEFAULT) self.assertTrue(s.execute_graph(statement, execution_profile=ep)[0].value) # bad graph name to verify it's passed ep.graph_options = ep.graph_options.copy() ep.graph_options.graph_name = "definitely_not_correct" self.assertRaises(InvalidRequest, s.execute_graph, statement, execution_profile=ep)
def test_additional_custom_payload(self): s = self.session custom_payload = {'some': 'example'.encode('utf-8'), 'items': 'here'.encode('utf-8')} sgs = SimpleGraphStatement("null", custom_payload=custom_payload) future = s.execute_graph_async(sgs) default_profile = s.cluster.profile_manager.profiles[EXEC_PROFILE_GRAPH_DEFAULT] default_graph_opts = default_profile.graph_options for k, v in chain(custom_payload.items(), default_graph_opts.get_options_map().items()): self.assertEqual(future.message.custom_payload[k], v)
def test_init(self): # just make sure Statement attributes are accepted kwargs = {'query_string': object(), 'retry_policy': RetryPolicy(), 'consistency_level': object(), 'fetch_size': object(), 'keyspace': object(), 'custom_payload': object()} statement = SimpleGraphStatement(**kwargs) for k, v in kwargs.items(): self.assertIs(getattr(statement, k), v) # but not a bogus parameter kwargs['bogus'] = object() self.assertRaises(TypeError, SimpleGraphStatement, **kwargs)
def execute_graph_async(self, query, parameters=None, trace=False, execution_profile=EXEC_PROFILE_GRAPH_DEFAULT): """ Execute the graph query and return a `ResponseFuture <http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.ResponseFuture.result>`_ object which callbacks may be attached to for asynchronous response delivery. You may also call ``ResponseFuture.result()`` to synchronously block for results at any time. """ if not isinstance(query, SimpleGraphStatement): query = SimpleGraphStatement(query) graph_parameters = None if parameters: graph_parameters = self._transform_params(parameters) execution_profile = self._get_execution_profile( execution_profile ) # look up instance here so we can apply the extended attributes try: options = execution_profile.graph_options.copy() except AttributeError: raise ValueError( "Execution profile for graph queries must derive from GraphExecutionProfile, and provide graph_options" ) custom_payload = options.get_options_map() custom_payload[_request_timeout_key] = int64_pack( long(execution_profile.request_timeout * 1000)) future = self._create_response_future( query, parameters=None, trace=trace, custom_payload=custom_payload, timeout=_NOT_SET, execution_profile=execution_profile) future.message._query_params = graph_parameters future._protocol_handler = self.client_protocol_handler if options.is_analytics_source and isinstance( execution_profile.load_balancing_policy, DSELoadBalancingPolicy): self._target_analytics_master(future) else: future.send_request() return future
def main(): arguments = docopt(__doc__) host = [arguments['--host']] if arguments["--host"] else ["localhost"] session = Cluster(host).connect() graph = arguments['<keyspace>'] if graph and graph not in session.cluster.metadata.keyspaces: print "Graph {}{}{} not found".format(Fore.RED, graph, Style.RESET_ALL) sys.exit(1) session.default_graph_options.graph_name = graph print Fore.GREEN + "Connected to {}/{}".format(host[0], graph) + Style.RESET_ALL accum = None eof = None print "Gremlin REPL, use heredocs for multiline ex:<<EOF, help for help\n" while True: graph = session.default_graph_options.graph_name prompt = "gremlin [{}/{}]> ".format( host[0], graph) if eof is None else "gremlin (cont)> " input = raw_input(prompt) output_time = False start_time = time.time() if input.startswith("<<"): # heredoc print "Multiline mode activated" eof = input[2:] accum = [] continue if eof and input == eof: eof = None input = "\n".join(accum) print input elif eof: accum.append(input) continue if input == "quit" or input == "exit": break if input == "%schema": continue if input == "help": print_help() continue if input == "docs": webbrowser.open( "http://docs.datastax.com/en/datastax_enterprise/5.0/datastax_enterprise/graph/graphTOC.html#graphTOC__missing-elem-id--graphTOC" ) continue total_time = None try: try: parsed = parse_line(input) result = parsed.execute(session) print_result_set(result) continue except ParseError as e: pass except Exception as e: print e continue stmt = SimpleGraphStatement(input) if input.startswith("a"): print Fore.GREEN + "Spark Graph Traversal Enabled, this may take a while..." + Style.RESET_ALL stmt.options.graph_source = "a" stmt.options.graph_alias = "a" start = time.time() result = session.execute_graph(stmt) total_time = time.time() - start except Exception as e: print e continue if isinstance(result, ResultSet): print_result_set(result) if total_time: print Fore.RED + "Query Time: {}s".format(round( total_time, 3)) + Style.RESET_ALL else: try: print "Unknown result", type(result), result except Exception as e: print e