List of usage examples for org.apache.cassandra.db Keyspace getColumnFamilyStore
public ColumnFamilyStore getColumnFamilyStore(TableId id)
From source file:com.cloudian.support.RowFinder.java
License:Apache License
private void find() throws IOException { // get ColumnFamilyStore instance System.out.println("Opening keyspace " + ksName + " ..."); Keyspace keyspace = Keyspace.open(ksName); System.out.println("Opened keyspace " + ksName); System.out.println("Getting column family " + cfName + " ..."); ColumnFamilyStore cfStore = keyspace.getColumnFamilyStore(cfName); Collection<SSTableReader> ssTables = cfStore.getSSTables(); System.out.println("Got column family " + cfName); ByteBuffer buff = ByteBufferUtil.bytes(this.rowKey); IPartitioner<?> partitioner = cfStore.partitioner; System.out.println(this.rowKey + " is included in the following files"); System.out.println("=============================================================================="); System.out.println("FINE_NAME, COLUMN_INFO, CONTIGUOUS_TOMBSTONED_COLUMNS(over " + threshold + ")"); System.out.println("=============================================================================="); for (SSTableReader reader : ssTables) { if (reader.getBloomFilter().isPresent(buff)) { // seek to row key RandomAccessReader dfile = reader.openDataReader(); RowIndexEntry entry = reader.getPosition(partitioner.decorateKey(buff), SSTableReader.Operator.EQ); if (entry == null) continue; dfile.seek(entry.position);/*from www . j a v a 2 s . co m*/ // read some ByteBufferUtil.readWithShortLength(dfile); if (reader.descriptor.version.hasRowSizeAndColumnCount) dfile.readLong(); DeletionInfo deletionInfo = new DeletionInfo(DeletionTime.serializer.deserialize(dfile)); int columnCount = reader.descriptor.version.hasRowSizeAndColumnCount ? dfile.readInt() : Integer.MAX_VALUE; // get iterator Iterator<OnDiskAtom> atomIterator = reader.metadata.getOnDiskIterator(dfile, columnCount, reader.descriptor.version); // iterate System.out.print(new File(reader.getFilename()).getName()); boolean isContiguous = false; int contiguousTombstonedColumns = 0; String contiguousTombstonedColumnsStart = null; int live = 0; int deleted = 0; int rangeTombstone = 0; StringBuffer sb = new StringBuffer(); while (atomIterator.hasNext()) { OnDiskAtom atom = atomIterator.next(); if (atom instanceof Column) { if (atom instanceof DeletedColumn) { deleted++; if (!isContiguous) { isContiguous = true; contiguousTombstonedColumnsStart = ByteBufferUtil.string(atom.name()); } contiguousTombstonedColumns++; } else { live++; if (isContiguous) { // print if (contiguousTombstonedColumns >= this.threshold) { sb.append(", [" + contiguousTombstonedColumnsStart + "|" + contiguousTombstonedColumns + "]"); } // reset contiguousTombstonedColumns = 0; contiguousTombstonedColumnsStart = null; isContiguous = false; } } } else if (atom instanceof RangeTombstone) { rangeTombstone++; int localDeletionTime = atom.getLocalDeletionTime(); ByteBuffer min = ((RangeTombstone) atom).min; ByteBuffer max = ((RangeTombstone) atom).max; String minString = ByteBufferUtil.string(min); String maxString = ByteBufferUtil.string(max); sb.append(", [" + minString + ", " + maxString + "(" + localDeletionTime + ")]"); } // if it ends with finished columns if (contiguousTombstonedColumns >= this.threshold) { sb.append( ", [" + contiguousTombstonedColumnsStart + "|" + contiguousTombstonedColumns + "]"); } } System.out.print(", (live, deleted, range tombstone)=(" + live + ", " + deleted + ", " + rangeTombstone + ")"); System.out.println(sb.toString()); } } }
From source file:com.cloudian.support.SSTableGarbageChecker.java
License:Apache License
/** * @param args//from w w w . ja va 2 s.c o m */ public static void main(String[] args) { String usage = "Usage: checksstablegarbage (-t|-g) SSTableFile1 (SSTableFile2 ... SSTableFileN)"; String showOption = (args.length > 0 && args[0].startsWith("-")) ? args[0] : null; if (args.length == 0 || args.length == 1 && showOption != null) { System.err.println("At lest 1 SSTableFile needs to be provided"); System.err.println(usage); System.exit(1); } if (showOption != null) { if (!showOption.equals(SHOW_OPTION_TOMBSTONED) && !showOption.equals(SHOW_OPTION_GCABLE)) { System.err.println(showOption + " is not a valid option"); System.err.println(usage); System.exit(1); } } System.out.println("Loading schema ..."); DatabaseDescriptor.loadSchemas(); System.out.println("Loaded schema"); if (Schema.instance.getCFMetaData(ksName, cfName) == null) { System.err.println(cfName + " in " + ksName + " does not exist"); System.exit(1); } File[] ssTableFiles = (showOption != null) ? new File[args.length - 1] : new File[args.length]; ssTableFilePaths = new String[ssTableFiles.length]; for (int i = (showOption != null) ? 1 : 0; i < args.length; i++) { int j = (showOption != null) ? i - 1 : i; ssTableFiles[j] = new File(args[i]); ssTableFilePaths[j] = ssTableFiles[j].getAbsolutePath(); } Arrays.sort(ssTableFilePaths); descriptors = new Descriptor[ssTableFiles.length]; Collection<SSTableReader> ssTables = null; StringBuffer message = new StringBuffer(); for (int i = ssTableFiles.length - 1; i >= 0; i--) { System.out.println("Opening " + ssTableFiles[i].getName() + "..."); if (!ssTableFiles[i].exists()) { System.err.println(args[i] + " does not exist"); System.exit(1); } else { descriptors[i] = Descriptor.fromFilename(ssTableFiles[i].getAbsolutePath()); if (i == ssTableFiles.length - 1) { ksName = descriptors[i].ksname; cfName = descriptors[i].cfname; // get ColumnFamilyStore instance System.out.println("Opening keyspace " + ksName + " ..."); Keyspace keyspace = Keyspace.open(ksName); System.out.println("Opened keyspace " + ksName + " ..."); System.out.println("Getting column family " + cfName + " ..."); cfStore = keyspace.getColumnFamilyStore(cfName); ssTables = cfStore.getSSTables(); System.out.println("Got column family " + cfName + " ..."); // get gc grace seconds gcGraceSeconds = cfStore.metadata.getGcGraceSeconds(); gcBefore = (int) (System.currentTimeMillis() / 1000) - gcGraceSeconds; message.append("[KS, CF, gc_grace(gcBefore, now)] = [" + ksName + DELIMITER + cfName + DELIMITER + gcGraceSeconds + "(" + gcBefore + ", " + System.currentTimeMillis() + ")]"); } else { boolean theSameCf = cfName.equals(descriptors[i].cfname); if (!theSameCf) { System.err.println("All SSTableFiles have to belong to the same column family"); System.err.println(args[i] + " does not a member of " + cfName); System.exit(1); } } SSTableReader found = null; for (SSTableReader reader : ssTables) { if (reader.getFilename().equals(ssTableFiles[i].getAbsolutePath())) { found = reader; break; } } if (found != null) { ssTableReaders.add(found); } else { System.err.println("Can not find SSTableReader for " + ssTableFiles[i].getAbsolutePath()); if (!ssTableFiles[i].exists()) { System.err.println(ssTableFiles[i].getName() + " does not exist anymore."); } System.err.println("Loaded SSTable files are the followings:"); for (SSTableReader reader : ssTables) { System.err.println(reader.getFilename()); } System.exit(1); } } System.out.println("Opened " + ssTableFiles[i].getName()); } System.out.println(message.toString()); try { SSTableGarbageChecker.checkCompacted(showOption); } catch (IOException e) { System.err.println("Check failed due to " + e.getMessage()); e.printStackTrace(); System.exit(1); } // Successful System.exit(0); }
From source file:com.wenyu.cassandra.tools.SSTableLevelToZero.java
License:Apache License
/** * @param args a list of sstables whose metadata we are changing *//*from w ww . j a va 2 s .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); }
From source file:edu.dprg.morphous.MoveSSTableTest.java
License:Apache License
@Test public void testMoveSSTablesBetweenDifferentColumnFamilies() throws Exception { final String ks1 = "testkeyspace_move_sstables"; final String cfName1 = "table1_copy"; final String cfName2 = "table1"; List<KSMetaData> schema = new ArrayList<KSMetaData>(); // A whole bucket of shorthand Class<? extends AbstractReplicationStrategy> simple = SimpleStrategy.class; Map<String, String> opts_rf1 = KSMetaData.optsWithRF(1); schema.add(KSMetaData.testMetadata(ks1, simple, opts_rf1, new CFMetaData(ks1, cfName1, ColumnFamilyType.Standard, BytesType.instance, null), new CFMetaData(ks1, cfName2, ColumnFamilyType.Standard, BytesType.instance, null))); for (KSMetaData ksm : schema) { MigrationManager.announceNewKeyspace(ksm); }// www . jav a2s.co m Keyspace keyspace = Keyspace.open(ks1); ColumnFamilyStore cfs1 = keyspace.getColumnFamilyStore(cfName1); cfs1.truncateBlocking(); ColumnFamilyStore cfs2 = keyspace.getColumnFamilyStore(cfName2); cfs1.truncateBlocking(); cfs2.truncateBlocking(); for (int i = 1; i <= 500; i++) { ByteBuffer key = ByteBufferUtil.bytes("key-cf1-" + i); RowMutation rm = new RowMutation(ks1, key); rm.add(cfName1, ByteBufferUtil.bytes("Column1"), ByteBufferUtil.bytes("cf1-value" + i), 0); rm.apply(); } cfs1.forceBlockingFlush(); for (int i = 1; i <= 100; i++) { ByteBuffer key = ByteBufferUtil.bytes("key-cf2-" + i); RowMutation rm = new RowMutation(ks1, key); rm.add(cfName2, ByteBufferUtil.bytes("Column1"), ByteBufferUtil.bytes("cf2-value" + i), 0); rm.apply(); } cfs2.forceBlockingFlush(); SlicePredicate sp = new SlicePredicate(); sp.setColumn_names(Arrays.asList(ByteBufferUtil.bytes("Column1"))); List<Row> rows1 = cfs1.getRangeSlice(Util.range("", ""), null, ThriftValidation.asIFilter(sp, cfs1.metadata, null), 1000, System.currentTimeMillis(), true, false); assertEquals(500, rows1.size()); List<Row> rows2 = cfs2.getRangeSlice(Util.range("", ""), null, ThriftValidation.asIFilter(sp, cfs2.metadata, null), 1000, System.currentTimeMillis(), true, false); assertEquals(100, rows2.size()); ColumnFamily cf = cfs1.getColumnFamily( QueryFilter.getIdentityFilter(Util.dk("key-cf1-1"), cfName1, System.currentTimeMillis())); assertNotNull(cf); new AtomicSwitchMorphousTaskHandler().swapSSTablesBetweenCfs(cfs1, cfs2); cfs1.reload(); rows1 = cfs1.getRangeSlice(Util.range("", ""), null, ThriftValidation.asIFilter(sp, cfs1.metadata, null), 1000, System.currentTimeMillis(), true, false); assertEquals(100, rows1.size()); cfs2.reload(); rows2 = cfs2.getRangeSlice(Util.range("", ""), null, ThriftValidation.asIFilter(sp, cfs2.metadata, null), 1000, System.currentTimeMillis(), true, false); assertEquals(500, rows2.size()); }
From source file:edu.dprg.morphous.MoveSSTableTest.java
License:Apache License
@Test public void testMigrateColumnFamilyDefinitionToUseNewPartitonKey() throws Exception { String ksName = "testkeyspace_migrate_cf"; String[] cfName = { "cf0", "cf1" }; edu.uiuc.dprg.morphous.Util.executeCql3Statement("CREATE KEYSPACE " + ksName + " WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};"); edu.uiuc.dprg.morphous.Util.executeCql3Statement( "CREATE TABLE " + ksName + "." + cfName[0] + " ( col0 varchar PRIMARY KEY, col1 varchar);"); edu.uiuc.dprg.morphous.Util.executeCql3Statement( "CREATE TABLE " + ksName + "." + cfName[1] + " ( col0 varchar, col1 varchar PRIMARY KEY);"); Keyspace ks = Keyspace.open(ksName); ColumnFamilyStore cfs0 = ks.getColumnFamilyStore(cfName[0]); ColumnFamilyStore cfs1 = ks.getColumnFamilyStore(cfName[1]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 100; j++) { edu.uiuc.dprg.morphous.Util.executeCql3Statement(String.format("INSERT INTO " + ksName + "." + cfName[i] + " (col0, col1) VALUES ('cf%d-col0-%03d', 'cf%d-col1-%03d');", i, j, i, j)); }//from w w w . j a va 2s . c o m } CqlResult selectCf0 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[0] + ";"); assertEquals(100, selectCf0.rows.size()); CqlResult selectCf1 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[1] + ";"); assertEquals(100, selectCf1.rows.size()); // Flush Memtables out to SSTables cfs0.forceBlockingFlush(); cfs1.forceBlockingFlush(); for (int i = 0; i < 100; i++) { String query = "SELECT * FROM " + ksName + "." + cfName[0] + String.format(" WHERE col0 = 'cf0-col0-%03d';", i); logger.info("Executing query {}", query); selectCf0 = edu.uiuc.dprg.morphous.Util.executeCql3Statement(query); assertEquals(1, selectCf0.rows.size()); } AtomicSwitchMorphousTaskHandler handler = new AtomicSwitchMorphousTaskHandler(); handler.swapSSTablesBetweenCfs(cfs0, cfs1); Morphous.instance().migrateColumnFamilyDefinitionToUseNewPartitonKey(ksName, cfName[1], "col0"); Morphous.instance().migrateColumnFamilyDefinitionToUseNewPartitonKey(ksName, cfName[0], "col1"); cfs0.reload(); cfs1.reload(); selectCf0 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[0] + ";"); assertEquals(100, selectCf0.rows.size()); selectCf1 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[1] + ";"); assertEquals(100, selectCf1.rows.size()); List<CqlRow> rows = edu.uiuc.dprg.morphous.Util.executeCql3Statement( "SELECT * FROM system.schema_columns where keyspace_name = '" + ksName + "';").rows; for (int i = 0; i < 100; i++) { String query = "SELECT * FROM " + ksName + "." + cfName[0] + String.format(" WHERE col1 = 'cf1-col1-%03d';", i); logger.info("Executing query {}", query); selectCf1 = edu.uiuc.dprg.morphous.Util.executeCql3Statement(query); assertEquals(1, selectCf1.rows.size()); } for (int i = 0; i < 100; i++) { String query = "SELECT * FROM " + ksName + "." + cfName[1] + String.format(" WHERE col0 = 'cf0-col0-%03d';", i); logger.info("Executing query {}", query); selectCf1 = edu.uiuc.dprg.morphous.Util.executeCql3Statement(query); assertEquals(1, selectCf1.rows.size()); } }
From source file:edu.dprg.morphous.MoveSSTableTest.java
License:Apache License
@Test public void testMigrateColumnFamilyDefinitionToUseNewPartitonKey2() throws Exception { String ksName = "testkeyspace_migrate_cf_2"; String[] cfName = { "cf0", "cf1" }; edu.uiuc.dprg.morphous.Util.executeCql3Statement("CREATE KEYSPACE " + ksName + " WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};"); edu.uiuc.dprg.morphous.Util.executeCql3Statement( "CREATE TABLE " + ksName + "." + cfName[0] + " ( col0 int PRIMARY KEY, col1 int, col2 varchar);"); edu.uiuc.dprg.morphous.Util.executeCql3Statement( "CREATE TABLE " + ksName + "." + cfName[1] + " ( col0 int, col1 int PRIMARY KEY, col2 varchar);"); Keyspace ks = Keyspace.open(ksName); ColumnFamilyStore cfs0 = ks.getColumnFamilyStore(cfName[0]); ColumnFamilyStore cfs1 = ks.getColumnFamilyStore(cfName[1]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 100; j++) { edu.uiuc.dprg.morphous.Util.executeCql3Statement(String.format( "INSERT INTO " + ksName + "." + cfName[i] + " (col0, col1, col2) VALUES (%d, %d, 'cf%d-col2-%03d');", j + i * 1000, 100 + j + i * 1000, i, j)); }/*from ww w.j a v a 2 s . com*/ } CqlResult selectCf0 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[0] + ";"); assertEquals(100, selectCf0.rows.size()); CqlResult selectCf1 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[1] + ";"); assertEquals(100, selectCf1.rows.size()); // Flush Memtables out to SSTables cfs0.forceBlockingFlush(); cfs1.forceBlockingFlush(); CqlResult originalResult; for (int i = 0; i < 100; i++) { String query = "SELECT * FROM " + ksName + "." + cfName[0] + String.format(" WHERE col0 = %d;", i); logger.info("Executing query {}", query); originalResult = edu.uiuc.dprg.morphous.Util.executeCql3Statement(query); assertEquals(1, originalResult.rows.size()); } originalResult = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[0] + ";"); AtomicSwitchMorphousTaskHandler handler = new AtomicSwitchMorphousTaskHandler(); handler.swapSSTablesBetweenCfs(cfs0, cfs1); Morphous.instance().migrateColumnFamilyDefinitionToUseNewPartitonKey(ksName, cfName[1], "col0"); Morphous.instance().migrateColumnFamilyDefinitionToUseNewPartitonKey(ksName, cfName[0], "col1"); cfs0.reload(); cfs1.reload(); selectCf0 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[0] + ";"); assertEquals(100, selectCf0.rows.size()); selectCf1 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[1] + ";"); assertEquals(100, selectCf1.rows.size()); List<CqlRow> rows = edu.uiuc.dprg.morphous.Util.executeCql3Statement( "SELECT * FROM system.schema_columns where keyspace_name = '" + ksName + "';").rows; for (int i = 0; i < 100; i++) { String query = "SELECT * FROM " + ksName + "." + cfName[0] + String.format(" WHERE col1 = %d;", i + 100 + 1000); logger.info("Executing query {}", query); selectCf1 = edu.uiuc.dprg.morphous.Util.executeCql3Statement(query); assertEquals(1, selectCf1.rows.size()); } for (int i = 0; i < 100; i++) { String query = "SELECT * FROM " + ksName + "." + cfName[1] + String.format(" WHERE col0 = %d;", i); logger.info("Executing query {}", query); selectCf1 = edu.uiuc.dprg.morphous.Util.executeCql3Statement(query); assertEquals(1, selectCf1.rows.size()); } }
From source file:edu.dprg.morphous.MoveSSTableTest.java
License:Apache License
@Test public void testCreateTempTable() { String ksName = "testkeyspace_create_temp_table"; String[] cfName = { "cf0", "cf1" }; edu.uiuc.dprg.morphous.Util.executeCql3Statement("CREATE KEYSPACE " + ksName + " WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};"); edu.uiuc.dprg.morphous.Util.executeCql3Statement( "CREATE TABLE " + ksName + "." + cfName[0] + " ( col0 varchar PRIMARY KEY, col1 varchar);"); Keyspace ks = Keyspace.open(ksName); ColumnFamilyStore cfs0 = ks.getColumnFamilyStore(cfName[0]); CFMetaData newCfm = Morphous.instance().createNewCFMetaDataFromOldCFMetaDataWithNewCFNameAndNewPartitonKey( cfs0.metadata, cfName[1], "col1"); Morphous.instance().createNewColumnFamilyWithCFMetaData(newCfm); ColumnFamilyStore cfs1 = ks.getColumnFamilyStore(cfName[1]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 100; j++) { edu.uiuc.dprg.morphous.Util.executeCql3Statement(String.format("INSERT INTO " + ksName + "." + cfName[i] + " (col0, col1) VALUES ('cf%d-col0-%03d', 'cf%d-col1-%03d');", i, j, i, j)); }/*from ww w. j av a2s . com*/ } CqlResult selectCf0 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[0] + ";"); assertEquals(100, selectCf0.rows.size()); CqlResult selectCf1 = edu.uiuc.dprg.morphous.Util .executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[1] + ";"); assertEquals(100, selectCf1.rows.size()); for (int j = 0; j < 100; j++) { CqlResult selectCf = edu.uiuc.dprg.morphous.Util.executeCql3Statement("SELECT * FROM " + ksName + "." + cfName[1] + " WHERE col1 = '" + String.format("cf1-col1-%03d", j) + "';"); assertEquals(1, selectCf.rows.size()); } }