Search in sources :

Example 1 with SimpleStatement

use of com.datastax.oss.driver.api.core.cql.SimpleStatement in project spring-boot by spring-projects.

the class CassandraAutoConfigurationWithPasswordAuthenticationIntegrationTests method authenticationWithValidUsernameAndPassword.

@Test
void authenticationWithValidUsernameAndPassword() {
    this.contextRunner.withPropertyValues("spring.data.cassandra.username=cassandra", "spring.data.cassandra.password=cassandra").run((context) -> {
        SimpleStatement select = SimpleStatement.newInstance("SELECT release_version FROM system.local").setConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
        assertThat(context.getBean(CqlSession.class).execute(select).one()).isNotNull();
    });
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Test(org.junit.jupiter.api.Test)

Example 2 with SimpleStatement

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

the class InterpreterLogicTest method should_generate_simple_statement.

@Test
public void should_generate_simple_statement() {
    // Given
    String input = "SELECT * FROM users LIMIT 10;";
    CassandraQueryOptions options = new CassandraQueryOptions(Option.apply(QUORUM), Option.empty(), Option.empty(), Option.empty(), Option.empty());
    // When
    final SimpleStatement actual = helper.generateSimpleStatement(new SimpleStm(input), options, intrContext);
    // Then
    assertThat(actual).isNotNull();
    assertThat(actual.getQuery()).isEqualTo("SELECT * FROM users LIMIT 10;");
    assertThat(actual.getConsistencyLevel()).isSameAs(QUORUM);
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) SimpleStm(org.apache.zeppelin.cassandra.TextBlockHierarchy.SimpleStm) Test(org.junit.Test)

Example 3 with SimpleStatement

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

the class InterpreterLogicTest method should_generate_batch_statement.

@Test
public void should_generate_batch_statement() {
    // Given
    SimpleStatement st1 = SimpleStatement.newInstance("SELECT * FROM users LIMIT 10;");
    SimpleStatement st2 = SimpleStatement.newInstance("INSERT INTO users(id) VALUES(10);");
    SimpleStatement st3 = SimpleStatement.newInstance("UPDATE users SET name = 'John DOE' WHERE id=10;");
    CassandraQueryOptions options = new CassandraQueryOptions(Option.apply(QUORUM), Option.empty(), Option.empty(), Option.empty(), Option.empty());
    // When
    BatchStatement actual = helper.generateBatchStatement(UNLOGGED, options, toScalaList(asList(st1, st2, st3)));
    // Then
    assertThat(actual).isNotNull();
    List<BatchableStatement> statements = new ArrayList<>();
    for (BatchableStatement b : actual) {
        statements.add(b);
    }
    assertThat(statements).hasSize(3);
    assertThat(statements.get(0)).isSameAs(st1);
    assertThat(statements.get(1)).isSameAs(st2);
    assertThat(statements.get(2)).isSameAs(st3);
    assertThat(actual.getConsistencyLevel()).isSameAs(QUORUM);
}
Also used : BatchableStatement(com.datastax.oss.driver.api.core.cql.BatchableStatement) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with SimpleStatement

use of com.datastax.oss.driver.api.core.cql.SimpleStatement in project rocketmq-externals by apache.

the class Querier method poll.

public void poll() {
    try {
        LinkedList<Table> tableLinkedList = new LinkedList<>();
        for (Map.Entry<String, Database> entry : schema.getDbMap().entrySet()) {
            String dbName = entry.getKey();
            Iterator<Map.Entry<String, Table>> iterator = entry.getValue().getTableMap().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Table> tableEntry = iterator.next();
                String tableName = tableEntry.getKey();
                Table table = tableEntry.getValue();
                Map<String, String> tableFilterMap = table.getFilterMap();
                Select selectFrom = QueryBuilder.selectFrom(dbName, tableName).all();
                if (tableFilterMap != null && !tableFilterMap.keySet().contains("NO-FILTER")) {
                    for (String key : tableFilterMap.keySet()) {
                        String value = tableFilterMap.get(key);
                        selectFrom.whereColumn(key).isEqualTo(QueryBuilder.literal(value));
                    }
                }
                SimpleStatement stmt;
                boolean finishUpdate = false;
                log.info("trying to execute sql query,{}", selectFrom.asCql());
                ResultSet result = null;
                while (!cqlSession.isClosed() && !finishUpdate) {
                    stmt = selectFrom.build();
                    result = cqlSession.execute(stmt);
                    if (result.wasApplied()) {
                        log.info("query columns success, executed cql query {}", selectFrom.asCql());
                    }
                    finishUpdate = true;
                }
                List<String> colList = tableEntry.getValue().getColList();
                List<String> dataTypeList = tableEntry.getValue().getRawDataTypeList();
                List<ColumnParser> parserList = tableEntry.getValue().getParserList();
                for (Row row : result) {
                    Table tableWithData = new Table(dbName, tableName);
                    tableWithData.setColList(colList);
                    tableWithData.setRawDataTypeList(dataTypeList);
                    tableWithData.setParserList(parserList);
                    for (String col : colList) {
                        tableWithData.getDataList().add(row.getObject(col));
                    }
                    tableLinkedList.add(tableWithData);
                }
            }
        }
        list = tableLinkedList;
    } catch (Exception e) {
        log.error("fail to poll data, {}", e);
    }
}
Also used : Table(org.apache.rocketmq.connect.cassandra.schema.Table) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) LinkedList(java.util.LinkedList) ColumnParser(org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser) Database(org.apache.rocketmq.connect.cassandra.schema.Database) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with SimpleStatement

