Search in sources :

Example 46 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project dsbulk by datastax.

the class ClusterInformationUtilsTest method should_get_information_about_cluster_with_one_host.

@Test
void should_get_information_about_cluster_with_one_host() {
    // given
    CqlSession session = DriverUtils.mockSession();
    Metadata metadata = session.getMetadata();
    TokenMap tokenMap = metadata.getTokenMap().get();
    when(tokenMap.getPartitionerName()).thenReturn("simple-partitioner");
    Node h1 = mockNode(HOST_ID_1, "1.2.3.4", "dc1");
    when(metadata.getNodes()).thenReturn(ImmutableMap.of(HOST_ID_1, h1));
    // when
    ClusterInformation infoAboutCluster = ClusterInformationUtils.getInfoAboutCluster(session);
    // then
    assertThat(infoAboutCluster.getDataCenters()).containsOnly("dc1");
    assertThat(infoAboutCluster.getPartitioner()).isEqualTo("simple-partitioner");
    assertThat(infoAboutCluster.getNumberOfNodes()).isEqualTo(1);
    assertThat(infoAboutCluster.getNodeInfos()).isEqualTo(Collections.singletonList("address: 1.2.3.4:9042, dseVersion: 6.7.0, cassandraVersion: 3.11.1, dataCenter: dc1"));
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) DriverUtils.mockNode(com.datastax.oss.dsbulk.tests.driver.DriverUtils.mockNode) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.jupiter.api.Test)

Example 47 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project dsbulk by datastax.

the class ClusterInformationUtilsTest method should_log_cluster_information_in_debug_mode.

@Test
void should_log_cluster_information_in_debug_mode(@LogCapture(value = ClusterInformationUtils.class, level = DEBUG) LogInterceptor interceptor) {
    // given
    CqlSession session = DriverUtils.mockSession();
    Metadata metadata = session.getMetadata();
    TokenMap tokenMap = metadata.getTokenMap().get();
    when(tokenMap.getPartitionerName()).thenReturn("simple-partitioner");
    Map<UUID, Node> nodes = IntStream.range(0, 110).mapToObj(i -> mockNode(UUID.randomUUID(), "1.2.3." + i, "dc1")).collect(Collectors.toMap(Node::getHostId, n -> n));
    when(metadata.getNodes()).thenReturn(nodes);
    // when
    ClusterInformationUtils.printDebugInfoAboutCluster(session);
    // then
    assertThat(interceptor).hasMessageContaining("Partitioner: simple-partitioner");
    assertThat(interceptor).hasMessageContaining("Total number of nodes: 110");
    assertThat(interceptor).hasMessageContaining("Nodes:");
    assertThat(interceptor).hasMessageContaining("(Other nodes omitted)");
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) TestAssertions.assertThat(com.datastax.oss.dsbulk.tests.assertions.TestAssertions.assertThat) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) DriverUtils(com.datastax.oss.dsbulk.tests.driver.DriverUtils) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) LogCapture(com.datastax.oss.dsbulk.tests.logging.LogCapture) DriverUtils.mockNode(com.datastax.oss.dsbulk.tests.driver.DriverUtils.mockNode) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Test(org.junit.jupiter.api.Test) DEBUG(org.slf4j.event.Level.DEBUG) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Map(java.util.Map) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) LogInterceptingExtension(com.datastax.oss.dsbulk.tests.logging.LogInterceptingExtension) Collections(java.util.Collections) LogInterceptor(com.datastax.oss.dsbulk.tests.logging.LogInterceptor) Node(com.datastax.oss.driver.api.core.metadata.Node) DriverUtils.mockNode(com.datastax.oss.dsbulk.tests.driver.DriverUtils.mockNode) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) UUID(java.util.UUID) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.jupiter.api.Test)

Example 48 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project dsbulk by datastax.

the class ClusterInformationUtilsTest method should_get_information_about_cluster_with_two_different_dc.

@Test
void should_get_information_about_cluster_with_two_different_dc() {
    // given
    CqlSession session = DriverUtils.mockSession();
    Metadata metadata = session.getMetadata();
    TokenMap tokenMap = metadata.getTokenMap().get();
    when(tokenMap.getPartitionerName()).thenReturn("simple-partitioner");
    Node h1 = mockNode(HOST_ID_1, "1.2.3.4", "dc1");
    Node h2 = mockNode(HOST_ID_2, "1.2.3.5", "dc2");
    when(metadata.getNodes()).thenReturn(ImmutableMap.of(HOST_ID_1, h1, HOST_ID_2, h2));
    // when
    ClusterInformation infoAboutCluster = ClusterInformationUtils.getInfoAboutCluster(session);
    // then
    assertThat(infoAboutCluster.getDataCenters()).containsExactlyInAnyOrder("dc1", "dc2");
    assertThat(infoAboutCluster.getPartitioner()).isEqualTo("simple-partitioner");
    assertThat(infoAboutCluster.getNumberOfNodes()).isEqualTo(2);
    assertThat(infoAboutCluster.getNodeInfos()).isEqualTo(Arrays.asList("address: 1.2.3.4:9042, dseVersion: 6.7.0, cassandraVersion: 3.11.1, dataCenter: dc1", "address: 1.2.3.5:9042, dseVersion: 6.7.0, cassandraVersion: 3.11.1, dataCenter: dc2"));
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) DriverUtils.mockNode(com.datastax.oss.dsbulk.tests.driver.DriverUtils.mockNode) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.jupiter.api.Test)

Example 49 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project dsbulk by datastax.

the class SchemaSettings method createReadStatements.

