DataStax Academy FAQ

DataStax Academy migrated to a new learning management system (LMS) in July 2020. We are also moving to a new Cassandra Certification process so there are changes to exam bookings, voucher system and issuing of certificates.

Check out the Academy FAQ pages for answers to your questions:


question

josemotta_190638 avatar image
josemotta_190638 asked ·

Configure access to use both CQL and graph in the same app/api

Current Workshop Series examples uses CQL only. How to configure access to use both CQL and graph drivers at same app?

cassandragraph
1 comment
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Could you please clarify if you're looking for driver usage examples for DSE Graph?

0 Likes 0 · ·

1 Answer

bettina.swynnerton avatar image
bettina.swynnerton answered ·

Hi @josemotta_190638,

Here is an example accessing a DSE 6.8 core graph in three different ways, all using the same driver Datastax Java driver 4.8.0.

The three different ways are:

- via CQL, select rows from a vertex table

- via String API with a string graph traversal statement

- via Fluent API with a fluent graph traversal

import com.datastax.dse.driver.api.core.graph.FluentGraphStatement;
import com.datastax.dse.driver.api.core.graph.GraphNode;
import com.datastax.dse.driver.api.core.graph.GraphResultSet;
import com.datastax.dse.driver.api.core.graph.ScriptGraphStatement;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import static com.datastax.dse.driver.api.core.graph.DseGraph.g;

public class SimpleTraversal {


    public static void main(String[] args) {

        try (CqlSession session = CqlSession.builder().build()) {

            //CQL query

            String script1 =  "select * from friends.person";

            ResultSet result1 = session.execute(script1);

            for (Row row : result1) {
                System.out.println(row.getString("person_id"));

            }

            //String API

            String script2 = "g.V()";

            ScriptGraphStatement statement =
                    ScriptGraphStatement.builder(script2).build();


            GraphResultSet result2 = session.execute(statement);
            for (GraphNode node : result2) {
                System.out.println(node);

            }

            //Fluent API

            GraphTraversal<Vertex,Vertex> traversal = g.V();

            FluentGraphStatement statement2 = FluentGraphStatement.newInstance(traversal);

            GraphResultSet result3 = session.execute(statement2);
            for (GraphNode node : result3) {
                System.out.println(node);

            }

        }
    }
}


You can pass configurations to the driver by using the application.conf

In there, I have defined the basic contact point and local datacenter, as well as the graphname and traversal source g.

See here for the driver docs:

https://docs.datastax.com/en/developer/java-driver/4.8/

I hope this helps, let me know if you have further questions.

4 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks @bettina.swynnerton, good news!

I noticed that the java driver is version 4.8 and python is still version 3.24. Is this the latest version? Does this Python driver also work for CQL and graph?

0 Likes 0 · ·

Yes, 3.24 is the latest Python driver version, and yes, you can use the same driver for CQL and graph queries. If needed, I can create you a similar example as for Java.

Have a look at the documentation, too:

https://docs.datastax.com/en/developer/python-driver/3.24/graph/


0 Likes 0 · ·
josemotta_190638 avatar image josemotta_190638 bettina.swynnerton ♦♦ ·

Hi @bettina.swynnerton! Would you please help with this error? I did some changes to connect to a local Cassandra cluster built with Kind and Tilt.

Exception has occurred: UnresolvableContactPoints {} File "e:_git\t1-astra\server\dao\session_manager.py", line 77, in connect cluster = Cluster(contact_points=connectionPoint, execution_profiles={'core': ep_graphson3}) File "e:_git\t1-astra\server\service\astra_service.py", line 32, in connect return self._session_manager.connect() File "e:_git\t1-astra\server\app.py", line 37, in hello astra_service.connect()

Follows the dao/session_manager.py link:

https://github.com/bampli/t1-astra/blob/master/server/dao/session_manager.py

I also changed server/app.py:

https://github.com/bampli/t1-astra/blob/master/server/app.py

0 Likes 0 · ·
Show more comments