Example usage for org.apache.cassandra.io.sstable CQLSSTableWriter addRow

List of usage examples for org.apache.cassandra.io.sstable CQLSSTableWriter addRow

Introduction

In this page you can find the example usage for org.apache.cassandra.io.sstable CQLSSTableWriter addRow.

Prototype

public CQLSSTableWriter addRow(Map<String, Object> values) throws InvalidRequestException, IOException 

Source Link

Document

Adds a new row to the writer.

Usage

From source file:test.GenSstable.java

License:Apache License

public static void main(String[] args) {
    if (args.length != 4) {
        System.out.println("usage: java test.gendata tablename no_years no_pnodes no_prices");
        return;/*from  ww w  . ja v a 2  s  . co m*/
    }

    String tblname = args[0];
    int yearsNb = Integer.parseInt(args[1]);
    int pnodesNb = Integer.parseInt(args[2]);
    int pricesNb = Integer.parseInt(args[3]);

    // magic!
    Config.setClientMode(true);

    String dir = DEFAULT_OUTPUT_DIR;
    // Create output directory that has keyspace and table name in the path
    File outputDir = new File(dir + File.separator + KEYSPACE + File.separator + tblname);
    if (!outputDir.exists() && !outputDir.mkdirs()) {
        throw new RuntimeException("Cannot create output directory: " + outputDir);
    }

    // Prepare SSTable writer
    CQLSSTableWriter.Builder builder = CQLSSTableWriter.builder();
    // set output directory
    builder.inDirectory(outputDir)
            // set target schema
            .forTable(getSchema(tblname, pricesNb))
            // set CQL statement to put data
            .using(createInsertStatement(tblname, pricesNb))
            // set partitioner if needed
            // default is Murmur3Partitioner so set if you use different one.
            .withPartitioner(new Murmur3Partitioner());
    CQLSSTableWriter writer = builder.build();

    Collection<String> names = generateNames(pnodesNb);
    SortedSet<Date> dates = generateDates(yearsNb);

    try {
        for (String name : names) {
            System.out.println("Inserting values for " + name + "...");
            for (Date date : dates) {
                List<Object> values = new ArrayList<>();

                values.add(name);
                values.add(date);
                values.addAll(generatePrices(pricesNb));

                writer.addRow(values);
            }
        }
    } catch (IOException | InvalidRequestException e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    System.out.println("Done generating sstable.");
}