use of com.datastax.driver.core.ResultSet in project janusgraph by JanusGraph.
the class CQLResultSetKeyIteratorTest method testPartialIterateColumns.
@Test
public void testPartialIterateColumns() throws IOException {
final Random random = new Random();
final Function1<Integer, ByteBuffer> randomLong = idx -> {
final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).putLong(random.nextLong());
buffer.flip();
return buffer;
};
final Array<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> keysMap = Array.range(0, random.nextInt(100) + 100).map(randomLong).map(key -> Tuple.of(key, Array.rangeClosed(0, random.nextInt(100) + 1).map(idx -> Tuple.of(randomLong.apply(idx), randomLong.apply(idx)))));
final Seq<Row> rows = keysMap.flatMap(tuple -> tuple._2.map(columnAndValue -> {
final Row row = mock(Row.class);
when(row.getBytes("key")).thenReturn(tuple._1);
when(row.getBytes("column1")).thenReturn(columnAndValue._1);
when(row.getBytes("value")).thenReturn(columnAndValue._2);
return row;
}));
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)) {
final Iterator<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> iterator = keysMap.iterator();
while (resultSetKeyIterator.hasNext()) {
final StaticBuffer next = resultSetKeyIterator.next();
try (final RecordIterator<Entry> entries = resultSetKeyIterator.getEntries()) {
final Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>> current = iterator.next();
final ByteBuffer currentKey = current._1;
final Array<Tuple2<ByteBuffer, ByteBuffer>> columnValues = current._2;
final Iterator<Tuple2<ByteBuffer, ByteBuffer>> columnIterator = columnValues.iterator();
while (entries.hasNext()) {
final Entry entry = entries.next();
final Tuple2<ByteBuffer, ByteBuffer> columnAndValue = columnIterator.next();
assertEquals(currentKey, next.asByteBuffer());
assertEquals(columnAndValue._1, entry.getColumn().asByteBuffer());
assertEquals(columnAndValue._2, entry.getValue().asByteBuffer());
assertEquals(columnIterator.hasNext(), entries.hasNext());
// 10% of the time, don't complete the iteration
if (random.nextInt(10) == 0) {
break;
}
}
}
}
}
}
use of com.datastax.driver.core.ResultSet in project YCSB by brianfrankcooper.
the class CassandraCQLClient method read.
/**
* Read a record from the database. Each field/value pair from the result will
* be stored in a HashMap.
*
* @param table
* The name of the table
* @param key
* The record key of the record to read.
* @param fields
* The list of fields to read, or null for all of them
* @param result
* A HashMap of field/value pairs for the result
* @return Zero on success, a non-zero error code on error
*/
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
try {
Statement stmt;
Select.Builder selectBuilder;
if (fields == null) {
selectBuilder = QueryBuilder.select().all();
} else {
selectBuilder = QueryBuilder.select();
for (String col : fields) {
((Select.Selection) selectBuilder).column(col);
}
}
stmt = selectBuilder.from(table).where(QueryBuilder.eq(YCSB_KEY, key)).limit(1);
stmt.setConsistencyLevel(readConsistencyLevel);
if (debug) {
System.out.println(stmt.toString());
}
if (trace) {
stmt.enableTracing();
}
ResultSet rs = session.execute(stmt);
if (rs.isExhausted()) {
return Status.NOT_FOUND;
}
// Should be only 1 row
Row row = rs.one();
ColumnDefinitions cd = row.getColumnDefinitions();
for (ColumnDefinitions.Definition def : cd) {
ByteBuffer val = row.getBytesUnsafe(def.getName());
if (val != null) {
result.put(def.getName(), new ByteArrayByteIterator(val.array()));
} else {
result.put(def.getName(), null);
}
}
return Status.OK;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error reading key: " + key);
return Status.ERROR;
}
}
use of com.datastax.driver.core.ResultSet in project YCSB by brianfrankcooper.
the class CassandraCQLClientTest method testUpdate.
@Test
public void testUpdate() throws Exception {
final String key = "key";
final HashMap<String, String> input = new HashMap<String, String>();
input.put("field0", "value1");
input.put("field1", "value2");
final Status status = client.insert(TABLE, key, StringByteIterator.getByteIteratorMap(input));
assertThat(status, is(Status.OK));
// Verify result
final Select selectStmt = QueryBuilder.select("field0", "field1").from(TABLE).where(QueryBuilder.eq(CassandraCQLClient.YCSB_KEY, key)).limit(1);
final ResultSet rs = session.execute(selectStmt);
final Row row = rs.one();
assertThat(row, notNullValue());
assertThat(rs.isExhausted(), is(true));
assertThat(row.getString("field0"), is("value1"));
assertThat(row.getString("field1"), is("value2"));
}
use of com.datastax.driver.core.ResultSet in project flink by apache.
the class CassandraConnectorITCase method verifyResultsIdealCircumstances.
@Override
protected void verifyResultsIdealCircumstances(CassandraTupleWriteAheadSink<Tuple3<String, Integer, Integer>> sink) {
ResultSet result = session.execute(SELECT_DATA_QUERY);
ArrayList<Integer> list = new ArrayList<>();
for (int x = 1; x <= 60; x++) {
list.add(x);
}
for (Row s : result) {
list.remove(new Integer(s.getInt("counter")));
}
Assert.assertTrue("The following ID's were not found in the ResultSet: " + list.toString(), list.isEmpty());
}
use of com.datastax.driver.core.ResultSet in project flink by apache.
the class CassandraConnectorITCase method verifyResultsDataDiscardingUponRestore.
@Override
protected void verifyResultsDataDiscardingUponRestore(CassandraTupleWriteAheadSink<Tuple3<String, Integer, Integer>> sink) {
ResultSet result = session.execute(SELECT_DATA_QUERY);
ArrayList<Integer> list = new ArrayList<>();
for (int x = 1; x <= 20; x++) {
list.add(x);
}
for (int x = 41; x <= 60; x++) {
list.add(x);
}
for (Row s : result) {
list.remove(new Integer(s.getInt("counter")));
}
Assert.assertTrue("The following ID's were not found in the ResultSet: " + list.toString(), list.isEmpty());
}
Aggregations