use of com.datastax.oss.driver.api.core.cql.SimpleStatement in project rocketmq-externals by apache.

the class Schema method load.

public void load() throws Exception {
    dbMap = new HashMap<>();
    Select selectFrom = QueryBuilder.selectFrom(SCHEMA_SYSTEM_SCHEMA, TABLE_KEYSPACES).column(COLUMN_KEYSPACE_NAME);
    SimpleStatement stmt;
    boolean finishUpdate = false;
    LOGGER.info("trying to execute sql query,{}", selectFrom.asCql());
    ResultSet result = null;
    try {
        while (!cqlSession.isClosed() && !finishUpdate) {
            stmt = selectFrom.build();
            result = cqlSession.execute(stmt);
            if (result.wasApplied()) {
                LOGGER.info("update table success, executed cql query {}", selectFrom.asCql());
            }
            finishUpdate = true;
        }
        for (Row row : result) {
            String dbName = row.getString(COLUMN_KEYSPACE_NAME);
            if (!IGNORED_DATABASES.contains(dbName) && dbTableMap.keySet().contains(dbName)) {
                Database database = new Database(dbName, cqlSession, dbTableMap.get(dbName), tableFilterMap);
                dbMap.put(dbName, database);
            }
        }
    } catch (Exception e) {
        LOGGER.error("init cassandra Schema failure,{}", e);
    }
    for (Database db : dbMap.values()) {
        db.init();
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row)

Aggregations

SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)8 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)5 Row (com.datastax.oss.driver.api.core.cql.Row)3 Select (com.datastax.oss.driver.api.querybuilder.select.Select)3 Map (java.util.Map)3 Field (io.openmessaging.connector.api.data.Field)2 FieldType (io.openmessaging.connector.api.data.FieldType)2 ColumnParser (org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser)2 Test (org.junit.Test)2 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1 BatchableStatement (com.datastax.oss.driver.api.core.cql.BatchableStatement)1 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)1 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)1 InsertInto (com.datastax.oss.driver.api.querybuilder.insert.InsertInto)1 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Database (org.apache.rocketmq.connect.cassandra.schema.Database)1 Table (org.apache.rocketmq.connect.cassandra.schema.Table)1