Beispiel #1
0
 def test_init(self):
     opts = GraphOptions(**self.api_params)
     self._verify_api_params(opts, self.api_params)
     self._verify_api_params(GraphOptions(), {
         'graph_source': 'g',
         'graph_language': 'gremlin-groovy'
     })
Beispiel #2
0
    def load_schema(self, use_schema=False):
        """加载schema文件
        """
        if use_schema:
            # 创建默认的执行配置,指向特定的graph
            ep = GraphExecutionProfile(graph_options=GraphOptions(
                graph_name=self.graph_name))
            cluster = Cluster(
                execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep})

            session = cluster.connect()

            # 创建graph
            session.execute_graph(
                "system.graph(name).ifNotExists().create()",
                {'name': self.graph_name},
                execution_profile=EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT)

            # 批量执行gremlin创建schema的命令
            with open(self.schema_path) as f:
                pat = re.compile("[/ \n]")  # 正则表达式
                for line in f:
                    if not re.match(pat, line):  # 开头不是斜杠、空格、换行的
                        print("正在加载 {}".format(line.strip()))
                        session.execute_graph(line.strip())

        else:
            print("schema未加载,请确保graph中存在schema")
Beispiel #3
0
    def __init__(self,
                 load_balancing_policy=None,
                 retry_policy=None,
                 consistency_level=ConsistencyLevel.LOCAL_ONE,
                 serial_consistency_level=None,
                 request_timeout=3600. * 24. * 7.,
                 row_factory=graph_object_row_factory,
                 graph_options=None):
        """
        Execution profile with timeout and load balancing appropriate for graph analytics queries.

        See also :class:`~.GraphExecutionPolicy`.

        In addition to default parameters shown in the signature, this profile also defaults ``retry_policy`` to
        :class:`dse.policies.NeverRetryPolicy`, and ``load_balancing_policy`` to one that targets the current Spark
        master.
        """
        load_balancing_policy = load_balancing_policy or DSELoadBalancingPolicy(
            default_lbp_factory())
        graph_options = graph_options or GraphOptions(
            graph_source=b'a', graph_language=b'gremlin-groovy')
        super(GraphAnalyticsExecutionProfile,
              self).__init__(load_balancing_policy, retry_policy,
                             consistency_level, serial_consistency_level,
                             request_timeout, row_factory, graph_options)
Beispiel #4
0
    def test_get_options(self):
        # nothing set --> base map
        base = GraphOptions(**self.api_params)
        self.assertEqual(GraphOptions().get_options_map(base), base._graph_options)

        # something set overrides
        kwargs = self.api_params.copy()  # this test concept got strange after we added default values for a couple GraphOption attrs
        kwargs['graph_name'] = 'unit_test'
        other = GraphOptions(**kwargs)
        options = base.get_options_map(other)
        updated = self.opt_mapping['graph_name']
        self.assertEqual(options[updated], six.b('unit_test'))
        for name in (n for n in self.opt_mapping.values() if n != updated):
            self.assertEqual(options[name], base._graph_options[name])

        # base unchanged
        self._verify_api_params(base, self.api_params)
    def test_get_options(self):
        # nothing set --> base map
        base = GraphOptions(**self.api_params)
        self.assertEqual(GraphOptions().get_options_map(base), base._graph_options)

        # something set overrides
        kwargs = self.api_params.copy()  # this test concept got strange after we added default values for a couple GraphOption attrs
        kwargs['graph_name'] = 'unit_test'
        other = GraphOptions(**kwargs)
        options = base.get_options_map(other)
        updated = self.opt_mapping['graph_name']
        self.assertEqual(options[updated], six.b('unit_test'))
        for name in (n for n in self.opt_mapping.values() if n != updated):
            self.assertEqual(options[name], base._graph_options[name])

        # base unchanged
        self._verify_api_params(base, self.api_params)
