Class CQLSSTableWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
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 ClassesModifier and TypeClassDescriptionstatic classA Builder for a CQLSSTableWriter object. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionAdds a new row to the writer.Adds a new row to the writer.Adds a new row to the writer.static CQLSSTableWriter.Builderbuilder()Returns a new builder for a CQLSSTableWriter.voidclose()Close this writer.Returns the User Defined type, used in this SSTable Writer, that can be used to create UDTValue instances.rawAddRow(ByteBuffer... values) Adds a new row to the writer given already serialized values.rawAddRow(List<ByteBuffer> values) Adds a new row to the writer given already serialized values.rawAddRow(Map<String, ByteBuffer> values) Adds a new row to the writer given already serialized values.
-
Field Details
-
UNSET_VALUE
-
-
Method Details
-
builder
Returns a new builder for a CQLSSTableWriter.- Returns:
- the new builder.
-
addRow
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:
InvalidRequestExceptionIOException
-
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:
InvalidRequestExceptionIOException
-
addRow
public CQLSSTableWriter addRow(Map<String, Object> values) throws InvalidRequestException, IOExceptionAdds 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 benull. 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:
InvalidRequestExceptionIOException
-
rawAddRow
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:
InvalidRequestExceptionIOException
-
rawAddRow
public CQLSSTableWriter rawAddRow(List<ByteBuffer> values) throws InvalidRequestException, 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:
InvalidRequestExceptionIOException
-
rawAddRow
public CQLSSTableWriter rawAddRow(Map<String, ByteBuffer> values) throws InvalidRequestException, IOExceptionAdds 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 benull. 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:
InvalidRequestExceptionIOException
-
getUDType
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
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-