public List<Statement<?>> createReadStatements(CqlSession session) {
    PreparedStatement preparedStatement = preparedStatements.get(0);
    ColumnDefinitions variables = preparedStatement.getVariableDefinitions();
    if (variables.size() == 0) {
        return Collections.singletonList(preparedStatement.bind());
    }
    boolean ok = true;
    Optional<CQLWord> start = queryInspector.getTokenRangeRestrictionStartVariable();
    Optional<CQLWord> end = queryInspector.getTokenRangeRestrictionEndVariable();
    if (!start.isPresent() || !end.isPresent()) {
        ok = false;
    }
    if (start.isPresent() && end.isPresent()) {
        Optional<CQLWord> unrecognized = StreamSupport.stream(variables.spliterator(), false).map(columnDefinition -> columnDefinition.getName().asInternal()).map(CQLWord::fromInternal).filter(name -> !name.equals(start.get()) && !name.equals(end.get())).findAny();
        ok = !unrecognized.isPresent();
    }
    if (!ok) {
        throw new IllegalArgumentException("The provided statement (schema.query) contains unrecognized WHERE restrictions; " + "the WHERE clause is only allowed to contain one token range restriction " + "of the form: WHERE token(...) > ? AND token(...) <= ?");
    }
    Metadata metadata = session.getMetadata();
    TokenRangeReadStatementGenerator generator = new TokenRangeReadStatementGenerator(table, metadata);
    List<Statement<?>> statements = generator.generate(splits, range -> preparedStatement.bind().setToken(queryInspector.getTokenRangeRestrictionStartVariableIndex(), range.getStart()).setToken(queryInspector.getTokenRangeRestrictionEndVariableIndex(), range.getEnd()));
    LOGGER.debug("Generated {} bound statements", statements.size());
    // Shuffle the statements to avoid hitting the same replicas sequentially when
    // the statements will be executed.
    Collections.shuffle(statements);
    return statements;
}
Also used : MAPPED_OR_INDEXED(com.datastax.oss.dsbulk.mapping.MappingPreference.MAPPED_OR_INDEXED) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MAPPED_ONLY(com.datastax.oss.dsbulk.mapping.MappingPreference.MAPPED_ONLY) DefaultMapping(com.datastax.oss.dsbulk.mapping.DefaultMapping) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) TokenRangeReadStatementGenerator(com.datastax.oss.dsbulk.partitioner.TokenRangeReadStatementGenerator) ConfigUtils(com.datastax.oss.dsbulk.config.ConfigUtils) ALIASED_SELECTOR(com.datastax.oss.dsbulk.mapping.CQLRenderMode.ALIASED_SELECTOR) BatchType(com.datastax.oss.driver.api.core.cql.BatchType) Map(java.util.Map) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) EnumSet(java.util.EnumSet) CQLRenderMode(com.datastax.oss.dsbulk.mapping.CQLRenderMode) VARIABLE(com.datastax.oss.dsbulk.mapping.CQLRenderMode.VARIABLE) MapType(com.datastax.oss.driver.api.core.type.MapType) GraphUtils(com.datastax.oss.dsbulk.workflow.commons.utils.GraphUtils) Set(java.util.Set) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) Field(com.datastax.oss.dsbulk.connectors.api.Field) Stream(java.util.stream.Stream) ConfigException(com.typesafe.config.ConfigException) CQLWord(com.datastax.oss.dsbulk.mapping.CQLWord) INDEXED_ONLY(com.datastax.oss.dsbulk.mapping.MappingPreference.INDEXED_ONLY) MICROSECONDS(java.util.concurrent.TimeUnit.MICROSECONDS) CQLLiteral(com.datastax.oss.dsbulk.mapping.CQLLiteral) ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) Preconditions(com.datastax.oss.driver.shaded.guava.common.base.Preconditions) STAR(com.datastax.oss.dsbulk.mapping.MappingInspector.STAR) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CqlSession(com.datastax.oss.driver.api.core.CqlSession) DefaultReadResultMapper(com.datastax.oss.dsbulk.workflow.commons.schema.DefaultReadResultMapper) FunctionCall(com.datastax.oss.dsbulk.mapping.FunctionCall) StreamSupport(java.util.stream.StreamSupport) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) LinkedHashSet(java.util.LinkedHashSet) DseEdgeMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseEdgeMetadata) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) RelationMetadata(com.datastax.oss.driver.api.core.metadata.schema.RelationMetadata) MappingPreference(com.datastax.oss.dsbulk.mapping.MappingPreference) Config(com.typesafe.config.Config) NestedBatchException(com.datastax.oss.dsbulk.workflow.commons.schema.NestedBatchException) DataType(com.datastax.oss.driver.api.core.type.DataType) ConvertingCodecFactory(com.datastax.oss.dsbulk.codecs.api.ConvertingCodecFactory) DefaultRecordMapper(com.datastax.oss.dsbulk.workflow.commons.schema.DefaultRecordMapper) RecordMapper(com.datastax.oss.dsbulk.workflow.commons.schema.RecordMapper) DseTableMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseTableMetadata) CQLFragment(com.datastax.oss.dsbulk.mapping.CQLFragment) WRITETIME(com.datastax.oss.dsbulk.mapping.MappingInspector.WRITETIME) ReadResultMapper(com.datastax.oss.dsbulk.workflow.commons.schema.ReadResultMapper) DseVertexMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseVertexMetadata) Nullable(edu.umd.cs.findbugs.annotations.Nullable) StatisticsMode(com.datastax.oss.dsbulk.workflow.commons.settings.StatsSettings.StatisticsMode) DefaultReadResultCounter(com.datastax.oss.dsbulk.workflow.commons.schema.DefaultReadResultCounter) CodecUtils.instantToNumber(com.datastax.oss.dsbulk.codecs.api.util.CodecUtils.instantToNumber) IndexedMappingField(com.datastax.oss.dsbulk.mapping.IndexedMappingField) LoggerFactory(org.slf4j.LoggerFactory) MappingField(com.datastax.oss.dsbulk.mapping.MappingField) QueryInspector(com.datastax.oss.dsbulk.workflow.commons.schema.QueryInspector) Mapping(com.datastax.oss.dsbulk.mapping.Mapping) ReadResultCounter(com.datastax.oss.dsbulk.workflow.commons.schema.ReadResultCounter) Lists(com.datastax.oss.driver.shaded.guava.common.collect.Lists) ConvertingCodec(com.datastax.oss.dsbulk.codecs.api.ConvertingCodec) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Predicates(com.datastax.oss.driver.shaded.guava.common.base.Predicates) URI(java.net.URI) IndexMetadata(com.datastax.oss.driver.api.core.metadata.schema.IndexMetadata) Record(com.datastax.oss.dsbulk.connectors.api.Record) ImmutableMultimap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMultimap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) NAMED_ASSIGNMENT(com.datastax.oss.dsbulk.mapping.CQLRenderMode.NAMED_ASSIGNMENT) Objects(java.util.Objects) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) SetType(com.datastax.oss.driver.api.core.type.SetType) TIMESTAMP_PATTERN(com.datastax.oss.dsbulk.codecs.api.CommonConversionContext.TIMESTAMP_PATTERN) MappingInspector(com.datastax.oss.dsbulk.mapping.MappingInspector) DseGraphKeyspaceMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseGraphKeyspaceMetadata) ListType(com.datastax.oss.driver.api.core.type.ListType) HashSet(java.util.HashSet) RecordMetadata(com.datastax.oss.dsbulk.connectors.api.RecordMetadata) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) MappedMappingField(com.datastax.oss.dsbulk.mapping.MappedMappingField) ImmutableSetMultimap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSetMultimap) INTERNAL(com.datastax.oss.dsbulk.mapping.CQLRenderMode.INTERNAL) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) TypedCQLLiteral(com.datastax.oss.dsbulk.mapping.TypedCQLLiteral) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) DseGraphTableMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseGraphTableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) DefaultProtocolVersion(com.datastax.oss.driver.api.core.DefaultProtocolVersion) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) TTL(com.datastax.oss.dsbulk.mapping.MappingInspector.TTL) Multimap(com.datastax.oss.driver.shaded.guava.common.collect.Multimap) EPOCH(java.time.Instant.EPOCH) Collections(java.util.Collections) Statement(com.datastax.oss.driver.api.core.cql.Statement) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) DseEdgeMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseEdgeMetadata) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) RelationMetadata(com.datastax.oss.driver.api.core.metadata.schema.RelationMetadata) DseTableMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseTableMetadata) DseVertexMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseVertexMetadata) IndexMetadata(com.datastax.oss.driver.api.core.metadata.schema.IndexMetadata) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DseGraphKeyspaceMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseGraphKeyspaceMetadata) RecordMetadata(com.datastax.oss.dsbulk.connectors.api.RecordMetadata) DseGraphTableMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseGraphTableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) TokenRangeReadStatementGenerator(com.datastax.oss.dsbulk.partitioner.TokenRangeReadStatementGenerator) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CQLWord(com.datastax.oss.dsbulk.mapping.CQLWord)

Example 50 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zeppelin by apache.

the class CassandraInterpreterTest method should_execute_statement_with_timestamp_option.

@Test
public void should_execute_statement_with_timestamp_option() throws Exception {
    // Given
    String statement1 = "INSERT INTO zeppelin.ts(key,val) VALUES('k','v1');";
    String statement2 = "@timestamp=15\n" + "INSERT INTO zeppelin.ts(key,val) VALUES('k','v2');";
    CqlSession session = EmbeddedCassandraServerHelper.getSession();
    // Insert v1 with current timestamp
    interpreter.interpret(statement1, intrContext);
    System.out.println("going to read data from zeppelin.ts;");
    session.execute("SELECT val FROM zeppelin.ts LIMIT 1").forEach(x -> System.out.println("row " + x));
    Thread.sleep(1);
    // When
    // Insert v2 with past timestamp
    interpreter.interpret(statement2, intrContext);
    System.out.println("going to read data from zeppelin.ts;");
    session.execute("SELECT val FROM zeppelin.ts LIMIT 1").forEach(x -> System.out.println("row " + x));
    final String actual = session.execute("SELECT val FROM zeppelin.ts LIMIT 1").one().getString("val");
    // Then
    assertThat(actual).isEqualTo("v1");
}
Also used : CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)577 Test (org.junit.Test)286 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)118 Row (com.datastax.oss.driver.api.core.cql.Row)98 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)82 Test (org.junit.jupiter.api.Test)78 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)65 List (java.util.List)54 Map (java.util.Map)52 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)51 Node (com.datastax.oss.driver.api.core.metadata.Node)50 ArrayList (java.util.ArrayList)49 BeforeClass (org.junit.BeforeClass)43 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)41 HashMap (java.util.HashMap)37 UUID (java.util.UUID)36 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)30 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)30 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)27