Author: jbellis
Date: Wed Mar 3 16:02:37 2010
New Revision: 918539
URL: http://svn.apache.org/viewvc?rev=918539&view=rev
Log:
add -x option to sstable2json. patch by Brandon Williams; reviewed by jbellis
for CASSANDRA-843
Modified:
incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/tools/SSTableExport.java
incubator/cassandra/branches/cassandra-0.6/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
Modified:
incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/tools/SSTableExport.java
URL:
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/tools/SSTableExport.java?rev=918539&r1=918538&r2=918539&view=diff
==============================================================================
---
incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/tools/SSTableExport.java
(original)
+++
incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/tools/SSTableExport.java
Wed Mar 3 16:02:37 2010
@@ -21,8 +21,8 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
-import java.util.Collection;
-import java.util.Iterator;
+import java.util.*;
+
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.db.DecoratedKey;
@@ -47,6 +47,7 @@
private static int INPUT_FILE_BUFFER_SIZE = 8 * 1024 * 1024;
private static final String KEY_OPTION = "k";
+ private static final String EXCLUDEKEY_OPTION = "x";
private static final String ENUMERATEKEYS_OPTION = "e";
private static Options options;
private static CommandLine cmd;
@@ -60,6 +61,11 @@
optKey.setArgs(500);
options.addOption(optKey);
+ Option excludeKey = new Option(EXCLUDEKEY_OPTION, true, "Excluded row
key");
+ // Number of times -x <key> can be passed on the command line.
+ excludeKey.setArgs(500);
+ options.addOption(excludeKey);
+
Option optEnumerate = new Option(ENUMERATEKEYS_OPTION, false,
"enumerate keys only");
options.addOption(optEnumerate);
}
@@ -180,18 +186,21 @@
* @param keys the keys corresponding to the rows to export
* @throws IOException on failure to read/write input/output
*/
- public static void export(String ssTableFile, PrintStream outs, String[]
keys)
+ public static void export(String ssTableFile, PrintStream outs, String[]
keys, String[] excludes)
throws IOException
{
SSTableReader reader = SSTableReader.open(ssTableFile);
SSTableScanner scanner = reader.getScanner(INPUT_FILE_BUFFER_SIZE);
IPartitioner<?> partitioner = DatabaseDescriptor.getPartitioner();
+ Set<String> excludeSet = new HashSet<String>(Arrays.asList(excludes));
int i = 0;
outs.println("{");
for (String key : keys)
{
+ if (excludeSet.contains(key))
+ continue;
DecoratedKey<?> dk = partitioner.decorateKey(key);
scanner.seekTo(dk);
@@ -232,23 +241,26 @@
* @param keys the keys corresponding to the rows to export
* @throws IOException on failure to read/write input/output
*/
- public static void export(String ssTableFile, String outFile, String[]
keys) throws IOException
+ public static void export(String ssTableFile, String outFile, String[]
keys, String[] excludes) throws IOException
{
PrintStream outs = new PrintStream(outFile);
- export(ssTableFile, outs, keys);
+ export(ssTableFile, outs, keys, excludes);
}
// This is necessary to accommodate the test suite since you cannot open a
Reader more
// than once from within the same process.
- static void export(SSTableReader reader, PrintStream outs) throws
IOException
+ static void export(SSTableReader reader, PrintStream outs, String[]
excludes) throws IOException
{
SSTableScanner scanner = reader.getScanner(INPUT_FILE_BUFFER_SIZE);
-
+ Set<String> excludeSet = new HashSet<String>(Arrays.asList(excludes));
+
outs.println("{");
while(scanner.hasNext())
{
IteratingRow row = scanner.next();
+ if (excludeSet.contains(row.getKey().key))
+ continue;
try
{
String jsonOut = serializeRow(row);
@@ -281,10 +293,10 @@
* @param outs PrintStream to write the output to
* @throws IOException on failure to read/write input/output
*/
- public static void export(String ssTableFile, PrintStream outs) throws
IOException
+ public static void export(String ssTableFile, PrintStream outs, String[]
excludes) throws IOException
{
SSTableReader reader = SSTableReader.open(ssTableFile);
- export(reader, outs);
+ export(reader, outs, excludes);
}
/**
@@ -294,10 +306,10 @@
* @param outFile file to write output to
* @throws IOException on failure to read/write SSTable/output file
*/
- public static void export(String ssTableFile, String outFile) throws
IOException
+ public static void export(String ssTableFile, String outFile, String[]
excludes) throws IOException
{
PrintStream outs = new PrintStream(outFile);
- export(ssTableFile, outs);
+ export(ssTableFile, outs, excludes);
}
/**
@@ -306,9 +318,9 @@
* @param ssTableFile SSTable to export
* @throws IOException on failure to read/write SSTable/standard out
*/
- public static void export(String ssTableFile) throws IOException
+ public static void export(String ssTableFile, String[] excludes) throws
IOException
{
- export(ssTableFile, System.out);
+ export(ssTableFile, System.out, excludes);
}
/**
@@ -320,7 +332,7 @@
*/
public static void main(String[] args) throws IOException
{
- String usage = String.format("Usage: %s <sstable> [-k key [-k key
[...]]]%n", SSTableExport.class.getName());
+ String usage = String.format("Usage: %s <sstable> [-k key [-k key
[...]] -x key [-x key [...]]]%n", SSTableExport.class.getName());
CommandLineParser parser = new PosixParser();
try
@@ -343,15 +355,16 @@
String[] keys = cmd.getOptionValues(KEY_OPTION);
+ String[] excludes = cmd.getOptionValues(EXCLUDEKEY_OPTION);
String ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();
if (cmd.hasOption(ENUMERATEKEYS_OPTION))
enumeratekeys(ssTableFileName, System.out);
else {
if ((keys != null) && (keys.length > 0))
- export(ssTableFileName, System.out, keys);
+ export(ssTableFileName, System.out, keys, excludes);
else
- export(ssTableFileName);
+ export(ssTableFileName, excludes);
}
System.exit(0);
}
Modified:
incubator/cassandra/branches/cassandra-0.6/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
URL:
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/test/unit/org/apache/cassandra/tools/SSTableExportTest.java?rev=918539&r1=918538&r2=918539&view=diff
==============================================================================
---
incubator/cassandra/branches/cassandra-0.6/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
(original)
+++
incubator/cassandra/branches/cassandra-0.6/test/unit/org/apache/cassandra/tools/SSTableExportTest.java
Wed Mar 3 16:02:37 2010
@@ -103,12 +103,19 @@
writer.append(partitioner.decorateKey("rowB"), dob);
dob.reset();
cfamily.clear();
-
+
+ // Add rowExclude
+ cfamily.addColumn(new QueryPath("Standard1", null, "colX".getBytes()),
"valX".getBytes(), 1, false);
+ ColumnFamily.serializer().serializeWithIndexes(cfamily, dob);
+ writer.append(partitioner.decorateKey("rowExclude"), dob);
+ dob.reset();
+ cfamily.clear();
+
SSTableReader reader = writer.closeAndOpenReader();
// Export to JSON and verify
File tempJson = File.createTempFile("Standard1", ".json");
- SSTableExport.export(reader, new PrintStream(tempJson.getPath()));
+ SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new
String[]{"rowExclude"});
JSONObject json = (JSONObject)JSONValue.parse(new
FileReader(tempJson));
@@ -119,6 +126,9 @@
JSONArray rowB = (JSONArray)json.get("rowB");
JSONArray colB = (JSONArray)rowB.get(0);
assert !(Boolean)colB.get(3);
+
+ JSONArray rowExclude = (JSONArray)json.get("rowExclude");
+ assert rowExclude == null;
}
@Test
@@ -143,12 +153,19 @@
writer.append(partitioner.decorateKey("rowB"), dob);
dob.reset();
cfamily.clear();
-
+
+ // Add rowExclude
+ cfamily.addColumn(new QueryPath("Super4", "superX".getBytes(),
"colX".getBytes()), "valX".getBytes(), 1, false);
+ ColumnFamily.serializer().serializeWithIndexes(cfamily, dob);
+ writer.append(partitioner.decorateKey("rowExclude"), dob);
+ dob.reset();
+ cfamily.clear();
+
SSTableReader reader = writer.closeAndOpenReader();
// Export to JSON and verify
File tempJson = File.createTempFile("Super4", ".json");
- SSTableExport.export(reader, new PrintStream(tempJson.getPath()));
+ SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new
String[]{"rowExclude"});
JSONObject json = (JSONObject)JSONValue.parse(new
FileReader(tempJson));
@@ -156,9 +173,10 @@
JSONObject superA =
(JSONObject)rowA.get(cfamily.getComparator().getString("superA".getBytes()));
JSONArray subColumns = (JSONArray)superA.get("subColumns");
JSONArray colA = (JSONArray)subColumns.get(0);
-
+ JSONObject rowExclude = (JSONObject)json.get("rowExclude");
assert Arrays.equals(hexToBytes((String)colA.get(1)),
"valA".getBytes());
- assert !(Boolean)colA.get(3);
+ assert !(Boolean)colA.get(3);
+ assert rowExclude == null;
}
@Test
@@ -176,12 +194,19 @@
writer.append(partitioner.decorateKey("rowA"), dob);
dob.reset();
cfamily.clear();
-
+
+ // Add rowExclude
+ cfamily.addColumn(new QueryPath("Standard1", null, "name".getBytes()),
"val".getBytes(), 1, false);
+ ColumnFamily.serializer().serializeWithIndexes(cfamily, dob);
+ writer.append(partitioner.decorateKey("rowExclude"), dob);
+ dob.reset();
+ cfamily.clear();
+
SSTableReader reader = writer.closeAndOpenReader();
// Export to JSON and verify
File tempJson = File.createTempFile("Standard1", ".json");
- SSTableExport.export(reader, new PrintStream(tempJson.getPath()));
+ SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new
String[]{"rowExclude"});
// Import JSON to another SSTable file
File tempSS2 = createTemporarySSTable("Keyspace1", "Standard1");
@@ -192,6 +217,11 @@
ColumnFamily cf =
qf.getSSTableColumnIterator(reader).getColumnFamily();
assertTrue(cf != null);
assertTrue(Arrays.equals(cf.getColumn("name".getBytes()).value(),
hexToBytes("76616c")));
+
+ qf = new NamesQueryFilter("rowExclude", new QueryPath("Standard1",
null, null), "name".getBytes());
+ cf = qf.getSSTableColumnIterator(reader).getColumnFamily();
+ assert cf == null;
+
}
}