org.apache.cassandra.thrift.Compression Java Examples

The following examples show how to use org.apache.cassandra.thrift.Compression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraOutputData.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #2
Source File: CassandraOutputData.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #3
Source File: CassandraInputData.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #4
Source File: CassandraThriftFacade.java    From emodb with Apache License 2.0 5 votes vote down vote up
/** @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);
    }
}
 
Example #5
Source File: CassandraThriftFacade.java    From emodb with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #6
Source File: CqlOperation.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@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)
    );
}
 
Example #7
Source File: CqlOperation.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@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)
    );
}
 
Example #8
Source File: CqlOperation.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public Object createPreparedStatement(String cqlQuery) throws TException
{
    return client.prepare_cql3_query(cqlQuery, Compression.NONE);
}
 
Example #9
Source File: CqlOperation.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public Object createPreparedStatement(String cqlQuery) throws TException
{
    return client.prepare_cql_query(cqlQuery, Compression.NONE);
}
 
Example #10
Source File: CassandraOutputData.java    From learning-hadoop with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs and executes a CQL TRUNCATE statement.
 * 
 * @param conn
 *            the connection to use
 * @param columnFamily
 *            the name of the column family to truncate.
 * @throws Exception
 *             if a problem occurs.
 */
public static void truncateColumnFamily(CassandraConnection conn,
		String columnFamily) throws Exception {
	String cqlCommand = "TRUNCATE " + columnFamily;

	conn.getClient().execute_cql_query(
			ByteBuffer.wrap(cqlCommand.getBytes()), Compression.NONE);
}