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); } }
/** * 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 } } }
/** * Send the batch insert. * * @param batch * the CQL batch insert statement * @param conn * the connection to use * @param compressCQL * true if the CQL should be compressed * @throws Exception * if a problem occurs */ public static void commitCQLBatch(StringBuilder batch, CassandraConnection conn, boolean compressCQL) throws Exception { // compress the batch if necessary byte[] toSend = null; if (compressCQL) { toSend = compressQuery(batch.toString(), Compression.GZIP); } else { toSend = batch.toString().getBytes( Charset.forName(CassandraColumnMetaData.UTF8)); } conn.getClient().execute_cql_query(ByteBuffer.wrap(toSend), compressCQL ? Compression.GZIP : Compression.NONE); }
/** * Compress a CQL query * * @param queryStr * the CQL query * @param compression * compression option (GZIP is the only option - so far) * @return an array of bytes containing the compressed query */ public static byte[] compressQuery(String queryStr, Compression compression) { byte[] data = queryStr.getBytes(Charset .forName(CassandraColumnMetaData.UTF8)); Deflater compressor = new Deflater(); compressor.setInput(data); compressor.finish(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; while (!compressor.finished()) { int size = compressor.deflate(buffer); byteArray.write(buffer, 0, size); } return byteArray.toByteArray(); }
/** * Compress a CQL query * * @param queryStr the CQL query * @param compression compression option (GZIP is the only option - so far) * @return an array of bytes containing the compressed query */ public static byte[] compressQuery(String queryStr, Compression compression) { byte[] data = queryStr.getBytes(Charset .forName(CassandraColumnMetaData.UTF8)); Deflater compressor = new Deflater(); compressor.setInput(data); compressor.finish(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; while (!compressor.finished()) { int size = compressor.deflate(buffer); byteArray.write(buffer, 0, size); } return byteArray.toByteArray(); }
protected void executeCQL(String cql) throws InvalidRequestException, UnavailableException, TimedOutException, SchemaDisagreementException, TException { try { client.execute_cql_query(ByteBuffer.wrap(cql.getBytes("UTF-8")), Compression.NONE); } catch (UnsupportedEncodingException e) { throw new InvalidRequestException("Argument is not in UTF-8 character set"); } }
/** @deprecated Remove once Cassandra 1.1 support is no longer necessary. */ public void executeCql3Script_1_1(String script) { try { _client.set_cql_version(CQL_VERSION); for (String cqlStatement : toCqlStatements(script)) { if (StringUtils.isNotBlank(cqlStatement)) { cqlStatement += ";"; _log.info("executing cql statement: " + cqlStatement); _client.execute_cql_query(ByteBuffer.wrap(cqlStatement.getBytes("UTF-8")), Compression.NONE); } } } catch (Exception e) { throw Throwables.propagate(e); } }
public void executeCql3Script(String script) { try { for (String cqlStatement : toCqlStatements(script)) { if (StringUtils.isNotBlank(cqlStatement)) { cqlStatement += ";"; _log.info("executing cql3 statement: " + cqlStatement); _client.execute_cql3_query(ByteBuffer.wrap(cqlStatement.getBytes("UTF-8")), Compression.NONE, ConsistencyLevel.LOCAL_QUORUM); } } } catch (Exception e) { throw Throwables.propagate(e); } }
@Override public <V> V execute(String query, ByteBuffer key, List<Object> queryParams, ResultHandler<V> handler) throws TException { String formattedQuery = formatCqlQuery(query, queryParams, true); return handler.simpleNativeHandler().apply( client.execute_cql3_query(formattedQuery, key, Compression.NONE, settings.command.consistencyLevel) ); }
@Override public <V> V execute(String query, ByteBuffer key, List<Object> queryParams, ResultHandler<V> handler) throws TException { String formattedQuery = formatCqlQuery(query, queryParams, false); return handler.simpleNativeHandler().apply( client.execute_cql_query(formattedQuery, key, Compression.NONE) ); }
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); } }); }
protected Integer getPreparedStatement(CassandraClient client, String cqlQuery) throws Exception { Integer statementId = client.preparedStatements.get(cqlQuery.hashCode()); if (statementId == null) { CqlPreparedResult response = session.cqlVersion.startsWith("3") ? client.prepare_cql3_query(ByteBufferUtil.bytes(cqlQuery), Compression.NONE) : client.prepare_cql_query(ByteBufferUtil.bytes(cqlQuery), Compression.NONE); statementId = response.itemId; client.preparedStatements.put(cqlQuery.hashCode(), statementId); } return statementId; }
/** * 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); }
@Override public <V> V execute(String query, ByteBuffer key, List<Object> queryParams, ResultHandler<V> handler) throws TException { String formattedQuery = formatCqlQuery(query, queryParams); return handler.simpleNativeHandler().apply( client.execute_cql3_query(formattedQuery, key, Compression.NONE, settings.command.consistencyLevel) ); }
private static ValidatingStatement prepare(StressSettings settings, String cql, boolean incLb, boolean incUb) { JavaDriverClient jclient = settings.getJavaDriverClient(); ThriftClient tclient = settings.getThriftClient(); PreparedStatement statement = jclient.prepare(cql); try { Integer thriftId = tclient.prepare_cql3_query(cql, Compression.NONE); return new ValidatingStatement(statement, thriftId, incLb, incUb); } catch (TException e) { throw new RuntimeException(e); } }
@Override public <V> V execute(String query, ByteBuffer key, List<ByteBuffer> queryParams, ResultHandler<V> handler) throws TException { String formattedQuery = formatCqlQuery(query, queryParams, true); return handler.simpleNativeHandler().apply( client.execute_cql3_query(formattedQuery, key, Compression.NONE, state.settings.command.consistencyLevel) ); }
@Override public <V> V execute(String query, ByteBuffer key, List<ByteBuffer> queryParams, ResultHandler<V> handler) throws TException { String formattedQuery = formatCqlQuery(query, queryParams, false); return handler.simpleNativeHandler().apply( client.execute_cql_query(formattedQuery, key, Compression.NONE) ); }
@Override public <V> V execute(String query, ByteBuffer key, List<Object> queryParams, ResultHandler<V> handler) throws TException { String formattedQuery = formatCqlQuery(query, queryParams); return handler.simpleNativeHandler().apply( client.execute_cql3_query(formattedQuery, key, Compression.NONE, state.settings.command.consistencyLevel) ); }
@Override public Object createPreparedStatement(String cqlQuery) throws TException { return client.prepare_cql3_query(cqlQuery, Compression.NONE); }
@Override public Object createPreparedStatement(String cqlQuery) throws TException { return client.prepare_cql_query(cqlQuery, Compression.NONE); }
CqlResult execute_cql_query(ByteBuffer cqlQuery, Compression compression) throws Exception { return client.execute_cql3_query(cqlQuery, compression, ConsistencyLevel.ONE); }