1919
2020import java .io .File ;
2121import java .io .IOException ;
22- import java .util .*;
22+ import java .util .Arrays ;
23+ import java .util .HashSet ;
24+ import java .util .List ;
2325import java .util .concurrent .atomic .AtomicLong ;
2426import java .util .stream .Collectors ;
2527import java .util .stream .Stream ;
26- import java .util .stream .StreamSupport ;
27-
28- import org .apache .commons .cli .*;
2928
3029import org .apache .cassandra .schema .TableMetadata ;
3130import org .apache .cassandra .config .DatabaseDescriptor ;
32- import org .apache .cassandra .cql3 .ColumnIdentifier ;
3331import org .apache .cassandra .db .DecoratedKey ;
3432import org .apache .cassandra .db .PartitionPosition ;
35- import org .apache .cassandra .db .SerializationHeader ;
36- import org .apache .cassandra .db .marshal .UTF8Type ;
3733import org .apache .cassandra .db .rows .UnfilteredRowIterator ;
38- import org .apache .cassandra .dht .*;
34+ import org .apache .cassandra .dht .AbstractBounds ;
35+ import org .apache .cassandra .dht .Bounds ;
36+ import org .apache .cassandra .dht .IPartitioner ;
3937import org .apache .cassandra .exceptions .ConfigurationException ;
4038import org .apache .cassandra .io .sstable .Descriptor ;
4139import org .apache .cassandra .io .sstable .ISSTableScanner ;
4240import org .apache .cassandra .io .sstable .KeyIterator ;
4341import org .apache .cassandra .io .sstable .format .SSTableReader ;
42+ import org .apache .commons .cli .CommandLine ;
43+ import org .apache .commons .cli .CommandLineParser ;
44+ import org .apache .commons .cli .HelpFormatter ;
45+ import org .apache .commons .cli .Option ;
46+ import org .apache .commons .cli .Options ;
47+ import org .apache .commons .cli .ParseException ;
48+ import org .apache .commons .cli .PosixParser ;
4449import org .apache .cassandra .io .sstable .metadata .MetadataComponent ;
4550import org .apache .cassandra .io .sstable .metadata .MetadataType ;
4651import org .apache .cassandra .schema .TableMetadataRef ;
@@ -85,48 +90,6 @@ public class SSTableExport
8590 options .addOption (rawTimestamps );
8691 }
8792
88- /**
89- * Construct table schema from info stored in SSTable's Stats.db
90- *
91- * @param desc SSTable's descriptor
92- * @return Restored TableMetadata
93- * @throws IOException when Stats.db cannot be read
94- */
95- public static TableMetadata metadataFromSSTable (Descriptor desc ) throws IOException
96- {
97- if (!desc .version .isCompatible ())
98- throw new IOException ("Cannot process old and unsupported SSTable version." );
99-
100- EnumSet <MetadataType > types = EnumSet .of (MetadataType .STATS , MetadataType .HEADER );
101- Map <MetadataType , MetadataComponent > sstableMetadata = desc .getMetadataSerializer ().deserialize (desc , types );
102- SerializationHeader .Component header = (SerializationHeader .Component ) sstableMetadata .get (MetadataType .HEADER );
103- IPartitioner partitioner = FBUtilities .newPartitioner (desc );
104-
105- TableMetadata .Builder builder = TableMetadata .builder ("keyspace" , "table" ).partitioner (partitioner );
106- header .getStaticColumns ().entrySet ().stream ()
107- .forEach (entry -> {
108- ColumnIdentifier ident = ColumnIdentifier .getInterned (UTF8Type .instance .getString (entry .getKey ()), true );
109- builder .addStaticColumn (ident , entry .getValue ());
110- });
111- header .getRegularColumns ().entrySet ().stream ()
112- .forEach (entry -> {
113- ColumnIdentifier ident = ColumnIdentifier .getInterned (UTF8Type .instance .getString (entry .getKey ()), true );
114- builder .addRegularColumn (ident , entry .getValue ());
115- });
116- builder .addPartitionKeyColumn ("PartitionKey" , header .getKeyType ());
117- for (int i = 0 ; i < header .getClusteringTypes ().size (); i ++)
118- {
119- builder .addClusteringColumn ("clustering" + (i > 0 ? i : "" ), header .getClusteringTypes ().get (i ));
120- }
121- return builder .build ();
122- }
123-
124- private static <T > Stream <T > iterToStream (Iterator <T > iter )
125- {
126- Spliterator <T > splititer = Spliterators .spliteratorUnknownSize (iter , Spliterator .IMMUTABLE );
127- return StreamSupport .stream (splititer , false );
128- }
129-
13093 /**
13194 * Given arguments specifying an SSTable, and optionally an output file, export the contents of the SSTable to JSON.
13295 *
@@ -171,12 +134,12 @@ public static void main(String[] args) throws ConfigurationException
171134 Descriptor desc = Descriptor .fromFilename (ssTableFileName );
172135 try
173136 {
174- TableMetadata metadata = metadataFromSSTable (desc );
137+ TableMetadata metadata = Util . metadataFromSSTable (desc );
175138 if (cmd .hasOption (ENUMERATE_KEYS_OPTION ))
176139 {
177140 try (KeyIterator iter = new KeyIterator (desc , metadata ))
178141 {
179- JsonTransformer .keysToJson (null , iterToStream (iter ),
142+ JsonTransformer .keysToJson (null , Util . iterToStream (iter ),
180143 cmd .hasOption (RAW_TIMESTAMPS ),
181144 metadata ,
182145 System .out );
@@ -202,7 +165,7 @@ public static void main(String[] args) throws ConfigurationException
202165 {
203166 currentScanner = sstable .getScanner ();
204167 }
205- Stream <UnfilteredRowIterator > partitions = iterToStream (currentScanner ).filter (i ->
168+ Stream <UnfilteredRowIterator > partitions = Util . iterToStream (currentScanner ).filter (i ->
206169 excludes .isEmpty () || !excludes .contains (metadata .partitionKeyType .getString (i .partitionKey ().getKey ()))
207170 );
208171 if (cmd .hasOption (DEBUG_OUTPUT_OPTION ))
0 commit comments