Beispiel #6
0
    def test_consistency_passing(self):
        """
        Test to validated that graph consistency levels are properly surfaced to the base dirver

        @since 1.0.0
        @jira_ticket PYTHON-509
        @expected_result graph consistency levels are surfaced correctly
        @test_category dse graph
        """
        cl_attrs = ('graph_read_consistency_level', 'graph_write_consistency_level')

        # Iterates over the graph options and constructs an array containing
        # The graph_options that correlate to graoh read and write consistency levels
        graph_params = [a[2] for a in _graph_options if a[0] in cl_attrs]

        s = self.session
        default_profile = s.cluster.profile_manager.profiles[EXEC_PROFILE_GRAPH_DEFAULT]
        default_graph_opts = default_profile.graph_options
        try:
            # Checks the default graph attributes and ensures that both  graph_read_consistency_level and graph_write_consistency_level
            # Are None by default
            for attr in cl_attrs:
                self.assertIsNone(getattr(default_graph_opts, attr))

            res = s.execute_graph("null")
            for param in graph_params:
                self.assertNotIn(param, res.response_future.message.custom_payload)

            # session defaults are passed
            opts = GraphOptions()
            opts.update(default_graph_opts)
            cl = {0: ConsistencyLevel.ONE, 1: ConsistencyLevel.LOCAL_QUORUM}
            for k, v in cl.items():
                setattr(opts, cl_attrs[k], v)
            default_profile.graph_options = opts

            res = s.execute_graph("null")

            for k, v in cl.items():
                self.assertEqual(res.response_future.message.custom_payload[graph_params[k]], six.b(ConsistencyLevel.value_to_name[v]))

            # passed profile values override session defaults
            cl = {0: ConsistencyLevel.ALL, 1: ConsistencyLevel.QUORUM}
            opts = GraphOptions()
            opts.update(default_graph_opts)
            for k, v in cl.items():
                attr_name = cl_attrs[k]
                setattr(opts, attr_name, v)
                self.assertNotEqual(getattr(default_profile.graph_options, attr_name), getattr(opts, attr_name))
            tmp_profile = s.execution_profile_clone_update(EXEC_PROFILE_GRAPH_DEFAULT, graph_options=opts)
            res = s.execute_graph("null", execution_profile=tmp_profile)

            for k, v in cl.items():
                self.assertEqual(res.response_future.message.custom_payload[graph_params[k]], six.b(ConsistencyLevel.value_to_name[v]))
        finally:
            default_profile.graph_options = default_graph_opts
Beispiel #7
0
    def test_consistency_levels(self):
        read_cl = ConsistencyLevel.ONE
        write_cl = ConsistencyLevel.LOCAL_QUORUM

        # set directly
        opts = GraphOptions(graph_read_consistency_level=read_cl,
                            graph_write_consistency_level=write_cl)
        self.assertEqual(opts.graph_read_consistency_level, read_cl)
        self.assertEqual(opts.graph_write_consistency_level, write_cl)

        # mapping from base
        opt_map = opts.get_options_map()
        self.assertEqual(opt_map['graph-read-consistency'],
                         six.b(ConsistencyLevel.value_to_name[read_cl]))
        self.assertEqual(opt_map['graph-write-consistency'],
                         six.b(ConsistencyLevel.value_to_name[write_cl]))

        # empty by default
        new_opts = GraphOptions()
        opt_map = new_opts.get_options_map()
        self.assertNotIn('graph-read-consistency', opt_map)
        self.assertNotIn('graph-write-consistency', opt_map)

        # set from other
        opt_map = new_opts.get_options_map(opts)
        self.assertEqual(opt_map['graph-read-consistency'],
                         six.b(ConsistencyLevel.value_to_name[read_cl]))
        self.assertEqual(opt_map['graph-write-consistency'],
                         six.b(ConsistencyLevel.value_to_name[write_cl]))
Beispiel #8
0
    def create_execution_profile(graph_name):
        """
        Creates an ExecutionProfile for GraphTraversal execution. You need to register that execution profile to the
        cluster by using `cluster.add_execution_profile`.

        :param graph_name: The graph name
        """

        ep = GraphExecutionProfile(
            row_factory=graph_traversal_dse_object_row_factory,
            graph_options=GraphOptions(
                graph_name=graph_name,
                graph_language=DseGraph.DSE_GRAPH_QUERY_LANGUAGE))
        return ep
    def create_graph(self):
        # 创建默认的执行配置,指向特定的graph
        ep = GraphExecutionProfile(graph_options=GraphOptions(
            graph_name=self.graph_name))
        cluster = Cluster(contact_points=[self.address],
                          execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep})

        session = cluster.connect()

        # 创建graph
        session.execute_graph(
            "system.graph(name).ifNotExists().create()",
            {'name': self.graph_name},
            execution_profile=EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT)
    def test_consistency_passing(self):
        """
        Test to validated that graph consistency levels are properly surfaced to the base dirver

        @since 1.0.0
        @jira_ticket PYTHON-509
        @expected_result graph consistency levels are surfaced correctly
        @test_category dse graph
        """
        cl_attrs = ('graph_read_consistency_level', 'graph_write_consistency_level')

        # Iterates over the graph options and constructs an array containing
        # The graph_options that correlate to graoh read and write consistency levels
        graph_params = [a[2] for a in _graph_options if a[0] in cl_attrs]

        s = self.session
        default_profile = s.cluster.profile_manager.profiles[EXEC_PROFILE_GRAPH_DEFAULT]
        default_graph_opts = default_profile.graph_options
        try:
            # Checks the default graph attributes and ensures that both  graph_read_consistency_level and graph_write_consistency_level
            # Are None by default
            for attr in cl_attrs:
                self.assertIsNone(getattr(default_graph_opts, attr))

            res = s.execute_graph("null")
            for param in graph_params:
                self.assertNotIn(param, res.response_future.message.custom_payload)

            # session defaults are passed
            opts = GraphOptions()
            opts.update(default_graph_opts)
            cl = {0: ConsistencyLevel.ONE, 1: ConsistencyLevel.LOCAL_QUORUM}
            for k, v in cl.items():
                setattr(opts, cl_attrs[k], v)
            default_profile.graph_options = opts

            res = s.execute_graph("null")

            for k, v in cl.items():
                self.assertEqual(res.response_future.message.custom_payload[graph_params[k]], six.b(ConsistencyLevel.value_to_name[v]))

            # passed profile values override session defaults
            cl = {0: ConsistencyLevel.ALL, 1: ConsistencyLevel.QUORUM}
            opts = GraphOptions()
            opts.update(default_graph_opts)
            for k, v in cl.items():
                attr_name = cl_attrs[k]
                setattr(opts, attr_name, v)
                self.assertNotEqual(getattr(default_profile.graph_options, attr_name), getattr(opts, attr_name))
            tmp_profile = s.execution_profile_clone_update(EXEC_PROFILE_GRAPH_DEFAULT, graph_options=opts)
            res = s.execute_graph("null", execution_profile=tmp_profile)

            for k, v in cl.items():
                self.assertEqual(res.response_future.message.custom_payload[graph_params[k]], six.b(ConsistencyLevel.value_to_name[v]))
        finally:
            default_profile.graph_options = default_graph_opts
    def test_consistency_levels(self):
        read_cl = ConsistencyLevel.ONE
        write_cl = ConsistencyLevel.LOCAL_QUORUM

        # set directly
        opts = GraphOptions(graph_read_consistency_level=read_cl, graph_write_consistency_level=write_cl)
        self.assertEqual(opts.graph_read_consistency_level, read_cl)
        self.assertEqual(opts.graph_write_consistency_level, write_cl)

        # mapping from base
        opt_map = opts.get_options_map()
        self.assertEqual(opt_map['graph-read-consistency'], six.b(ConsistencyLevel.value_to_name[read_cl]))
        self.assertEqual(opt_map['graph-write-consistency'], six.b(ConsistencyLevel.value_to_name[write_cl]))

        # empty by default
        new_opts = GraphOptions()
        opt_map = new_opts.get_options_map()
        self.assertNotIn('graph-read-consistency', opt_map)
        self.assertNotIn('graph-write-consistency', opt_map)

        # set from other
        opt_map = new_opts.get_options_map(opts)
        self.assertEqual(opt_map['graph-read-consistency'], six.b(ConsistencyLevel.value_to_name[read_cl]))
        self.assertEqual(opt_map['graph-write-consistency'], six.b(ConsistencyLevel.value_to_name[write_cl]))
    def test_set_attr(self):
        expected = 'test@@@@'
        opts = GraphOptions(graph_name=expected)
        self.assertEqual(opts.graph_name, six.b(expected))
        expected = 'somethingelse####'
        opts.graph_name = expected
        self.assertEqual(opts.graph_name, six.b(expected))

        # will update options with set value
        another = GraphOptions()
        self.assertIsNone(another.graph_name)
        another.update(opts)
        self.assertEqual(another.graph_name, six.b(expected))

        opts.graph_name = None
        self.assertIsNone(opts.graph_name)
        # will not update another with its set-->unset value
        another.update(opts)
        self.assertEqual(another.graph_name, six.b(expected))  # remains unset
        opt_map = another.get_options_map(opts)
        self.assertEqual(opt_map, another._graph_options)
Beispiel #13
0
def get_result():
    # create the default execution profile pointing at a specific graph
    graph_name = 'test'
    ep = GraphExecutionProfile(graph_options=GraphOptions(
        graph_name=graph_name))
    cluster = Cluster(execution_profiles={EXEC_PROFILE_GRAPH_DEFAULT: ep})
    session = cluster.connect()

    # use the system execution profile (or one with no graph_options.graph_name set) when accessing the system API
    session.execute_graph("system.graph(name).ifNotExists().create()",
                          {'name': graph_name},
                          execution_profile=EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT)

    # ... set dev mode or configure graph schema ...

    # // Property Labels
    result = session.execute_graph(
        'schema.propertyKey("genreId").Text().create()')
    result = session.execute_graph(
        'schema.propertyKey("name").Text().create()')

    # // Vertex labels
    # schema.vertexLabel("movie").properties("movieId","title","year").create()
    # schema.vertexLabel("person").properties("personId","name").create()
    result = session.execute_graph(
        'schema.vertexLabel("genre").properties("genreId","name").create()')

    # // Edge labels
    # schema.edgeLabel("director").single().connection("movie","person").create()
    # schema.edgeLabel("belongsTo").single().connection("movie","genre").create()
    #
    # // Vertex indexes
    # schema.vertexLabel("movie").index("moviesById").materialized().by("movieId").add()
    # schema.vertexLabel("person").index("personsById").materialized().by("personId").add()
    # schema.vertexLabel("genre").index("genresByName").materialized().by("name").add()

    result = session.execute_graph(
        'graph.addVertex(label,"genre","genreId","g2","name","Adventure")')
    data = ''
    for r in result:
        data = r

    # Drop the graph database
    session.execute_graph("system.graph(name).drop()", {'name': graph_name},
                          execution_profile=EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT)
    #Test
    return str(data)
Beispiel #14
0
    def test_graph_source_convenience_attributes(self):
        opts = GraphOptions()
        self.assertEqual(opts.graph_source, b'g')
        self.assertFalse(opts.is_analytics_source)
        self.assertTrue(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)

        opts.set_source_default()
        self.assertIsNotNone(opts.graph_source)
        self.assertFalse(opts.is_analytics_source)
        self.assertFalse(opts.is_graph_source)
        self.assertTrue(opts.is_default_source)

        opts.set_source_analytics()
        self.assertIsNotNone(opts.graph_source)
        self.assertTrue(opts.is_analytics_source)
        self.assertFalse(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)

        opts.set_source_graph()
        self.assertIsNotNone(opts.graph_source)
        self.assertFalse(opts.is_analytics_source)
        self.assertTrue(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)
Beispiel #15
0
    def __init__(self,
                 load_balancing_policy=None,
                 retry_policy=None,
                 consistency_level=ConsistencyLevel.LOCAL_ONE,
                 serial_consistency_level=None,
                 request_timeout=30.0,
                 row_factory=graph_object_row_factory,
                 graph_options=None):
        """
        Default execution profile for graph execution.

        See `ExecutionProfile <http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.ExecutionProfile>`_
        for base attributes.

        In addition to default parameters shown in the signature, this profile also defaults ``retry_policy`` to
        :class:`dse.policies.NeverRetryPolicy`.
        """
        retry_policy = retry_policy or NeverRetryPolicy()
        super(GraphExecutionProfile,
              self).__init__(load_balancing_policy, retry_policy,
                             consistency_level, serial_consistency_level,
                             request_timeout, row_factory)
        self.graph_options = graph_options or GraphOptions(
            graph_source=b'g', graph_language=b'gremlin-groovy')
    def test_graph_source_convenience_attributes(self):
        opts = GraphOptions()
        self.assertEqual(opts.graph_source, b'g')
        self.assertFalse(opts.is_analytics_source)
        self.assertTrue(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)

        opts.set_source_default()
        self.assertIsNotNone(opts.graph_source)
        self.assertFalse(opts.is_analytics_source)
        self.assertFalse(opts.is_graph_source)
        self.assertTrue(opts.is_default_source)

        opts.set_source_analytics()
        self.assertIsNotNone(opts.graph_source)
        self.assertTrue(opts.is_analytics_source)
        self.assertFalse(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)

        opts.set_source_graph()
        self.assertIsNotNone(opts.graph_source)
        self.assertFalse(opts.is_analytics_source)
        self.assertTrue(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)
Beispiel #17
0
 def test_update(self):
     opts = GraphOptions(**self.api_params)
     new_params = dict((k, str(int(v) + 1)) for k, v in self.api_params.items())
     opts.update(GraphOptions(**new_params))
     self._verify_api_params(opts, new_params)
 def test_update(self):
     opts = GraphOptions(**self.api_params)
     new_params = dict((k, str(int(v) + 1)) for k, v in self.api_params.items())
     opts.update(GraphOptions(**new_params))
     self._verify_api_params(opts, new_params)
Beispiel #19
0
# At the time of this blog post, dse_graph only supports gremlinpython version 3.2.x
# This script was tested using gremlinpython version 3.2.6

from dse.cluster import Cluster, GraphExecutionProfile, EXEC_PROFILE_GRAPH_DEFAULT, EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT
from dse.graph import GraphOptions
from dse_graph import DseGraph
from gremlin_python.process.graph_traversal import __
from gremlin_python.structure.graph import Vertex

graph_name = 'modern'
ep_schema = GraphExecutionProfile(graph_options=GraphOptions(graph_name=graph_name))
ep = DseGraph.create_execution_profile(graph_name)

cluster = Cluster(execution_profiles={'schema': ep_schema, EXEC_PROFILE_GRAPH_DEFAULT: ep})
session = cluster.connect()

# Define schema
session.execute_graph("system.graph(name).create()", { 'name': graph_name }, execution_profile = EXEC_PROFILE_GRAPH_SYSTEM_DEFAULT)
session.execute_graph("schema.propertyKey('neighborhood').Bigint().create()", execution_profile = 'schema')
session.execute_graph("schema.propertyKey('name').Text().create()", execution_profile = 'schema')
session.execute_graph("schema.propertyKey('age').Bigint().create()", execution_profile = 'schema')
session.execute_graph("schema.propertyKey('weight').Float().create()", execution_profile = 'schema')
session.execute_graph("schema.vertexLabel('person').partitionKey('neighborhood').clusteringKey('name').properties('age').create()", execution_profile = 'schema')
session.execute_graph("schema.edgeLabel('knows').properties('weight').connection('person', 'person').create()", execution_profile = 'schema')

# Execute batch
batch = DseGraph.batch()
batch.add(__.addV('person').property('neighborhood', 0).property('name', 'bob').property('age', 23))
batch.add(__.addV('person').property('neighborhood', 0).property('name', 'alice').property('age', 21))
batch.add(__.addE('knows')
        .from_(Vertex({ 'neighborhood': 0, 'name': 'bob', '~label' : 'person' }))
Beispiel #20
0
    def test_set_attr(self):
        expected = 'test@@@@'
        opts = GraphOptions(graph_name=expected)
        self.assertEqual(opts.graph_name, six.b(expected))
        expected = 'somethingelse####'
        opts.graph_name = expected
        self.assertEqual(opts.graph_name, six.b(expected))

        # will update options with set value
        another = GraphOptions()
        self.assertIsNone(another.graph_name)
        another.update(opts)
        self.assertEqual(another.graph_name, six.b(expected))

        opts.graph_name = None
        self.assertIsNone(opts.graph_name)
        # will not update another with its set-->unset value
        another.update(opts)
        self.assertEqual(another.graph_name, six.b(expected))  # remains unset
        opt_map = another.get_options_map(opts)
        self.assertEqual(opt_map, another._graph_options)
Beispiel #21
0
 def test_del_attr(self):
     opts = GraphOptions(**self.api_params)
     test_params = self.api_params.copy()
     del test_params['graph_source']
     del opts.graph_source
     self._verify_api_params(opts, test_params)