public static CqlResult processPrepared(CQLStatement statement, ThriftClientState clientState, List<ByteBuffer> variables) throws RequestValidationException, RequestExecutionException { // Check to see if there are any bound variables to verify if (!(variables.isEmpty() && (statement.boundTerms == 0))) { if (variables.size() != statement.boundTerms) throw new InvalidRequestException(String.format("there were %d markers(?) in CQL but %d bound variables", statement.boundTerms, variables.size())); // at this point there is a match in count between markers and variables that is non-zero if (logger.isTraceEnabled()) for (int i = 0; i < variables.size(); i++) logger.trace("[{}] '{}'", i+1, variables.get(i)); } return processStatement(statement, new ExecutionContext(clientState, null, variables)); }
@Override public Function<CqlResult, Integer> simpleNativeHandler() { return new Function<CqlResult, Integer>() { @Override public Integer apply(CqlResult result) { switch (result.getType()) { case ROWS: return result.getRows().size(); default: return 1; } } }; }
@Override public Function<CqlResult, ByteBuffer[][]> simpleNativeHandler() { return new Function<CqlResult, ByteBuffer[][]>() { @Override public ByteBuffer[][] apply(CqlResult result) { ByteBuffer[][] r = new ByteBuffer[result.getRows().size()][]; for (int i = 0 ; i < r.length ; i++) { CqlRow row = result.getRows().get(i); r[i] = new ByteBuffer[row.getColumns().size()]; for (int j = 0 ; j < r[i].length ; j++) r[i][j] = ByteBuffer.wrap(row.getColumns().get(j).getValue()); } return r; } }; }
@Override public Function<CqlResult, byte[][]> simpleNativeHandler() { return new Function<CqlResult, byte[][]>() { @Override public byte[][] apply(CqlResult result) { byte[][] r = new byte[result.getRows().size()][]; for (int i = 0 ; i < r.length ; i++) r[i] = result.getRows().get(i).getKey(); return r; } }; }
public static CqlResult processPrepared(CQLStatement statement, ThriftClientState clientState, List<ByteBuffer> variables) throws RequestValidationException, RequestExecutionException { // Check to see if there are any bound variables to verify if (!(variables.isEmpty() && (statement.boundTerms == 0))) { if (variables.size() != statement.boundTerms) throw new InvalidRequestException(String.format("there were %d markers(?) in CQL but %d bound variables", statement.boundTerms, variables.size())); // at this point there is a match in count between markers and variables that is non-zero if (logger.isTraceEnabled()) for (int i = 0; i < variables.size(); i++) logger.trace("[{}] '{}'", i+1, variables.get(i)); } return processStatement(statement, clientState, variables); }
protected List<CqlResult> executeCql(final String statements) throws MojoExecutionException { final List<CqlResult> results = new ArrayList<CqlResult>(); if (StringUtils.isBlank(statements)) { getLog().warn("No CQL provided. Nothing to do."); } else { try { CqlExecOperation operation = new CqlExecOperation(statements); Utils.executeThrift(operation); results.addAll(operation.results); } catch (ThriftApiExecutionException taee) { throw new MojoExecutionException(taee.getMessage(), taee); } } return results; }
private CqlResult executeStatement(Client client, String statement) throws ThriftApiExecutionException { ByteBuffer buf = ByteBufferUtil.bytes(statement); try { if (cqlVersion.charAt(0) >= '3') { return client.execute_cql3_query(buf, Compression.NONE, ConsistencyLevel.ONE); } else { return client.execute_cql_query(buf, Compression.NONE); } } catch (Exception e) { getLog().debug(statement); throw new ThriftApiExecutionException(e); } }
private void printResults(List<CqlResult> results) { // TODO fix ghetto formatting getLog().info("-----------------------------------------------"); for (CqlResult result : results) { switch (result.type) { case VOID: // Void method so nothing to log break; case INT: getLog().info("Number result: " + result.getNum()); break; case ROWS: printRows(result); break; } } }
/** * getByCql * * @throws Exception */ @Test public void getByCql() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // String CQL = "select * from student where KEY='Jack'"; // query, compression CqlResult result = client.execute_cql_query( ByteBufferHelper.toByteBuffer(CQL), Compression.NONE); System.out.println(result); for (CqlRow cqlRow : result.getRows()) { for (Column column : cqlRow.getColumns()) { System.out.println(ByteHelper.toString(cqlRow.getKey()) + ", " + ByteHelper.toString(column.getName()) + ": " + ByteHelper.toString(column.getValue()) + ", " + column.getTimestamp()); // Jack, KEY: Jack, -1 // Jack, art: 87, 1380933848350 // Jack, grad: 5, 1380932164492000 // Jack, math: 97, 1380933848305 } } }
public CqlResult toThriftResult() { assert metadata.names != null; String UTF8 = "UTF8Type"; CqlMetadata schema = new CqlMetadata(new HashMap<ByteBuffer, String>(), new HashMap<ByteBuffer, String>(), // The 2 following ones shouldn't be needed in CQL3 UTF8, UTF8); for (int i = 0; i < metadata.columnCount; i++) { ColumnSpecification spec = metadata.names.get(i); ByteBuffer colName = ByteBufferUtil.bytes(spec.name.toString()); schema.name_types.put(colName, UTF8); AbstractType<?> normalizedType = spec.type instanceof ReversedType ? ((ReversedType)spec.type).baseType : spec.type; schema.value_types.put(colName, normalizedType.toString()); } List<CqlRow> cqlRows = new ArrayList<CqlRow>(rows.size()); for (List<ByteBuffer> row : rows) { List<Column> thriftCols = new ArrayList<Column>(metadata.columnCount); for (int i = 0; i < metadata.columnCount; i++) { Column col = new Column(ByteBufferUtil.bytes(metadata.names.get(i).name.toString())); col.setValue(row.get(i)); thriftCols.add(col); } // The key of CqlRow shoudn't be needed in CQL3 cqlRows.add(new CqlRow(ByteBufferUtil.EMPTY_BYTE_BUFFER, thriftCols)); } CqlResult res = new CqlResult(CqlResultType.ROWS); res.setRows(cqlRows).setSchema(schema); return res; }
public static CqlResult process(String queryString, ThriftClientState clientState) throws RequestValidationException, RequestExecutionException { logger.trace("CQL QUERY: {}", queryString); return processStatement(getStatement(queryString), new ExecutionContext(clientState, queryString, Collections.<ByteBuffer>emptyList())); }
/** * Create CFMetaData from thrift {@link CqlRow} that contains columns from schema_columnfamilies. * * @param columnsRes CqlRow containing columns from schema_columnfamilies. * @return CFMetaData derived from CqlRow */ public static CFMetaData fromThriftCqlRow(CqlRow cf, CqlResult columnsRes) { UntypedResultSet.Row cfRow = new UntypedResultSet.Row(convertThriftCqlRow(cf)); List<Map<String, ByteBuffer>> cols = new ArrayList<>(columnsRes.rows.size()); for (CqlRow row : columnsRes.rows) cols.add(convertThriftCqlRow(row)); UntypedResultSet colsRow = UntypedResultSet.create(cols); return fromSchemaNoTriggers(cfRow, colsRow); }
void validate(CqlResult rs) { switch (validationType) { case NOT_FAIL: return; case NON_ZERO: if (rs.getRowsSize() == 0) throw new IllegalStateException("Expected non-zero results"); break; default: throw new IllegalStateException("Unsupported validation type"); } }
public boolean run() throws Exception { CqlResult rs = client.execute_prepared_cql3_query(thriftId, partitions.get(0).getToken(), thriftArgs(), ThriftConversion.toThrift(cl)); validate(rs); rowCount = rs.getRowsSize(); partitionCount = Math.min(1, rowCount); return true; }
public CqlResult toThriftResult() { assert metadata.names != null; String UTF8 = "UTF8Type"; CqlMetadata schema = new CqlMetadata(new HashMap<ByteBuffer, String>(), new HashMap<ByteBuffer, String>(), // The 2 following ones shouldn't be needed in CQL3 UTF8, UTF8); for (ColumnSpecification name : metadata.names) { ByteBuffer colName = ByteBufferUtil.bytes(name.toString()); schema.name_types.put(colName, UTF8); AbstractType<?> normalizedType = name.type instanceof ReversedType ? ((ReversedType)name.type).baseType : name.type; schema.value_types.put(colName, normalizedType.toString()); } List<CqlRow> cqlRows = new ArrayList<CqlRow>(rows.size()); for (List<ByteBuffer> row : rows) { List<Column> thriftCols = new ArrayList<Column>(metadata.names.size()); for (int i = 0; i < metadata.names.size(); i++) { Column col = new Column(ByteBufferUtil.bytes(metadata.names.get(i).toString())); col.setValue(row.get(i)); thriftCols.add(col); } // The key of CqlRow shoudn't be needed in CQL3 cqlRows.add(new CqlRow(ByteBufferUtil.EMPTY_BYTE_BUFFER, thriftCols)); } CqlResult res = new CqlResult(CqlResultType.ROWS); res.setRows(cqlRows).setSchema(schema); return res; }
/** * List pending token relocations for all nodes. * * @return * @throws ShuffleError */ private Map<String, List<CqlRow>> listRelocations() throws ShuffleError { String cqlQuery = "SELECT token_bytes,requested_at FROM system.range_xfers"; Map<String, List<CqlRow>> results = new HashMap<String, List<CqlRow>>(); for (String host : getLiveNodes()) { CqlResult result = executeCqlQuery(host, thriftPort, thriftFramed, cqlQuery); results.put(host, result.getRows()); } return results; }
public void run(final CassandraClient client) throws IOException { run(new CQLQueryExecutor() { public boolean execute(String cqlQuery, List<String> queryParams) throws Exception { CqlResult result = null; if (session.usePreparedStatements()) { Integer stmntId = getPreparedStatement(client, cqlQuery); if (session.cqlVersion.startsWith("3")) result = client.execute_prepared_cql3_query(stmntId, queryParamsAsByteBuffer(queryParams), session.getConsistencyLevel()); else result = client.execute_prepared_cql_query(stmntId, queryParamsAsByteBuffer(queryParams)); } else { String formattedQuery = formatCqlQuery(cqlQuery, queryParams); if (session.cqlVersion.startsWith("3")) result = client.execute_cql3_query(ByteBuffer.wrap(formattedQuery.getBytes()), Compression.NONE, session.getConsistencyLevel()); else result = client.execute_cql_query(ByteBuffer.wrap(formattedQuery.getBytes()), Compression.NONE); } return validateThriftResult(result); } }); }
private void printRows(CqlResult result) { for (CqlRow row : result.getRows()) { getLog().info("Row key: " + keyValidatorVal.getString(row.key)); getLog().info("-----------------------------------------------"); for (Column column : row.getColumns()) { getLog().info(" name: " + comparatorVal.getString(column.name)); getLog().info(" value: " + defaultValidatorVal.getString(column.value)); getLog().info("-----------------------------------------------"); } } }
/** * removeByCql * * @throws Exception */ @Test public void removeByCql() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // String CQL = "delete from student where KEY='Mary'"; CqlResult result = client.execute_cql_query( ByteBufferHelper.toByteBuffer(CQL), Compression.NONE); System.out.println(result); }
public boolean run() throws Exception { CqlResult rs = client.execute_prepared_cql3_query(thriftId, partitions.get(0).getToken(), thriftArgs(), ThriftConversion.toThrift(cl)); rowCount = rs.getRowsSize(); partitionCount = Math.min(1, rowCount); return true; }