Skip to content

Commit 80bc114

Browse files
Chris LohfinkChris Lohfink
authored andcommitted
update README for hints tool
1 parent cee4e4c commit 80bc114

File tree

2 files changed

+121
-41
lines changed

2 files changed

+121
-41
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ since been merged into Cassandra starting with versions 3.0.4 and 3.4 as the `ss
1414
Example usage:
1515

1616
java -jar sstable-tools.jar cqlsh
17+
java -jar sstable-tools.jar hints 1458779867606-1.hints
1718
java -jar sstable-tools.jar describe ma-2-big-Data.db
1819

1920
Example shell usage:
@@ -112,6 +113,7 @@ Example shell usage:
112113

113114
* [cqlsh](#cqlsh) - Drop into an interactive shell to make queries against SSTables.
114115
* [describe](#describe) - Describe SSTable data and metadata.
116+
* [hints](#hints) - Dump hints from a hint file
115117

116118
## Building
117119

@@ -286,3 +288,37 @@ RegularColumns: {val:org.apache.cassandra.db.marshal.UTF8Type}
286288
java -jar sstable-tools.jar describe /path/to/file.db
287289
```
288290

291+
## hints
292+
293+
Deserialize the mutations in a hints file and print them to standard out. To have the information necessary do deserialize
294+
the mutations this tool requires the schema of the file. This is currently handled by connecting to the cluster and querying
295+
the metadata. You can specify the host and (cql) port via the `-h` and `-p` options.
296+
297+
Example Output:
298+
299+
```
300+
java -jar sstable-tools.jar hints 1458786695234-1.hints
301+
Loading schema from 127.0.0.1:9042
302+
/Users/clohfink/1458786695234-1.hints
303+
=====================================
304+
[test.t1] key=1 columns=[[] | [val]]
305+
Row: EMPTY | val=1
306+
[[val=1 ts=1458786688691698]]
307+
308+
```
309+
310+
### Usage
311+
312+
```
313+
java -jar sstable-tools.jar hints
314+
315+
usage: hints [-h <arg>] [-p <arg>] [-s] hintfile [hintfile ...]
316+
317+
Hint Dump for Apache Cassandra 3.x
318+
Options:
319+
-h <arg> Host to extract schema frome.
320+
-p <arg> CQL native port.
321+
-s Only output mutations.
322+
hintfile at least one file containing hints
323+
324+
```

src/main/java/org/apache/cassandra/hints/HintsTool.java

Lines changed: 85 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,114 @@
22

33

44
import com.csforge.sstable.CassandraUtils;
5-
import com.csforge.sstable.TableTransformer;
65
import com.google.common.base.Strings;
76
import org.apache.cassandra.config.Config;
87
import org.apache.cassandra.db.Mutation;
98
import org.apache.cassandra.db.partitions.PartitionUpdate;
109
import org.apache.cassandra.db.rows.Row;
1110
import org.apache.cassandra.io.util.DataInputPlus;
1211
import org.apache.cassandra.net.MessagingService;
12+
import org.apache.commons.cli.*;
1313

14-
import java.io.ByteArrayInputStream;
15-
import java.io.File;
14+
import java.io.*;
1615
import java.nio.ByteBuffer;
1716
import java.util.Iterator;
1817

