Class CQLSSTableWriter

java.lang.Object
org.apache.cassandra.io.sstable.CQLSSTableWriter
All Implemented Interfaces:
Closeable, AutoCloseable

public class CQLSSTableWriter extends Object implements Closeable
Utility to write SSTables.

Typical usage looks like:

   String type = CREATE TYPE myKs.myType (a int, b int)";
   String schema = "CREATE TABLE myKs.myTable ("
                 + "  k int PRIMARY KEY,"
                 + "  v1 text,"
                 + "  v2 int,"
                 + "  v3 myType,"
                 + ")";
   String insert = "INSERT INTO myKs.myTable (k, v1, v2, v3) VALUES (?, ?, ?, ?)";

   // Creates a new writer. You need to provide at least the directory where to write the created sstable,
   // the schema for the sstable to write and a (prepared) modification statement to use. If you do not use the
   // default partitioner (Murmur3Partitioner), you will also need to provide the partitioner in use, see
   // CQLSSTableWriter.Builder for more details on the available options.
   CQLSSTableWriter writer = CQLSSTableWriter.builder()
                                             .inDirectory("path/to/directory")
                                             .withType(type)
                                             .forTable(schema)
                                             .using(insert).build();

   UserType myType = writer.getUDType("myType");
   // Adds a nember of rows to the resulting sstable
   writer.addRow(0, "test1", 24, myType.newValue().setInt("a", 10).setInt("b", 20));
   writer.addRow(1, "test2", null, null);
   writer.addRow(2, "test3", 42, myType.newValue().setInt("a", 30).setInt("b", 40));

   // Close the writer, finalizing the sstable
   writer.close();
 

Please note that CQLSSTableWriter is not thread-safe (multiple threads cannot access the same instance). It is however safe to use multiple instances in parallel (even if those instance write sstables for the same table).

  • Field Details

    • UNSET_VALUE

      public static final ByteBuffer UNSET_VALUE
  • Method Details

    • builder

      public static CQLSSTableWriter.Builder builder()
      Returns a new builder for a CQLSSTableWriter.
      Returns:
      the new builder.
    • addRow

      public CQLSSTableWriter addRow(Object... values) throws InvalidRequestException, IOException
      Adds a new row to the writer.

      This is a shortcut for addRow(Arrays.asList(values)).

      Parameters:
      values - the row values (corresponding to the bind variables of the modification statement used when creating by this writer).
      Returns:
      this writer.
      Throws:
      InvalidRequestException
      IOException
    • addRow

      Adds a new row to the writer.

      Each provided value type should correspond to the types of the CQL column the value is for. The correspondance between java type and CQL type is the same one than the one documented at www.datastax.com/drivers/java/2.0/apidocs/com/datastax/driver/core/DataType.Name.html#asJavaClass().

      If you prefer providing the values directly as binary, use rawAddRow(java.nio.ByteBuffer...) instead.

      Parameters:
      values - the row values (corresponding to the bind variables of the modification statement used when creating by this writer).
      Returns:
      this writer.
      Throws:
      InvalidRequestException
      IOException
    • addRow

      Adds a new row to the writer.

      This is equivalent to the other addRow methods, but takes a map whose keys are the names of the columns to add instead of taking a list of the values in the order of the modification statement used during construction of this write.

      Please note that the column names in the map keys must be in lowercase unless the declared column name is a case-sensitive quoted identifier (in which case the map key must use the exact case of the column).

      Parameters:
      values - a map of colum name to column values representing the new row to add. Note that if a column is not part of the map, it's value will be null. If the map contains keys that does not correspond to one of the column of the modification statement used when creating this writer, the the corresponding value is ignored.
      Returns:
      this writer.
      Throws:
      InvalidRequestException
      IOException
    • rawAddRow

      public CQLSSTableWriter rawAddRow(ByteBuffer... values) throws InvalidRequestException, IOException
      Adds a new row to the writer given already serialized values.
      Parameters:
      values - the row values (corresponding to the bind variables of the modification statement used when creating by this writer) as binary.
      Returns:
      this writer.
      Throws:
      InvalidRequestException
      IOException
    • rawAddRow

      Adds a new row to the writer given already serialized values.

      This is a shortcut for rawAddRow(Arrays.asList(values)).

      Parameters:
      values - the row values (corresponding to the bind variables of the modification statement used when creating by this writer) as binary.
      Returns:
      this writer.
      Throws:
      InvalidRequestException
      IOException
    • rawAddRow

      Adds a new row to the writer given already serialized values.

      This is equivalent to the other rawAddRow methods, but takes a map whose keys are the names of the columns to add instead of taking a list of the values in the order of the modification statement used during construction of this write.

      Parameters:
      values - a map of colum name to column values representing the new row to add. Note that if a column is not part of the map, it's value will be null. If the map contains keys that does not correspond to one of the column of the modification statement used when creating this writer, the the corresponding value is ignored.
      Returns:
      this writer.
      Throws:
      InvalidRequestException
      IOException
    • getUDType

      public UserType getUDType(String dataType)
      Returns the User Defined type, used in this SSTable Writer, that can be used to create UDTValue instances.
      Parameters:
      dataType - name of the User Defined type
      Returns:
      user defined type
    • close

      public void close() throws IOException
      Close this writer.

      This method should be called, otherwise the produced sstables are not guaranteed to be complete (and won't be in practice).

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException