|
| 1 | +package com.datastax.oss.driver.examples.basic; |
| 2 | + |
| 3 | +import com.datastax.oss.driver.api.core.CqlIdentifier; |
| 4 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 5 | +import com.datastax.oss.driver.api.core.DefaultProtocolVersion; |
| 6 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 7 | +import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
| 8 | +import com.datastax.oss.driver.api.core.cql.TraceEvent; |
| 9 | +import com.datastax.oss.driver.api.core.metadata.Metadata; |
| 10 | +import com.datastax.oss.driver.api.core.metadata.Node; |
| 11 | +import com.datastax.oss.driver.api.core.metadata.TokenMap; |
| 12 | +import com.datastax.oss.driver.api.core.metadata.token.Token; |
| 13 | +import com.datastax.oss.driver.api.core.type.codec.TypeCodecs; |
| 14 | +import java.nio.ByteBuffer; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +/** |
| 18 | + * Demonstrates usage of TokenMap and NodeShardingInfo Needs a Scylla cluster to be running locally |
| 19 | + * or adjustment of session builder. |
| 20 | + */ |
| 21 | +public class TokenMapAndShardIdLookup { |
| 22 | + |
| 23 | + private static String CREATE_KEYSPACE = |
| 24 | + "CREATE KEYSPACE IF NOT EXISTS tokenmap_example_ks " |
| 25 | + + "WITH replication = {" |
| 26 | + + "'class': 'SimpleStrategy', " |
| 27 | + + "'replication_factor': 1" |
| 28 | + + "}"; |
| 29 | + |
| 30 | + private static String CREATE_TABLE = |
| 31 | + "" |
| 32 | + + "CREATE TABLE IF NOT EXISTS tokenmap_example_ks.example_tab (" |
| 33 | + + "my_column bigint," |
| 34 | + + "PRIMARY KEY (my_column)" |
| 35 | + + ")"; |
| 36 | + |
| 37 | + private static String INSERT_COLUMN = |
| 38 | + "INSERT INTO tokenmap_example_ks.example_tab (my_column) VALUES (2)"; |
| 39 | + |
| 40 | + private static String SELECT_COLUMN = |
| 41 | + "SELECT * FROM tokenmap_example_ks.example_tab WHERE my_column = 2"; |
| 42 | + |
| 43 | + private static ByteBuffer PARTITION_KEY = TypeCodecs.BIGINT.encode(2L, DefaultProtocolVersion.V3); |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + |
| 47 | + try (CqlSession session = CqlSession.builder().build()) { |
| 48 | + |
| 49 | + System.out.printf("Connected session: %s%n", session.getName()); |
| 50 | + |
| 51 | + session.execute(CREATE_KEYSPACE); |
| 52 | + session.execute(CREATE_TABLE); |
| 53 | + session.execute(INSERT_COLUMN); |
| 54 | + |
| 55 | + Metadata metadata = session.refreshSchema(); |
| 56 | + |
| 57 | + System.out.println("Prepared example data"); |
| 58 | + |
| 59 | + TokenMap tokenMap = metadata.getTokenMap().get(); |
| 60 | + |
| 61 | + Set<Node> nodes = |
| 62 | + tokenMap.getReplicas(CqlIdentifier.fromCql("tokenmap_example_ks"), PARTITION_KEY); |
| 63 | + System.out.println("Replica set size: " + nodes.size()); |
| 64 | + |
| 65 | + Token token = tokenMap.newToken(PARTITION_KEY); |
| 66 | + assert nodes.size() > 0; |
| 67 | + Node node = nodes.iterator().next(); |
| 68 | + |
| 69 | + assert node.getShardingInfo() != null; |
| 70 | + int shardId = node.getShardingInfo().shardId(token); |
| 71 | + |
| 72 | + System.out.println( |
| 73 | + "Hardcoded partition key should belong to shard number " |
| 74 | + + shardId |
| 75 | + + " (on Node: " |
| 76 | + + node |
| 77 | + + ")"); |
| 78 | + |
| 79 | + System.out.println("You can compare it with SELECT query trace:"); |
| 80 | + // If there is only 1 node, then the SELECT has to hit the one we did shardId calculation for. |
| 81 | + SimpleStatement statement = SimpleStatement.builder(SELECT_COLUMN).setTracing(true).build(); |
| 82 | + ResultSet rs = session.execute(statement); |
| 83 | + |
| 84 | + for (TraceEvent event : rs.getExecutionInfo().getQueryTrace().getEvents()) { |
| 85 | + System.out.println(event); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments