Example usage for org.apache.cassandra.io.sstable Descriptor getMetadataSerializer

List of usage examples for org.apache.cassandra.io.sstable Descriptor getMetadataSerializer

Introduction

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

Prototype

public IMetadataSerializer getMetadataSerializer() 

Source Link

Usage

From source file:com.wenyu.cassandra.tools.SSTableLevelToZero.java

License:Apache License

/**
 * @param args a list of sstables whose metadata we are changing
 *///from   ww w. j  a va 2s  . c  om
public static void main(String[] args) throws IOException {
    PrintStream out = System.out;
    if (args.length < 3) {
        out.println("This command should be run with Cassandra stopped!");
        out.println("Usage: sstableleveltozero <keyspace> <columnfamily> <sstablefiles_fullpath>");
        System.exit(1);
    }

    // TODO several daemon threads will run from here.
    // So we have to explicitly call System.exit.
    try {
        // load keyspace descriptions.
        DatabaseDescriptor.loadSchemas();

        String keyspaceName = args[0];
        String columnfamily = args[1];
        String sstables = args[2];

        Set<String> sstableSet = new HashSet<>();
        for (String sstable : sstables.split(",")) {
            sstable = sstable.trim();
            if (sstable.length() == 0) {
                continue;
            }
            sstableSet.add(sstable);
        }

        // validate columnfamily
        if (Schema.instance.getCFMetaData(keyspaceName, columnfamily) == null) {
            System.err.println("ColumnFamily not found: " + keyspaceName + "/" + columnfamily);
            System.exit(1);
        }

        Keyspace keyspace = Keyspace.openWithoutSSTables(keyspaceName);
        ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(columnfamily);
        boolean foundSSTable = false;
        for (Map.Entry<Descriptor, Set<Component>> sstable : cfs.directories.sstableLister().list()
                .entrySet()) {
            if (sstable.getValue().contains(Component.STATS)) {
                foundSSTable = true;
                Descriptor descriptor = sstable.getKey();

                StatsMetadata metadata = (StatsMetadata) descriptor.getMetadataSerializer()
                        .deserialize(descriptor, MetadataType.STATS);
                String path = descriptor.filenameFor(Component.DATA);
                if (metadata.sstableLevel > 0 && sstableSet.contains(path)) {
                    out.println("Changing level from " + metadata.sstableLevel + " to 0 on "
                            + descriptor.filenameFor(Component.DATA));
                    descriptor.getMetadataSerializer().mutateLevel(descriptor, 0);
                } else if (metadata.sstableLevel <= 0 && sstableSet.contains(path)) {
                    out.println("Skipped " + descriptor.filenameFor(Component.DATA)
                            + " since it is already on level 0");
                }
            }
        }

        if (!foundSSTable) {
            out.println("Found no sstables, did you give the correct keyspace/columnfamily?");
        }
    } catch (Throwable t) {
        JVMStabilityInspector.inspectThrowable(t);
        t.printStackTrace();
        System.exit(1);
    }
    System.exit(0);
}