Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Workshop desarrollo Cassandra con el driver Java

450 views

Published on

Workshop desarrollo Cassandra con el driver Java

  • Be the first to comment

  • Be the first to like this

Workshop desarrollo Cassandra con el driver Java

  1. 1. Workshop desarrollo Cassandra Java José Hernández Isthari jose.Hernandez@isthari.com
  2. 2. Dependencias  Maven <dependency> <groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-core</artifactId> <version>2.0.1</version> </dependency>
  3. 3. Cluster de cassandra
  4. 4. Conexión  Cluster Cluster cluster = Cluster.builder() .addContactPoint(“10.202.203.105") .addContactPoint(“10.202.203.106") .build();
  5. 5. Conexión  Cluster Cluster cluster = Cluster.builder() .addContactPoint(“10.202.203.105") .addContactPoint(“10.202.203.106") .build();  Auto – discover New Cassandra host /10.202.203.108 New Cassandra host /10.202.203.109 New Cassandra host /10.202.203.111 …
  6. 6. Desconexión  Cluster Cluster cluster = Cluster.builder() .addContactPoint(“10.202.203.105") .addContactPoint(“10.202.203.106") .build();  Desconectar Cluster.close();
  7. 7. Statements  Conectar a un keyspace Session session = cluster.connect("webinar");
  8. 8. Statements  Conectar a un keyspace Session session = cluster.connect("webinar");  DDL session.execute("create table usuarios ( id uuid , nombre text, primary key (id)) ");
  9. 9. Statements  Conectar a un keyspace Session session = cluster.connect("webinar");  DDL session.execute("create table usuarios ( id uuid , nombre text, primary key (id)) ");  DML session.execute("insert into usuarios (id, nombre) values (18c8dfee-…, 'Juan')");
  10. 10. Query ResultSet resultSet = session.execute("select * from usuarios");
  11. 11. Query ResultSet resultSet = session.execute("select * from usuarios"); for (Row row : resultSet){ … }
  12. 12. Query ResultSet resultSet = session.execute("select * from usuarios"); for (Row row : resultSet){ LOGGER.info(row.getUUID("id")+" - "+ row.getString("nombre")); }
  13. 13. Bound statement PreparedStatement stmt = session.prepare( "insert into usuarios (id, nombre) values (?,?)");
  14. 14. Bound statement PreparedStatement stmt = session.prepare( "insert into usuarios (id, nombre) values (?,?)"); session.execute( stmt.bind(UUID.randomUUID(), "Jose"));
  15. 15. Async queries
  16. 16. Async queries
  17. 17. Async queries ResultSetFuture resultSet1 = session.executeAsync("select … ResultSetFuture resultSet2 = session.executeAsync("select … ResultSetFuture resultSet3 = session.executeAsync("select …
  18. 18. Async queries ResultSetFuture resultSet1 = session.executeAsync("select … ResultSetFuture resultSet2 = session.executeAsync("select … ResultSetFuture resultSet3 = session.executeAsync("select … for (Row row : resultSet1.getUninterruptibly()){ ... }
  19. 19. Async queries ResultSetFuture resultSet1 = session.executeAsync("select … ResultSetFuture resultSet2 = session.executeAsync("select … ResultSetFuture resultSet3 = session.executeAsync("select … for (Row row : resultSet1.getUninterruptibly()){ ... } for (Row row : resultSet2 .getUninterruptibly(1000, TimeUnit.MILLISECONDS)){ ... }
  20. 20. Query builder Statement stmt1 = QueryBuilder .select()
  21. 21. Query builder Statement stmt1 = QueryBuilder .select() .from("usuarios")
  22. 22. Query builder Statement stmt1 = QueryBuilder .select() .from("usuarios") .where(eq("id", UUID.fromString("0bd4f5c9…")));
  23. 23. Query builder Statement stmt1 = QueryBuilder .select() .from("usuarios") .where(eq("id", UUID.fromString("0bd4f5c9…"))); session.execute(stmt1);
  24. 24. Query builder Statement stmt = QueryBuilder.insertInto(“usuarios")
  25. 25. Query builder Statement stmt = QueryBuilder.insertInto(“usuarios") .value("id", UUID.randomUUID()) .value(“nombre", “Felipe")
  26. 26. Query builder Statement stmt = QueryBuilder.insertInto(“usuarios") .value("id", UUID.randomUUID()) .value(“nombre", “Felipe") .setConsistencyLevel(ConsistencyLevel.ANY)
  27. 27. Query builder Statement stmt = QueryBuilder.insertInto(“usuarios") .value("id", UUID.randomUUID()) .value(“nombre", “Felipe") .setConsistencyLevel(ConsistencyLevel.ANY); session.execute(stmt);
  28. 28. Tracing stmt.enableTracing(); ExecutionInfo info = session.execute(stmt).getExecutionInfo(); QueryTrace trace = info.getQueryTrace ();
  29. 29. Load balancing policy  RoundRobinPolicy  DCAwareRoundRobinPolicy  TokenAware
  30. 30. Load balancing policy  RoundRobinPolicy  DCAwareRoundRobinPolicy  TokenAware Ejemplo: DCAwareRoundRobinPolicy Cluster cluster = Cluster.builder() … .withLoadBalancingPolicy( new DCAwareRoundRobinPolicy("localDC"))
  31. 31. Preguntas y sugerencias Gracias por asistir

×