18+
import static com.csforge.sstable.TableTransformer.*;
19+
1920
public class HintsTool {
21+
private static final String HOST_OPTION = "h";
22+
private static final String PORT_OPTION = "p";
23+
private static final String SILENT_OPTION = "s";
24+
private static final Options options = new Options();
2025

2126
static {
2227
Config.setClientMode(true);
28+
Option hostOption = new Option(HOST_OPTION, true, "Host to extract schema frome.");
29+
hostOption.setRequired(false);
30+
options.addOption(hostOption);
31+
Option portOption = new Option(PORT_OPTION, true, "CQL native port.");
32+
portOption.setRequired(false);
33+
options.addOption(portOption);
34+
Option silentOption = new Option(SILENT_OPTION, false, "Only output mutations.");
35+
silentOption.setRequired(false);
36+
options.addOption(silentOption);
2337
}
2438

25-
public static void main(String... args) {
26-
String host = "127.0.0.1";
27-
int port = 9042;
28-
File hint_file = null;
39+
private static void dumpHints(File hint_file, PrintStream out, int version) throws IOException {
40+
try (HintsReader reader = HintsReader.open(hint_file)) {
41+
for (HintsReader.Page page : reader) {
42+
Iterator<ByteBuffer> hints = page.buffersIterator(); //hints iterator im pretty sure is busted
43+
while (hints.hasNext()) {
44+
ByteBuffer buf = hints.next();
45+
DataInputPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(buf.array()));
46+
Hint hint = Hint.serializer.deserialize(in, version);
47+
Mutation mutation = hint.mutation;
48+
for (PartitionUpdate partition : mutation.getPartitionUpdates()) {
49+
out.println(partition);
50+
for (Row row : partition) {
51+
out.println(row);
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
2958

30-
if (args.length == 1) {
31-
hint_file = new File(args[0]);
32-
} else if (args.length == 2) {
33-
host = args[0];
34-
hint_file = new File(args[1]);
35-
} else if (args.length == 3) {
36-
host = args[0];
37-
port = Integer.parseInt(args[1]);
38-
hint_file = new File(args[2]);
39-
} else {
40-
System.err.println("Usage: hints <host> <port> <hint_file>\n hints <host> <hint_file>\n" +
41-
" hints <hint_file>");
42-
System.exit(-1);
59+
private static void printHelp() {
60+
try (PrintWriter errWriter = new PrintWriter(System.err, true)) {
61+
HelpFormatter formatter = new HelpFormatter();
62+
formatter.printHelp(errWriter, 120, "hints hintfile [hintfile ...]",
63+
String.format("%nHint Dump for Apache Cassandra 3.x%nOptions:"),
64+
options, 2, 1, "", true);
65+
errWriter.println(" hintfile at least one file containing hints");
66+
errWriter.println();
4367
}
68+
}
4469

70+
public static void main(String... args) {
71+
CommandLineParser parser = new PosixParser();
72+
CommandLine cmd = null;
4573
try {
46-
System.out.printf("%sLoading schema from %s:%s", TableTransformer.ANSI_RED, host, port);
47-
System.out.flush();
48-
CassandraUtils.loadTablesFromRemote(host, port);
49-
System.out.println("\r\u001B[1;34m" + hint_file.getAbsolutePath());
50-
System.out.println(TableTransformer.ANSI_CYAN + Strings.repeat("=", hint_file.getAbsolutePath().length()));
51-
System.out.print(TableTransformer.ANSI_RESET);
52-
int version = Integer.parseInt(System.getProperty("hints.version", "" + MessagingService.current_version));
74+
cmd = parser.parse(options, args);
75+
} catch (ParseException e) {
76+
System.err.format("%sFailure parsing arguments: %s%s%n%n", ANSI_RED, e.getMessage(), ANSI_RESET);
77+
printHelp();
78+
System.exit(-1);
5379

54-
try (HintsReader reader = HintsReader.open(hint_file)) {
55-
for (HintsReader.Page page : reader) {
56-
Iterator<ByteBuffer> hints = page.buffersIterator(); //hints iterator im pretty sure is busted
57-
while (hints.hasNext()) {
58-
ByteBuffer buf = hints.next();
59-
DataInputPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(buf.array()));
60-
Hint hint = Hint.serializer.deserialize(in, version);
61-
Mutation mutation = hint.mutation;
62-
for (PartitionUpdate partition : mutation.getPartitionUpdates()) {
63-
System.out.println(partition);
64-
for (Row row : partition) {
65-
System.out.println(row);
66-
}
67-
}
80+
}
81+
if(cmd.getArgs().length == 0) {
82+
printHelp();
83+
System.exit(-2);
84+
}
85+
String host = cmd.getOptionValue(HOST_OPTION, "127.0.0.1");
86+
int port = Integer.parseInt(cmd.getOptionValue(PORT_OPTION, "9042"));
87+
try {
88+
int version = MessagingService.current_version;
89+
try {
90+
if (!cmd.hasOption(SILENT_OPTION)) {
91+
System.out.printf("%sLoading schema from %s:%s%s", ANSI_RED, host, port, ANSI_RESET);
92+
System.out.flush();
93+
}
94+
CassandraUtils.loadTablesFromRemote(host, port);
95+
version = Integer.parseInt(System.getProperty("hints.version", "" + MessagingService.current_version));
96+
} finally {
97+
System.out.print(ANSI_RESET);
98+
}
99+
for (String hint : cmd.getArgs()) {
100+
File hint_file = new File(hint);
101+
if (!hint_file.exists()) {
102+
if(!cmd.hasOption(SILENT_OPTION)) {
103+
System.out.println("\r" + ANSI_RESET);
104+
}
105+
System.err.println("Non-existant hint file provided: " + hint);
106+
} else {
107+
if(!cmd.hasOption(SILENT_OPTION)) {
108+
System.out.println("\r\u001B[1;34m" + hint_file.getAbsolutePath());
109+
System.out.println(ANSI_CYAN + Strings.repeat("=", hint_file.getAbsolutePath().length()));
110+
System.out.print(ANSI_RESET);
68111
}
112+
dumpHints(hint_file, System.out, version);
69113
}
70114
}
71115
} catch (Exception e) {

0 commit comments

Comments
 (0)