Search in sources :

Example 6 with ResultSet

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

the class Updater method updateRow.

/**
 * Since we have no way of getting the id of a record, and we cannot get the primary key list of a table,
 * even we can it is not extensible. So we the result sql sentense would be like
 * UPDATE dbName.tableName SET afterUpdateValues WHERE beforeUpdateValues.
 */
private Boolean updateRow(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
    log.info("Updater.updateRow() get called ");
    int count = 0;
    InsertInto insert = QueryBuilder.insertInto(dbName, tableName);
    RegularInsert regularInsert = null;
    for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
        count++;
        String fieldName = entry.getKey().getName();
        FieldType fieldType = entry.getKey().getType();
        Object fieldValue = entry.getValue()[1];
        if (count == 1) {
            regularInsert = insert.value(fieldName, buildTerm(fieldType, fieldValue));
        } else {
            regularInsert = regularInsert.value(fieldName, buildTerm(fieldType, fieldValue));
        }
    }
    SimpleStatement stmt;
    boolean finishUpdate = false;
    log.info("trying to execute sql query,{}", regularInsert.asCql());
    try {
        while (!cqlSession.isClosed() && !finishUpdate) {
            stmt = regularInsert.build();
            ResultSet result = cqlSession.execute(stmt);
            if (result.wasApplied()) {
                log.info("update table success, executed cql query {}", regularInsert.asCql());
                return true;
            }
            finishUpdate = true;
        }
    } catch (Exception e) {
        log.error("update table error,{}", e);
    }
    return false;
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) FieldType(io.openmessaging.connector.api.data.FieldType) Field(io.openmessaging.connector.api.data.Field) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Map(java.util.Map)

Example 7 with ResultSet

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

the class Database method init.

public void init() {
    Select selectFrom = QueryBuilder.selectFrom(SCHEMA_SYSTEM_SCHEMA, TABLE_COLUMNS).all().whereColumn(COLUMN_KEYSPACE_NAME).isEqualTo(QueryBuilder.literal(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("query columns success, executed cql query {}", selectFrom.asCql());
            }
            finishUpdate = true;
        }
        for (Row row : result) {
            String tableName = row.getString(COLUMN_TABLE_NAME);
            String columnName = row.getString(COLUMN_COLUMN_NAME);
            String columnType = row.getString(COLUMN_TYPE);
            ColumnParser columnParser = ColumnParser.getColumnParser(columnType, columnType, GENERAL_CHARSET);
            if (!tableWhiteList.contains(tableName)) {
                continue;
            }
            if (!tableMap.containsKey(tableName)) {
                addTable(tableName);
            }
            Table table = tableMap.get(tableName);
            table.addCol(columnName);
            table.addParser(columnParser);
            table.addRawDataType(columnType);
            table.setFilterMap(tableFilterMap.get(tableName));
        }
    } catch (Exception e) {
        LOGGER.error("init cassandra Schema failure,{}", e);
    }
}
Also used : ColumnParser(org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser) 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)

Example 8 with ResultSet

use of com.datastax.oss.driver.api.core.cql.ResultSet in project janusgraph by JanusGraph.

the class CQLResultSetKeyIteratorTest method testEmpty.

@Test
public void testEmpty() throws IOException {
    final Array<Row> rows = Array.empty();
    final ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.iterator()).thenReturn(rows.iterator());
    final CQLColValGetter getter = new CQLColValGetter(new EntryMetaData[0]);
    try (final CQLResultSetKeyIterator resultSetKeyIterator = new CQLResultSetKeyIterator(ALL_COLUMNS, getter, resultSet)) {
        assertFalse(resultSetKeyIterator.hasNext());
    }
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) Test(org.junit.jupiter.api.Test)

Example 9 with ResultSet

use of com.datastax.oss.driver.api.core.cql.ResultSet in project janusgraph by JanusGraph.

the class CQLResultSetKeyIteratorTest method testNoIterateColumns.

@Test
public void testNoIterateColumns() throws IOException {
    final Array<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> keysMap = generateRandomKeysMap();
    final ResultSet resultSet = generateMockedResultSet(keysMap);
    final CQLColValGetter getter = new CQLColValGetter(new EntryMetaData[0]);
    try (final CQLResultSetKeyIterator resultSetKeyIterator = new CQLResultSetKeyIterator(ALL_COLUMNS, getter, resultSet)) {
        final Iterator<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> iterator = keysMap.iterator();
        while (resultSetKeyIterator.hasNext()) {
            final StaticBuffer next = resultSetKeyIterator.next();
            assertEquals(iterator.next()._1, next.asByteBuffer());
        }
    }
}
Also used : Tuple2(io.vavr.Tuple2) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with ResultSet

use of com.datastax.oss.driver.api.core.cql.ResultSet in project janusgraph by JanusGraph.

the class CQLResultSetKeyIteratorTest method generateMockedResultSet.

private ResultSet generateMockedResultSet(Array<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> keysMap) {
    final Seq<Row> rows = keysMap.flatMap(tuple -> tuple._2.map(columnAndValue -> {
        final Row row = mock(Row.class);
        when(row.getByteBuffer("key")).thenReturn(tuple._1);
        when(row.getByteBuffer("column1")).thenReturn(columnAndValue._1);
        when(row.getByteBuffer("value")).thenReturn(columnAndValue._2);
        return row;
    }));
    final ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.iterator()).thenReturn(rows.iterator());
    return resultSet;
}
Also used : Tuple(io.vavr.Tuple) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) EntryMetaData(org.janusgraph.diskstorage.EntryMetaData) SliceQuery(org.janusgraph.diskstorage.keycolumnvalue.SliceQuery) Array(io.vavr.collection.Array) IOException(java.io.IOException) Random(java.util.Random) Mockito.when(org.mockito.Mockito.when) Function1(io.vavr.Function1) RecordIterator(org.janusgraph.diskstorage.util.RecordIterator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test) Tuple2(io.vavr.Tuple2) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Entry(org.janusgraph.diskstorage.Entry) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) BufferUtil(org.janusgraph.diskstorage.util.BufferUtil) Iterator(io.vavr.collection.Iterator) Seq(io.vavr.collection.Seq) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Row(com.datastax.oss.driver.api.core.cql.Row) Mockito.mock(org.mockito.Mockito.mock) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row)

Aggregations

ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)11 Row (com.datastax.oss.driver.api.core.cql.Row)6 Test (org.junit.jupiter.api.Test)6 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)5 Tuple2 (io.vavr.Tuple2)4 Entry (org.janusgraph.diskstorage.Entry)4 Select (com.datastax.oss.driver.api.querybuilder.select.Select)3 Array (io.vavr.collection.Array)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 Field (io.openmessaging.connector.api.data.Field)2 FieldType (io.openmessaging.connector.api.data.FieldType)2 Random (java.util.Random)2 ColumnParser (org.apache.rocketmq.connect.cassandra.schema.column.ColumnParser)2 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 Function1 (io.vavr.Function1)1