Search in sources :

Example 1 with SSTableWriter

use of org.apache.cassandra.io.sstable.SSTableWriter in project eiger by wlloyd.

the class SSTableImport method importSorted.

public static int importSorted(String jsonFile, ColumnFamily columnFamily, String ssTablePath, IPartitioner<?> partitioner) throws IOException {
    // already imported keys count
    int importedKeys = 0;
    long start = System.currentTimeMillis();
    JsonParser parser = getParser(jsonFile);
    if (keyCountToImport == null) {
        keyCountToImport = 0;
        System.out.println("Counting keys to import, please wait... (NOTE: to skip this use -n <num_keys>)");
        // START_OBJECT
        parser.nextToken();
        while (parser.nextToken() != null) {
            parser.nextToken();
            parser.skipChildren();
            if (parser.getCurrentName() == null)
                continue;
            keyCountToImport++;
        }
    }
    System.out.printf("Importing %s keys...%n", keyCountToImport);
    // renewing parser
    parser = getParser(jsonFile);
    SSTableWriter writer = new SSTableWriter(ssTablePath, keyCountToImport);
    int lineNumber = 1;
    DecoratedKey prevStoredKey = null;
    while (parser.nextToken() != null) {
        String key = parser.getCurrentName();
        if (key != null) {
            String tokenName = parser.nextToken().name();
            if (tokenName.equals("START_ARRAY")) {
                if (columnFamily.getType() == ColumnFamilyType.Super) {
                    throw new RuntimeException("Can't write Standard columns to the Super Column Family.");
                }
                List<?> columns = parser.readValueAs(new TypeReference<List<?>>() {
                });
                addToStandardCF(columns, columnFamily);
            } else if (tokenName.equals("START_OBJECT")) {
                if (columnFamily.getType() == ColumnFamilyType.Standard) {
                    throw new RuntimeException("Can't write Super columns to the Standard Column Family.");
                }
                Map<?, ?> columns = parser.readValueAs(new TypeReference<Map<?, ?>>() {
                });
                addToSuperCF(columns, columnFamily);
            } else {
                throw new UnsupportedOperationException("Only Array or Hash allowed as row content.");
            }
            DecoratedKey currentKey = partitioner.decorateKey(hexToBytes(key));
            if (prevStoredKey != null && prevStoredKey.compareTo(currentKey) != -1) {
                System.err.printf("Line %d: Key %s is greater than previous, collection is not sorted properly. Aborting import. You might need to delete SSTables manually.%n", lineNumber, key);
                return -1;
            }
            // saving decorated key
            writer.append(currentKey, columnFamily);
            columnFamily.clear();
            prevStoredKey = currentKey;
            importedKeys++;
            lineNumber++;
            long current = System.currentTimeMillis();
            if (// 5 secs.
            current - start >= 5000) {
                System.out.printf("Currently imported %d keys.%n", importedKeys);
                start = current;
            }
            if (keyCountToImport == importedKeys)
                break;
        }
    }
    writer.closeAndOpenReader();
    return importedKeys;
}
Also used : SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) List(java.util.List) TypeReference(org.codehaus.jackson.type.TypeReference) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JsonParser(org.codehaus.jackson.JsonParser)

Example 2 with SSTableWriter

use of org.apache.cassandra.io.sstable.SSTableWriter in project eiger by wlloyd.

the class SSTableExportTest method testEnumeratekeys.

@Test
public void testEnumeratekeys() throws IOException {
    File tempSS = tempSSTableFile("Keyspace1", "Standard1");
    ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "Standard1");
    SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2);
    // Add rowA
    cfamily.addColumn(new QueryPath("Standard1", null, ByteBufferUtil.bytes("colA")), ByteBufferUtil.bytes("valA"), System.currentTimeMillis());
    writer.append(Util.dk("rowA"), cfamily);
    cfamily.clear();
    // Add rowB
    cfamily.addColumn(new QueryPath("Standard1", null, ByteBufferUtil.bytes("colB")), ByteBufferUtil.bytes("valB"), System.currentTimeMillis());
    writer.append(Util.dk("rowB"), cfamily);
    cfamily.clear();
    writer.closeAndOpenReader();
    // Enumerate and verify
    File temp = File.createTempFile("Standard1", ".txt");
    SSTableExport.enumeratekeys(writer.getFilename(), new PrintStream(temp.getPath()));
    FileReader file = new FileReader(temp);
    char[] buf = new char[(int) temp.length()];
    file.read(buf);
    String output = new String(buf);
    String sep = System.getProperty("line.separator");
    assert output.equals(asHex("rowA") + sep + asHex("rowB") + sep) : output;
}
Also used : QueryPath(org.apache.cassandra.db.filter.QueryPath) PrintStream(java.io.PrintStream) SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) FileReader(java.io.FileReader) File(java.io.File) SSTableUtils.tempSSTableFile(org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile) ColumnFamily(org.apache.cassandra.db.ColumnFamily) Test(org.junit.Test)

Example 3 with SSTableWriter

use of org.apache.cassandra.io.sstable.SSTableWriter in project eiger by wlloyd.

the class SSTableExportTest method testEscapingDoubleQuotes.

@Test
public void testEscapingDoubleQuotes() throws IOException {
    File tempSS = tempSSTableFile("Keyspace1", "ValuesWithQuotes");
    ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "ValuesWithQuotes");
    SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2);
    // Add rowA
    cfamily.addColumn(null, new Column(ByteBufferUtil.bytes("data"), UTF8Type.instance.fromString("{\"foo\":\"bar\"}")));
    writer.append(Util.dk("rowA"), cfamily);
    cfamily.clear();
    SSTableReader reader = writer.closeAndOpenReader();
    // Export to JSON and verify
    File tempJson = File.createTempFile("ValuesWithQuotes", ".json");
    SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new String[0]);
    JSONObject json = (JSONObject) JSONValue.parse(new FileReader(tempJson));
    JSONArray rowA = (JSONArray) json.get(asHex("rowA"));
    JSONArray data = (JSONArray) rowA.get(0);
    assert hexToBytes((String) data.get(0)).equals(ByteBufferUtil.bytes("data"));
    assert data.get(1).equals("{\"foo\":\"bar\"}");
}
Also used : PrintStream(java.io.PrintStream) SSTableReader(org.apache.cassandra.io.sstable.SSTableReader) JSONObject(org.json.simple.JSONObject) Column(org.apache.cassandra.db.Column) ExpiringColumn(org.apache.cassandra.db.ExpiringColumn) CounterColumn(org.apache.cassandra.db.CounterColumn) SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) JSONArray(org.json.simple.JSONArray) FileReader(java.io.FileReader) File(java.io.File) SSTableUtils.tempSSTableFile(org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile) ColumnFamily(org.apache.cassandra.db.ColumnFamily) Test(org.junit.Test)

Example 4 with SSTableWriter

use of org.apache.cassandra.io.sstable.SSTableWriter in project eiger by wlloyd.

the class SSTableExportTest method testExportCounterCf.

@Test
public void testExportCounterCf() throws IOException {
    File tempSS = tempSSTableFile("Keyspace1", "Counter1");
    ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "Counter1");
    SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2);
    // Add rowA
    cfamily.addColumn(null, new CounterColumn(ByteBufferUtil.bytes("colA"), 42, System.currentTimeMillis()));
    writer.append(Util.dk("rowA"), cfamily);
    cfamily.clear();
    SSTableReader reader = writer.closeAndOpenReader();
    // Export to JSON and verify
    File tempJson = File.createTempFile("Counter1", ".json");
    SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new String[0]);
    JSONObject json = (JSONObject) JSONValue.parse(new FileReader(tempJson));
    JSONArray rowA = (JSONArray) json.get(asHex("rowA"));
    JSONArray colA = (JSONArray) rowA.get(0);
    assert hexToBytes((String) colA.get(0)).equals(ByteBufferUtil.bytes("colA"));
    assert ((String) colA.get(3)).equals("c");
    assert (Long) colA.get(4) == Long.MIN_VALUE;
}
Also used : PrintStream(java.io.PrintStream) SSTableReader(org.apache.cassandra.io.sstable.SSTableReader) JSONObject(org.json.simple.JSONObject) SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) JSONArray(org.json.simple.JSONArray) CounterColumn(org.apache.cassandra.db.CounterColumn) FileReader(java.io.FileReader) File(java.io.File) SSTableUtils.tempSSTableFile(org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile) ColumnFamily(org.apache.cassandra.db.ColumnFamily) Test(org.junit.Test)

Example 5 with SSTableWriter

use of org.apache.cassandra.io.sstable.SSTableWriter in project eiger by wlloyd.

the class SSTableImport method importUnsorted.

private static int importUnsorted(JsonParser parser, ColumnFamily columnFamily, String ssTablePath, IPartitioner<?> partitioner) throws IOException {
    int importedKeys = 0;
    long start = System.currentTimeMillis();
    Map<?, ?> data = parser.readValueAs(new TypeReference<Map<?, ?>>() {
    });
    keyCountToImport = (keyCountToImport == null) ? data.size() : keyCountToImport;
    SSTableWriter writer = new SSTableWriter(ssTablePath, keyCountToImport);
    System.out.printf("Importing %s keys...%n", keyCountToImport);
    // sort by dk representation, but hold onto the hex version
    SortedMap<DecoratedKey, String> decoratedKeys = new TreeMap<DecoratedKey, String>();
    for (Object keyObject : data.keySet()) {
        String key = (String) keyObject;
        decoratedKeys.put(partitioner.decorateKey(hexToBytes(key)), key);
    }
    for (Map.Entry<DecoratedKey, String> rowKey : decoratedKeys.entrySet()) {
        if (columnFamily.getType() == ColumnFamilyType.Super) {
            addToSuperCF((Map<?, ?>) data.get(rowKey.getValue()), columnFamily);
        } else {
            addToStandardCF((List<?>) data.get(rowKey.getValue()), columnFamily);
        }
        writer.append(rowKey.getKey(), columnFamily);
        columnFamily.clear();
        importedKeys++;
        long current = System.currentTimeMillis();
        if (// 5 secs.
        current - start >= 5000) {
            System.out.printf("Currently imported %d keys.%n", importedKeys);
            start = current;
        }
        if (keyCountToImport == importedKeys)
            break;
    }
    writer.closeAndOpenReader();
    return importedKeys;
}
Also used : SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

SSTableWriter (org.apache.cassandra.io.sstable.SSTableWriter)10 File (java.io.File)8 SSTableReader (org.apache.cassandra.io.sstable.SSTableReader)7 PrintStream (java.io.PrintStream)6 ColumnFamily (org.apache.cassandra.db.ColumnFamily)6 SSTableUtils.tempSSTableFile (org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile)6 Test (org.junit.Test)6 FileReader (java.io.FileReader)5 QueryPath (org.apache.cassandra.db.filter.QueryPath)4 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 IOException (java.io.IOException)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 CounterColumn (org.apache.cassandra.db.CounterColumn)2 ExpiringColumn (org.apache.cassandra.db.ExpiringColumn)2 List (java.util.List)1 Column (org.apache.cassandra.db.Column)1 DecoratedKey (org.apache.cassandra.db.DecoratedKey)1