How to connect Cassandra using Java class



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








29















I am doing this to connect cassandra.But my code is returning an error.. Here is my code



public class CassandraConnection 

public static void main(String args)
String serverIp = "166.78.10.41";
String keyspace = "gamma";
CassandraConnection connection;

Cluster cluster = Cluster.builder()
.addContactPoints(serverIp)
.build();

Session session = cluster.connect(keyspace);


String cqlStatement = "SELECT * FROM TestCF";
for (Row row : session.execute(cqlStatement))
System.out.println(row.toString());






this is the error log ..




Failed to execute goal on project CassandraConnection: Could not resolve dependencies for project com.mycompany:CassandraConnection:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT, org.scalaz:scalaz-effect_2.9.3:jar:7.1.0-SNAPSHOT: Could not find artifact org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT -> [Help 1]



To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.



For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException











share|improve this question






























    29















    I am doing this to connect cassandra.But my code is returning an error.. Here is my code



    public class CassandraConnection 

    public static void main(String args)
    String serverIp = "166.78.10.41";
    String keyspace = "gamma";
    CassandraConnection connection;

    Cluster cluster = Cluster.builder()
    .addContactPoints(serverIp)
    .build();

    Session session = cluster.connect(keyspace);


    String cqlStatement = "SELECT * FROM TestCF";
    for (Row row : session.execute(cqlStatement))
    System.out.println(row.toString());






    this is the error log ..




    Failed to execute goal on project CassandraConnection: Could not resolve dependencies for project com.mycompany:CassandraConnection:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT, org.scalaz:scalaz-effect_2.9.3:jar:7.1.0-SNAPSHOT: Could not find artifact org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT -> [Help 1]



    To see the full stack trace of the errors, re-run Maven with the -e switch.
    Re-run Maven using the -X switch to enable full debug logging.



    For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException











    share|improve this question


























      29












      29








      29


      14






      I am doing this to connect cassandra.But my code is returning an error.. Here is my code



      public class CassandraConnection 

      public static void main(String args)
      String serverIp = "166.78.10.41";
      String keyspace = "gamma";
      CassandraConnection connection;

      Cluster cluster = Cluster.builder()
      .addContactPoints(serverIp)
      .build();

      Session session = cluster.connect(keyspace);


      String cqlStatement = "SELECT * FROM TestCF";
      for (Row row : session.execute(cqlStatement))
      System.out.println(row.toString());






      this is the error log ..




      Failed to execute goal on project CassandraConnection: Could not resolve dependencies for project com.mycompany:CassandraConnection:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT, org.scalaz:scalaz-effect_2.9.3:jar:7.1.0-SNAPSHOT: Could not find artifact org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT -> [Help 1]



      To see the full stack trace of the errors, re-run Maven with the -e switch.
      Re-run Maven using the -X switch to enable full debug logging.



      For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException











      share|improve this question
















      I am doing this to connect cassandra.But my code is returning an error.. Here is my code



      public class CassandraConnection 

      public static void main(String args)
      String serverIp = "166.78.10.41";
      String keyspace = "gamma";
      CassandraConnection connection;

      Cluster cluster = Cluster.builder()
      .addContactPoints(serverIp)
      .build();

      Session session = cluster.connect(keyspace);


      String cqlStatement = "SELECT * FROM TestCF";
      for (Row row : session.execute(cqlStatement))
      System.out.println(row.toString());






      this is the error log ..




      Failed to execute goal on project CassandraConnection: Could not resolve dependencies for project com.mycompany:CassandraConnection:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT, org.scalaz:scalaz-effect_2.9.3:jar:7.1.0-SNAPSHOT: Could not find artifact org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT -> [Help 1]



      To see the full stack trace of the errors, re-run Maven with the -e switch.
      Re-run Maven using the -X switch to enable full debug logging.



      For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException








      java cassandra






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 17 '17 at 2:15









      Matt Davis

      191139




      191139










      asked Jun 1 '13 at 8:00









      MandrekMandrek

      4041821




      4041821






















          2 Answers
          2






          active

          oldest

          votes


















          128














          Did you do any research on the matter?



          Picking a driver



          You need a way to communicate with cassandra, best option is to use a high level API. You have a wide range of choices here but when we look at it from a high level prespective there are really two choices.




          1. CQL based drivers - Higher level abstraction of what thrift does. Also the newer tool, companies providing support / documentation for cassandra recommend that new cassandra applications are CQL based.


          2. Thrift based drives - Have access to low level storage, so its easier to get things wrong.

          I'll use datastax's CQL driver.



          Download and build the driver from datastax's github repo OR use maven and add the following dependencies:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>2.1.3</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>2.1.2</version>
          </dependency>


          Picking maven is a good idea, as it will manage all your dependencies for you, but if you dont use maven, at least you will learn about managing jars and reading through stack-traces.






          Code



          The driver's documentation is coming along nicely. If you get stuck read through it, the documentation contains lots of examples.



          I'll use the following two variables throughout the examples.



          String serverIP = "127.0.0.1";
          String keyspace = "system";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();

          Session session = cluster.connect(keyspace);

          // you are now connected to the cluster, congrats!


          Read



          String cqlStatement = "SELECT * FROM local";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());



          Create/Update/Delete



          // for all three it works the same way (as a note the 'system' keyspace cant 
          // be modified by users so below im using a keyspace name 'exampkeyspace' and
          // a table (or columnfamily) called users

          String cqlStatementC = "INSERT INTO exampkeyspace.users (username, password) " +
          "VALUES ('Serenity', 'fa3dfQefx')";

          String cqlStatementU = "UPDATE exampkeyspace.users " +
          "SET password = 'zzaEcvAf32hla'," +
          "WHERE username = 'Serenity';";

          String cqlStatementD = "DELETE FROM exampkeyspace.users " +
          "WHERE username = 'Serenity';";

          session.execute(cqlStatementC); // interchangeable, put any of the statements u wish.





          Other useful code



          Creating a Keyspace



          String cqlStatement = "CREATE KEYSPACE exampkeyspace WITH " + 
          "replication = 'class':'SimpleStrategy','replication_factor':1";

          session.execute(cqlStatement);


          Creating a ColumnFamily (aka table)



          // based on the above keyspace, we would change the cluster and session as follows:
          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();
          Session session = cluster.connect("exampkeyspace");

          String cqlStatement = "CREATE TABLE users (" +
          " username varchar PRIMARY KEY," +
          " password varchar " +
          ");";

          session.execute(cqlStatement);





          share|improve this answer




















          • 7





            This answer is freaking amazing. I wish I could +1 it about 30 more times.

            – crush
            Sep 14 '13 at 13:49


















          0














          To connect with cassandra from a java program, you need to add some basic dependency to the program. Add the following dependencies to the program.
          Maven dependencies List:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.25</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>1.7.25</version>
          <scope>test</scope>
          </dependency>

          <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>21.0</version>
          </dependency>

          <dependency>
          <groupId>io.netty</groupId>
          <artifactId>netty-all</artifactId>
          <version>4.1.20.Final</version>
          </dependency>

          <dependency>
          <groupId>com.codahale.metrics</groupId>
          <artifactId>metrics-core</artifactId>
          <version>3.0.2</version>
          </dependency>


          Simple java program to connect with cassandra to a keyspace and to retrieve the values of a table



          import com.datastax.driver.core.Cluster;
          import com.datastax.driver.core.Row;
          import com.datastax.driver.core.Session;

          public class Test

          public static void main(String args)
          String serverIp = "127.0.0.1";
          String keyspace = "test";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIp)
          .build();

          Session session = cluster.connect(keyspace);


          String cqlStatement = "SELECT * FROM emp";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());


          session.close();




          Git Hub Repo for the program as maven project: GitURL






          share|improve this answer




















          • 1





            I'm sorry to ask, but what this answer adds to already existing one? (from 5 years ago)

            – Alex Ott
            Oct 24 '18 at 17:44











          • Yea, but the existing answer has only two dependencies mentioned and with those dependencies we wont be able to connect with cassandra. And right dependencies with proper version will connect to the DB. That's why mentioned the required dependencies which works properly with respective to versions also.

            – Vishnu Prabhakar
            Oct 25 '18 at 6:13











          • In reality, you only need one dependency - to cassandra-driver-core... Guava is dependency of java-driver-mapping. And in rare cases you need to use shaded jar: docs.datastax.com/en/developer/java-driver/3.6/manual/…

            – Alex Ott
            Oct 25 '18 at 6:32











          • When i try to run a program to connect with cassandra, i have included all the mentioned jars to connect successfully. With only cassandra-driver-core and cassandra-driver-mapping I'm getting run time errors to include the other mentioned jars. So posted the list of dependencies to connect with cassandra without any errors

            – Vishnu Prabhakar
            Oct 25 '18 at 6:51












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16870502%2fhow-to-connect-cassandra-using-java-class%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          128














          Did you do any research on the matter?



          Picking a driver



          You need a way to communicate with cassandra, best option is to use a high level API. You have a wide range of choices here but when we look at it from a high level prespective there are really two choices.




          1. CQL based drivers - Higher level abstraction of what thrift does. Also the newer tool, companies providing support / documentation for cassandra recommend that new cassandra applications are CQL based.


          2. Thrift based drives - Have access to low level storage, so its easier to get things wrong.

          I'll use datastax's CQL driver.



          Download and build the driver from datastax's github repo OR use maven and add the following dependencies:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>2.1.3</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>2.1.2</version>
          </dependency>


          Picking maven is a good idea, as it will manage all your dependencies for you, but if you dont use maven, at least you will learn about managing jars and reading through stack-traces.






          Code



          The driver's documentation is coming along nicely. If you get stuck read through it, the documentation contains lots of examples.



          I'll use the following two variables throughout the examples.



          String serverIP = "127.0.0.1";
          String keyspace = "system";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();

          Session session = cluster.connect(keyspace);

          // you are now connected to the cluster, congrats!


          Read



          String cqlStatement = "SELECT * FROM local";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());



          Create/Update/Delete



          // for all three it works the same way (as a note the 'system' keyspace cant 
          // be modified by users so below im using a keyspace name 'exampkeyspace' and
          // a table (or columnfamily) called users

          String cqlStatementC = "INSERT INTO exampkeyspace.users (username, password) " +
          "VALUES ('Serenity', 'fa3dfQefx')";

          String cqlStatementU = "UPDATE exampkeyspace.users " +
          "SET password = 'zzaEcvAf32hla'," +
          "WHERE username = 'Serenity';";

          String cqlStatementD = "DELETE FROM exampkeyspace.users " +
          "WHERE username = 'Serenity';";

          session.execute(cqlStatementC); // interchangeable, put any of the statements u wish.





          Other useful code



          Creating a Keyspace



          String cqlStatement = "CREATE KEYSPACE exampkeyspace WITH " + 
          "replication = 'class':'SimpleStrategy','replication_factor':1";

          session.execute(cqlStatement);


          Creating a ColumnFamily (aka table)



          // based on the above keyspace, we would change the cluster and session as follows:
          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();
          Session session = cluster.connect("exampkeyspace");

          String cqlStatement = "CREATE TABLE users (" +
          " username varchar PRIMARY KEY," +
          " password varchar " +
          ");";

          session.execute(cqlStatement);





          share|improve this answer




















          • 7





            This answer is freaking amazing. I wish I could +1 it about 30 more times.

            – crush
            Sep 14 '13 at 13:49















          128














          Did you do any research on the matter?



          Picking a driver



          You need a way to communicate with cassandra, best option is to use a high level API. You have a wide range of choices here but when we look at it from a high level prespective there are really two choices.




          1. CQL based drivers - Higher level abstraction of what thrift does. Also the newer tool, companies providing support / documentation for cassandra recommend that new cassandra applications are CQL based.


          2. Thrift based drives - Have access to low level storage, so its easier to get things wrong.

          I'll use datastax's CQL driver.



          Download and build the driver from datastax's github repo OR use maven and add the following dependencies:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>2.1.3</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>2.1.2</version>
          </dependency>


          Picking maven is a good idea, as it will manage all your dependencies for you, but if you dont use maven, at least you will learn about managing jars and reading through stack-traces.






          Code



          The driver's documentation is coming along nicely. If you get stuck read through it, the documentation contains lots of examples.



          I'll use the following two variables throughout the examples.



          String serverIP = "127.0.0.1";
          String keyspace = "system";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();

          Session session = cluster.connect(keyspace);

          // you are now connected to the cluster, congrats!


          Read



          String cqlStatement = "SELECT * FROM local";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());



          Create/Update/Delete



          // for all three it works the same way (as a note the 'system' keyspace cant 
          // be modified by users so below im using a keyspace name 'exampkeyspace' and
          // a table (or columnfamily) called users

          String cqlStatementC = "INSERT INTO exampkeyspace.users (username, password) " +
          "VALUES ('Serenity', 'fa3dfQefx')";

          String cqlStatementU = "UPDATE exampkeyspace.users " +
          "SET password = 'zzaEcvAf32hla'," +
          "WHERE username = 'Serenity';";

          String cqlStatementD = "DELETE FROM exampkeyspace.users " +
          "WHERE username = 'Serenity';";

          session.execute(cqlStatementC); // interchangeable, put any of the statements u wish.





          Other useful code



          Creating a Keyspace



          String cqlStatement = "CREATE KEYSPACE exampkeyspace WITH " + 
          "replication = 'class':'SimpleStrategy','replication_factor':1";

          session.execute(cqlStatement);


          Creating a ColumnFamily (aka table)



          // based on the above keyspace, we would change the cluster and session as follows:
          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();
          Session session = cluster.connect("exampkeyspace");

          String cqlStatement = "CREATE TABLE users (" +
          " username varchar PRIMARY KEY," +
          " password varchar " +
          ");";

          session.execute(cqlStatement);





          share|improve this answer




















          • 7





            This answer is freaking amazing. I wish I could +1 it about 30 more times.

            – crush
            Sep 14 '13 at 13:49













          128












          128








          128







          Did you do any research on the matter?



          Picking a driver



          You need a way to communicate with cassandra, best option is to use a high level API. You have a wide range of choices here but when we look at it from a high level prespective there are really two choices.




          1. CQL based drivers - Higher level abstraction of what thrift does. Also the newer tool, companies providing support / documentation for cassandra recommend that new cassandra applications are CQL based.


          2. Thrift based drives - Have access to low level storage, so its easier to get things wrong.

          I'll use datastax's CQL driver.



          Download and build the driver from datastax's github repo OR use maven and add the following dependencies:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>2.1.3</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>2.1.2</version>
          </dependency>


          Picking maven is a good idea, as it will manage all your dependencies for you, but if you dont use maven, at least you will learn about managing jars and reading through stack-traces.






          Code



          The driver's documentation is coming along nicely. If you get stuck read through it, the documentation contains lots of examples.



          I'll use the following two variables throughout the examples.



          String serverIP = "127.0.0.1";
          String keyspace = "system";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();

          Session session = cluster.connect(keyspace);

          // you are now connected to the cluster, congrats!


          Read



          String cqlStatement = "SELECT * FROM local";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());



          Create/Update/Delete



          // for all three it works the same way (as a note the 'system' keyspace cant 
          // be modified by users so below im using a keyspace name 'exampkeyspace' and
          // a table (or columnfamily) called users

          String cqlStatementC = "INSERT INTO exampkeyspace.users (username, password) " +
          "VALUES ('Serenity', 'fa3dfQefx')";

          String cqlStatementU = "UPDATE exampkeyspace.users " +
          "SET password = 'zzaEcvAf32hla'," +
          "WHERE username = 'Serenity';";

          String cqlStatementD = "DELETE FROM exampkeyspace.users " +
          "WHERE username = 'Serenity';";

          session.execute(cqlStatementC); // interchangeable, put any of the statements u wish.





          Other useful code



          Creating a Keyspace



          String cqlStatement = "CREATE KEYSPACE exampkeyspace WITH " + 
          "replication = 'class':'SimpleStrategy','replication_factor':1";

          session.execute(cqlStatement);


          Creating a ColumnFamily (aka table)



          // based on the above keyspace, we would change the cluster and session as follows:
          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();
          Session session = cluster.connect("exampkeyspace");

          String cqlStatement = "CREATE TABLE users (" +
          " username varchar PRIMARY KEY," +
          " password varchar " +
          ");";

          session.execute(cqlStatement);





          share|improve this answer















          Did you do any research on the matter?



          Picking a driver



          You need a way to communicate with cassandra, best option is to use a high level API. You have a wide range of choices here but when we look at it from a high level prespective there are really two choices.




          1. CQL based drivers - Higher level abstraction of what thrift does. Also the newer tool, companies providing support / documentation for cassandra recommend that new cassandra applications are CQL based.


          2. Thrift based drives - Have access to low level storage, so its easier to get things wrong.

          I'll use datastax's CQL driver.



          Download and build the driver from datastax's github repo OR use maven and add the following dependencies:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>2.1.3</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>2.1.2</version>
          </dependency>


          Picking maven is a good idea, as it will manage all your dependencies for you, but if you dont use maven, at least you will learn about managing jars and reading through stack-traces.






          Code



          The driver's documentation is coming along nicely. If you get stuck read through it, the documentation contains lots of examples.



          I'll use the following two variables throughout the examples.



          String serverIP = "127.0.0.1";
          String keyspace = "system";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();

          Session session = cluster.connect(keyspace);

          // you are now connected to the cluster, congrats!


          Read



          String cqlStatement = "SELECT * FROM local";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());



          Create/Update/Delete



          // for all three it works the same way (as a note the 'system' keyspace cant 
          // be modified by users so below im using a keyspace name 'exampkeyspace' and
          // a table (or columnfamily) called users

          String cqlStatementC = "INSERT INTO exampkeyspace.users (username, password) " +
          "VALUES ('Serenity', 'fa3dfQefx')";

          String cqlStatementU = "UPDATE exampkeyspace.users " +
          "SET password = 'zzaEcvAf32hla'," +
          "WHERE username = 'Serenity';";

          String cqlStatementD = "DELETE FROM exampkeyspace.users " +
          "WHERE username = 'Serenity';";

          session.execute(cqlStatementC); // interchangeable, put any of the statements u wish.





          Other useful code



          Creating a Keyspace



          String cqlStatement = "CREATE KEYSPACE exampkeyspace WITH " + 
          "replication = 'class':'SimpleStrategy','replication_factor':1";

          session.execute(cqlStatement);


          Creating a ColumnFamily (aka table)



          // based on the above keyspace, we would change the cluster and session as follows:
          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIP)
          .build();
          Session session = cluster.connect("exampkeyspace");

          String cqlStatement = "CREATE TABLE users (" +
          " username varchar PRIMARY KEY," +
          " password varchar " +
          ");";

          session.execute(cqlStatement);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 14 at 18:57









          mkobit

          22.3k693107




          22.3k693107










          answered Jun 1 '13 at 11:04









          Lyuben TodorovLyuben Todorov

          14.1k54366




          14.1k54366







          • 7





            This answer is freaking amazing. I wish I could +1 it about 30 more times.

            – crush
            Sep 14 '13 at 13:49












          • 7





            This answer is freaking amazing. I wish I could +1 it about 30 more times.

            – crush
            Sep 14 '13 at 13:49







          7




          7





          This answer is freaking amazing. I wish I could +1 it about 30 more times.

          – crush
          Sep 14 '13 at 13:49





          This answer is freaking amazing. I wish I could +1 it about 30 more times.

          – crush
          Sep 14 '13 at 13:49













          0














          To connect with cassandra from a java program, you need to add some basic dependency to the program. Add the following dependencies to the program.
          Maven dependencies List:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.25</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>1.7.25</version>
          <scope>test</scope>
          </dependency>

          <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>21.0</version>
          </dependency>

          <dependency>
          <groupId>io.netty</groupId>
          <artifactId>netty-all</artifactId>
          <version>4.1.20.Final</version>
          </dependency>

          <dependency>
          <groupId>com.codahale.metrics</groupId>
          <artifactId>metrics-core</artifactId>
          <version>3.0.2</version>
          </dependency>


          Simple java program to connect with cassandra to a keyspace and to retrieve the values of a table



          import com.datastax.driver.core.Cluster;
          import com.datastax.driver.core.Row;
          import com.datastax.driver.core.Session;

          public class Test

          public static void main(String args)
          String serverIp = "127.0.0.1";
          String keyspace = "test";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIp)
          .build();

          Session session = cluster.connect(keyspace);


          String cqlStatement = "SELECT * FROM emp";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());


          session.close();




          Git Hub Repo for the program as maven project: GitURL






          share|improve this answer




















          • 1





            I'm sorry to ask, but what this answer adds to already existing one? (from 5 years ago)

            – Alex Ott
            Oct 24 '18 at 17:44











          • Yea, but the existing answer has only two dependencies mentioned and with those dependencies we wont be able to connect with cassandra. And right dependencies with proper version will connect to the DB. That's why mentioned the required dependencies which works properly with respective to versions also.

            – Vishnu Prabhakar
            Oct 25 '18 at 6:13











          • In reality, you only need one dependency - to cassandra-driver-core... Guava is dependency of java-driver-mapping. And in rare cases you need to use shaded jar: docs.datastax.com/en/developer/java-driver/3.6/manual/…

            – Alex Ott
            Oct 25 '18 at 6:32











          • When i try to run a program to connect with cassandra, i have included all the mentioned jars to connect successfully. With only cassandra-driver-core and cassandra-driver-mapping I'm getting run time errors to include the other mentioned jars. So posted the list of dependencies to connect with cassandra without any errors

            – Vishnu Prabhakar
            Oct 25 '18 at 6:51
















          0














          To connect with cassandra from a java program, you need to add some basic dependency to the program. Add the following dependencies to the program.
          Maven dependencies List:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.25</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>1.7.25</version>
          <scope>test</scope>
          </dependency>

          <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>21.0</version>
          </dependency>

          <dependency>
          <groupId>io.netty</groupId>
          <artifactId>netty-all</artifactId>
          <version>4.1.20.Final</version>
          </dependency>

          <dependency>
          <groupId>com.codahale.metrics</groupId>
          <artifactId>metrics-core</artifactId>
          <version>3.0.2</version>
          </dependency>


          Simple java program to connect with cassandra to a keyspace and to retrieve the values of a table



          import com.datastax.driver.core.Cluster;
          import com.datastax.driver.core.Row;
          import com.datastax.driver.core.Session;

          public class Test

          public static void main(String args)
          String serverIp = "127.0.0.1";
          String keyspace = "test";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIp)
          .build();

          Session session = cluster.connect(keyspace);


          String cqlStatement = "SELECT * FROM emp";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());


          session.close();




          Git Hub Repo for the program as maven project: GitURL






          share|improve this answer




















          • 1





            I'm sorry to ask, but what this answer adds to already existing one? (from 5 years ago)

            – Alex Ott
            Oct 24 '18 at 17:44











          • Yea, but the existing answer has only two dependencies mentioned and with those dependencies we wont be able to connect with cassandra. And right dependencies with proper version will connect to the DB. That's why mentioned the required dependencies which works properly with respective to versions also.

            – Vishnu Prabhakar
            Oct 25 '18 at 6:13











          • In reality, you only need one dependency - to cassandra-driver-core... Guava is dependency of java-driver-mapping. And in rare cases you need to use shaded jar: docs.datastax.com/en/developer/java-driver/3.6/manual/…

            – Alex Ott
            Oct 25 '18 at 6:32











          • When i try to run a program to connect with cassandra, i have included all the mentioned jars to connect successfully. With only cassandra-driver-core and cassandra-driver-mapping I'm getting run time errors to include the other mentioned jars. So posted the list of dependencies to connect with cassandra without any errors

            – Vishnu Prabhakar
            Oct 25 '18 at 6:51














          0












          0








          0







          To connect with cassandra from a java program, you need to add some basic dependency to the program. Add the following dependencies to the program.
          Maven dependencies List:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.25</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>1.7.25</version>
          <scope>test</scope>
          </dependency>

          <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>21.0</version>
          </dependency>

          <dependency>
          <groupId>io.netty</groupId>
          <artifactId>netty-all</artifactId>
          <version>4.1.20.Final</version>
          </dependency>

          <dependency>
          <groupId>com.codahale.metrics</groupId>
          <artifactId>metrics-core</artifactId>
          <version>3.0.2</version>
          </dependency>


          Simple java program to connect with cassandra to a keyspace and to retrieve the values of a table



          import com.datastax.driver.core.Cluster;
          import com.datastax.driver.core.Row;
          import com.datastax.driver.core.Session;

          public class Test

          public static void main(String args)
          String serverIp = "127.0.0.1";
          String keyspace = "test";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIp)
          .build();

          Session session = cluster.connect(keyspace);


          String cqlStatement = "SELECT * FROM emp";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());


          session.close();




          Git Hub Repo for the program as maven project: GitURL






          share|improve this answer















          To connect with cassandra from a java program, you need to add some basic dependency to the program. Add the following dependencies to the program.
          Maven dependencies List:



          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-core</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>com.datastax.cassandra</groupId>
          <artifactId>cassandra-driver-mapping</artifactId>
          <version>3.6.0</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.25</version>
          </dependency>

          <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-simple</artifactId>
          <version>1.7.25</version>
          <scope>test</scope>
          </dependency>

          <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>21.0</version>
          </dependency>

          <dependency>
          <groupId>io.netty</groupId>
          <artifactId>netty-all</artifactId>
          <version>4.1.20.Final</version>
          </dependency>

          <dependency>
          <groupId>com.codahale.metrics</groupId>
          <artifactId>metrics-core</artifactId>
          <version>3.0.2</version>
          </dependency>


          Simple java program to connect with cassandra to a keyspace and to retrieve the values of a table



          import com.datastax.driver.core.Cluster;
          import com.datastax.driver.core.Row;
          import com.datastax.driver.core.Session;

          public class Test

          public static void main(String args)
          String serverIp = "127.0.0.1";
          String keyspace = "test";

          Cluster cluster = Cluster.builder()
          .addContactPoints(serverIp)
          .build();

          Session session = cluster.connect(keyspace);


          String cqlStatement = "SELECT * FROM emp";
          for (Row row : session.execute(cqlStatement))
          System.out.println(row.toString());


          session.close();




          Git Hub Repo for the program as maven project: GitURL







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 16:25

























          answered Oct 24 '18 at 13:50









          Vishnu PrabhakarVishnu Prabhakar

          3119




          3119







          • 1





            I'm sorry to ask, but what this answer adds to already existing one? (from 5 years ago)

            – Alex Ott
            Oct 24 '18 at 17:44











          • Yea, but the existing answer has only two dependencies mentioned and with those dependencies we wont be able to connect with cassandra. And right dependencies with proper version will connect to the DB. That's why mentioned the required dependencies which works properly with respective to versions also.

            – Vishnu Prabhakar
            Oct 25 '18 at 6:13











          • In reality, you only need one dependency - to cassandra-driver-core... Guava is dependency of java-driver-mapping. And in rare cases you need to use shaded jar: docs.datastax.com/en/developer/java-driver/3.6/manual/…

            – Alex Ott
            Oct 25 '18 at 6:32











          • When i try to run a program to connect with cassandra, i have included all the mentioned jars to connect successfully. With only cassandra-driver-core and cassandra-driver-mapping I'm getting run time errors to include the other mentioned jars. So posted the list of dependencies to connect with cassandra without any errors

            – Vishnu Prabhakar
            Oct 25 '18 at 6:51













          • 1





            I'm sorry to ask, but what this answer adds to already existing one? (from 5 years ago)

            – Alex Ott
            Oct 24 '18 at 17:44











          • Yea, but the existing answer has only two dependencies mentioned and with those dependencies we wont be able to connect with cassandra. And right dependencies with proper version will connect to the DB. That's why mentioned the required dependencies which works properly with respective to versions also.

            – Vishnu Prabhakar
            Oct 25 '18 at 6:13











          • In reality, you only need one dependency - to cassandra-driver-core... Guava is dependency of java-driver-mapping. And in rare cases you need to use shaded jar: docs.datastax.com/en/developer/java-driver/3.6/manual/…

            – Alex Ott
            Oct 25 '18 at 6:32











          • When i try to run a program to connect with cassandra, i have included all the mentioned jars to connect successfully. With only cassandra-driver-core and cassandra-driver-mapping I'm getting run time errors to include the other mentioned jars. So posted the list of dependencies to connect with cassandra without any errors

            – Vishnu Prabhakar
            Oct 25 '18 at 6:51








          1




          1





          I'm sorry to ask, but what this answer adds to already existing one? (from 5 years ago)

          – Alex Ott
          Oct 24 '18 at 17:44





          I'm sorry to ask, but what this answer adds to already existing one? (from 5 years ago)

          – Alex Ott
          Oct 24 '18 at 17:44













          Yea, but the existing answer has only two dependencies mentioned and with those dependencies we wont be able to connect with cassandra. And right dependencies with proper version will connect to the DB. That's why mentioned the required dependencies which works properly with respective to versions also.

          – Vishnu Prabhakar
          Oct 25 '18 at 6:13





          Yea, but the existing answer has only two dependencies mentioned and with those dependencies we wont be able to connect with cassandra. And right dependencies with proper version will connect to the DB. That's why mentioned the required dependencies which works properly with respective to versions also.

          – Vishnu Prabhakar
          Oct 25 '18 at 6:13













          In reality, you only need one dependency - to cassandra-driver-core... Guava is dependency of java-driver-mapping. And in rare cases you need to use shaded jar: docs.datastax.com/en/developer/java-driver/3.6/manual/…

          – Alex Ott
          Oct 25 '18 at 6:32





          In reality, you only need one dependency - to cassandra-driver-core... Guava is dependency of java-driver-mapping. And in rare cases you need to use shaded jar: docs.datastax.com/en/developer/java-driver/3.6/manual/…

          – Alex Ott
          Oct 25 '18 at 6:32













          When i try to run a program to connect with cassandra, i have included all the mentioned jars to connect successfully. With only cassandra-driver-core and cassandra-driver-mapping I'm getting run time errors to include the other mentioned jars. So posted the list of dependencies to connect with cassandra without any errors

          – Vishnu Prabhakar
          Oct 25 '18 at 6:51






          When i try to run a program to connect with cassandra, i have included all the mentioned jars to connect successfully. With only cassandra-driver-core and cassandra-driver-mapping I'm getting run time errors to include the other mentioned jars. So posted the list of dependencies to connect with cassandra without any errors

          – Vishnu Prabhakar
          Oct 25 '18 at 6:51


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16870502%2fhow-to-connect-cassandra-using-java-class%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus