Class CQLSSTableWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class CQLSSTableWriter
    extends java.lang.Object
    implements java.io.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).

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CQLSSTableWriter.Builder
      A Builder for a CQLSSTableWriter object.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.nio.ByteBuffer UNSET_VALUE  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      CQLSSTableWriter addRow​(java.lang.Object... values)
      Adds a new row to the writer.
      CQLSSTableWriter addRow​(java.util.List<java.lang.Object> values)
      Adds a new row to the writer.
      CQLSSTableWriter addRow​(java.util.Map<java.lang.String,​java.lang.Object> values)
      Adds a new row to the writer.
      static CQLSSTableWriter.Builder builder()
      Returns a new builder for a CQLSSTableWriter.
      void close()
      Close this writer.
      UserType getUDType​(java.lang.String dataType)
      Returns the User Defined type, used in this SSTable Writer, that can be used to create UDTValue instances.
      CQLSSTableWriter rawAddRow​(java.nio.ByteBuffer... values)
      Adds a new row to the writer given already serialized values.
      CQLSSTableWriter rawAddRow​(java.util.List<java.nio.ByteBuffer> values)
      Adds a new row to the writer given already serialized values.
      CQLSSTableWriter rawAddRow​(java.util.Map<java.lang.String,​java.nio.ByteBuffer> values)
      Adds a new row to the writer given already serialized values.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNSET_VALUE

        public static final java.nio.ByteBuffer UNSET_VALUE
    • Method Detail

      • builder

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

        public CQLSSTableWriter addRow​(java.lang.Object... values)
                                throws InvalidRequestException,
                                       java.io.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
        java.io.IOException
      • addRow

        public CQLSSTableWriter addRow​(java.util.List<java.lang.Object> values)
                                throws InvalidRequestException,
                                       java.io.IOException
        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
        java.io.IOException
      • addRow

        public CQLSSTableWriter addRow​(java.util.Map<java.lang.String,​java.lang.Object> values)
                                throws InvalidRequestException,
                                       java.io.IOException
        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
        java.io.IOException
      • rawAddRow

        public CQLSSTableWriter rawAddRow​(java.nio.ByteBuffer... values)
                                   throws InvalidRequestException,
                                          java.io.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
        java.io.IOException
      • rawAddRow

        public CQLSSTableWriter rawAddRow​(java.util.List<java.nio.ByteBuffer> values)
                                   throws InvalidRequestException,
                                          java.io.IOException
        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
        java.io.IOException
      • rawAddRow

        public CQLSSTableWriter rawAddRow​(java.util.Map<java.lang.String,​java.nio.ByteBuffer> values)
                                   throws InvalidRequestException,
                                          java.io.IOException
        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
        java.io.IOException
      • getUDType

        public UserType getUDType​(java.lang.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 java.io.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 java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException