private void ensureAllPresent() { // Make sure that the keyspace and cf exist with suitable defaults. If // you want different defaults // set them up outside of Rapture log.info(String.format("Ensuring keyspace:%s and cf:%s are present", keySpace, columnFamily)); try { KsDef keyspace = getKeyspace(keySpace); if (keyspace == null) { String cql = "CREATE keyspace " + keySpace + " WITH strategy_class = 'SimpleStrategy' AND strategy_options:replication_factor = '1'"; executeCQL(cql); } client.set_keyspace(keySpace); ensureStandardCF(columnFamily); } catch (TException e) { throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, messageCatalog.getMessage("DbCommsError"), e); } }
public static String getKeySpace(String ksName, List<KsDef> keyspaces) { int matches = 0; String lastMatchedName = ""; for (KsDef ksDef : keyspaces) { if (ksDef.name.equals(ksName)) { return ksName; } else if (ksDef.name.toUpperCase().equals(ksName.toUpperCase())) { lastMatchedName = ksDef.name; matches++; } } if (matches > 1 || matches == 0) throw new RuntimeException("Keyspace '" + ksName + "' not found."); return lastMatchedName; }
/** * creating a keyspace * * @throws Exception */ @Test public void createKeyspace() throws Exception { // keyspace define KsDef kd = new KsDef(); kd.setName("mock"); // kd.setStrategy_class("org.apache.cassandra.locator.SimpleStrategy"); kd.setStrategy_class(SimpleStrategy.class.getName()); // Map<String, String> map = new HashMap<String, String>(); map.put("replication_factor", String.valueOf(1)); kd.setStrategy_options(map); kd.setCf_defs(new ArrayList<CfDef>()); // client.system_add_keyspace(kd);// InvalidRequestException(why:Keyspace // names must be case-insensitively // unique ("mock" conflicts with // "mock")) }
/** * keyspace exist * * @throws Exception */ @Test public void keyspaceExists() throws Exception { String KEYSPACE = "mock"; // boolean result = false; try { KsDef kd = client.describe_keyspace(KEYSPACE);// NotFoundException if (kd != null) { result = true; } } catch (Exception ex) { ex.printStackTrace(); } System.out.println(result);// true }
/** * column family exist * * @throws Exception */ @Test public void columnFamilyExists() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // String COLUMN_FAMILY = "student"; boolean result = false; KsDef kd = client.describe_keyspace(KEYSPACE); for (CfDef entry : kd.getCf_defs()) { if (entry.getName().equals(COLUMN_FAMILY)) { result = true; break; } } // System.out.println(result);// true }
/** * getting a column family * * @param client * @param keyspace * @param columnFamily * @return */ protected static CfDef getColumnFamily(Cassandra.Client client, String keyspace, String columnFamily) { CfDef result = null; // try { KsDef kd = client.describe_keyspace(keyspace); for (CfDef entry : kd.getCf_defs()) { if (entry.getName().equals(columnFamily)) { result = entry; break; } } } catch (Exception ex) { ex.printStackTrace(); } return result; }
/** * modifying a column family to an existing keyspace * * @throws Exception */ @Test public void modifyColumnFamily() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // String COLUMN_FAMILY = "student"; KsDef kd = client.describe_keyspace(KEYSPACE); CfDef cd = null; for (CfDef entry : kd.getCf_defs()) { if (entry.getName().equals(COLUMN_FAMILY)) { cd = entry; break; } } System.out.println(cd.getName()); client.system_update_column_family(cd);// InvalidRequestException(why:student // already exists in keyspace mock) System.out.println("modify column family [" + COLUMN_FAMILY + "]"); }
@Test public void getKeyspace() throws Exception { TTransport ttransport = ctDataSource.getTTransport(); TProtocol tprotocol = new TBinaryProtocol(ttransport); Cassandra.Client client = new Cassandra.Client(tprotocol); // long beg = System.currentTimeMillis(); String KEYSPACE = "system"; // KsDef kd = client.describe_keyspace(KEYSPACE);// NotFoundException ttransport.close(); // long end = System.currentTimeMillis(); System.out.println((end - beg) + " at mills."); // System.out.println(kd); }
/** * Static utility routine that returns a list of column families that exist in * the keyspace encapsulated in the supplied connection * * @param conn the connection to use * @return a list of column families (tables) * @throws Exception if a problem occurs */ public static List<String> getColumnFamilyNames(CassandraConnection conn) throws Exception { KsDef keySpace = conn.describeKeyspace(); List<CfDef> colFams = null; if (keySpace != null) { colFams = keySpace.getCf_defs(); } else { throw new Exception(BaseMessages.getString(PKG, "CassandraColumnMetaData.Error.UnableToGetMetaDataForKeyspace", conn.m_keyspaceName)); } List<String> colFamNames = new ArrayList<String>(); for (CfDef fam : colFams) { colFamNames.add(fam.getName()); } return colFamNames; }
/** * Return true if the given store name currently exists in the given keyspace. This * method can be used with a connection connected to any keyspace. * * @param dbConn Database connection to use. * @param cfName Candidate ColumnFamily name. * @return True if the CF exists in the database. */ public boolean columnFamilyExists(DBConn dbConn, String keyspace, String cfName) { KsDef ksDef = null; try { ksDef = dbConn.getClientSession().describe_keyspace(keyspace); } catch (Exception ex) { throw new RuntimeException("Failed to get keyspace definition for '" + keyspace + "'", ex); } List<CfDef> cfDefList = ksDef.getCf_defs(); for (CfDef cfDef : cfDefList) { if (cfDef.getName().equals(cfName)) { return true; } } return false; }
public org.apache.cassandra.db.migration.avro.KsDef deflate() { org.apache.cassandra.db.migration.avro.KsDef ks = new org.apache.cassandra.db.migration.avro.KsDef(); ks.name = new Utf8(name); ks.strategy_class = new Utf8(strategyClass.getName()); if (strategyOptions != null) { ks.strategy_options = new HashMap<CharSequence, CharSequence>(); for (Map.Entry<String, String> e : strategyOptions.entrySet()) { ks.strategy_options.put(new Utf8(e.getKey()), new Utf8(e.getValue())); } } ks.cf_defs = SerDeUtils.createArray(cfMetaData.size(), org.apache.cassandra.db.migration.avro.CfDef.SCHEMA$); for (CFMetaData cfm : cfMetaData.values()) ks.cf_defs.add(cfm.deflate()); ks.durable_writes = durable_writes; return ks; }
public void ensureStandardCF(String cfName) throws InvalidRequestException, TException, UnavailableException, TimedOutException, SchemaDisagreementException { KsDef keyspace = getKeyspace(keySpace); log.info(String.format("Ensuring standard cf:%s", cfName)); for (CfDef cfdef : keyspace.getCf_defs()) { if (cfdef.getName().equals(cfName)) { return; } } String cql = "CREATE TABLE %s (KEY text PRIMARY KEY) WITH comparator=text"; executeCQL(String.format(cql, cfName)); }
private KsDef getKeyspace(String keys) throws InvalidRequestException, TException { List<KsDef> keyspaces = client.describe_keyspaces(); for (KsDef k : keyspaces) { if (k.getName().equals(keys)) { return k; } } return null; }
@Nullable public KsDef describeKeyspace(String keyspace) { try { return _client.describe_keyspace(keyspace); } catch (Exception e) { return null; // Does not exist } }
public void systemAddKeyspace(KsDef keyspaceDefinition) { try { _client.system_add_keyspace(keyspaceDefinition); } catch (Exception e) { throw Throwables.propagate(e); } }
public void systemUpdateKeyspace(KsDef keyspaceDefinition) { try { _client.system_update_keyspace(keyspaceDefinition); } catch (Exception e) { throw Throwables.propagate(e); } }
public static KSMetaData fromThrift(KsDef ksd, CFMetaData... cfDefs) throws ConfigurationException { Class<? extends AbstractReplicationStrategy> cls = AbstractReplicationStrategy.getClass(ksd.strategy_class); if (cls.equals(LocalStrategy.class)) throw new ConfigurationException("Unable to use given strategy class: LocalStrategy is reserved for internal use."); return new KSMetaData(ksd.name, cls, ksd.strategy_options == null ? Collections.<String, String>emptyMap() : ksd.strategy_options, ksd.durable_writes, Arrays.asList(cfDefs)); }
public KsDef toThrift() { List<CfDef> cfDefs = new ArrayList<>(cfMetaData.size()); for (CFMetaData cfm : cfMetaData().values()) { // Don't expose CF that cannot be correctly handle by thrift; see CASSANDRA-4377 for further details if (cfm.isThriftCompatible()) cfDefs.add(cfm.toThrift()); } KsDef ksdef = new KsDef(name, strategyClass.getName(), cfDefs); ksdef.setStrategy_options(strategyOptions); ksdef.setDurable_writes(durableWrites); return ksdef; }
/** * Returns set of column family names in specified keySpace. * @param keySpace - keyspace definition to get column family names from. * @return Set - column family names */ public static Set<String> getCfNamesByKeySpace(KsDef keySpace) { Set<String> names = new LinkedHashSet<String>(); for (CfDef cfDef : keySpace.getCf_defs()) { names.add(cfDef.getName()); } return names; }
/** * Parse the statement from cli and return KsDef * * @param keyspaceName - name of the keyspace to lookup * @param keyspaces - List of known keyspaces * * @return metadata about keyspace or null */ public static KsDef getKeySpaceDef(String keyspaceName, List<KsDef> keyspaces) { keyspaceName = keyspaceName.toUpperCase(); for (KsDef ksDef : keyspaces) { if (ksDef.name.toUpperCase().equals(keyspaceName)) return ksDef; } return null; }
public KsDef toThrift() { List<CfDef> cfDefs = new ArrayList<CfDef>(cfMetaData.size()); for (CFMetaData cfm : cfMetaData().values()) { // Don't expose CF that cannot be correctly handle by thrift; see CASSANDRA-4377 for further details if (cfm.isThriftCompatible()) cfDefs.add(cfm.toThrift()); } KsDef ksdef = new KsDef(name, strategyClass.getName(), cfDefs); ksdef.setStrategy_options(strategyOptions); ksdef.setDurable_writes(durableWrites); return ksdef; }
/** * getting a keyspace * * @throws Exception */ @Test public void getKeyspace() throws Exception { String KEYSPACE = "mock"; // KsDef kd = client.describe_keyspace(KEYSPACE);// NotFoundException System.out.println(kd);// KsDef(name:mock, // strategy_class:org.apache.cassandra.locator.SimpleStrategy, // strategy_options:{replication_factor=1}, // replication_factor:1, cf_defs:[], // durable_writes:true) assertNotNull(kd); }
/** * list all keyspaces * * @throws Exception */ @Test // 50 times: 982 mills. public void listKeyspaces() throws Exception { int count = 50; // long beg = System.currentTimeMillis(); for (int i = 0; i < count; i++) { TTransport ttransport = createTFramedTransport(); TProtocol tprotocol = createTBinaryProtocol(ttransport); Cassandra.Client client = createClient(tprotocol); System.out.println(client); // List<KsDef> kds = client.describe_keyspaces(); for (KsDef kd : kds) { StringBuilder buffer = new StringBuilder(); buffer.append("["); buffer.append(kd.getName()); buffer.append("] "); List<CfDef> cds = kd.getCf_defs(); int size = cds.size(); for (int j = 0; j < size; j++) { buffer.append(cds.get(j).getName()); if (j < size - 1) { buffer.append(", "); } } // System.out.println(buffer); } ttransport.close(); } long end = System.currentTimeMillis(); System.out.println(count + " times: " + (end - beg) + " mills. "); }
/** * modifying a keyspace * * @throws Exception */ @Test public void modyfyKeyspace() throws Exception { String KEYSPACE = "mock"; // KsDef kd = client.describe_keyspace(KEYSPACE);// NotFoundException System.out.println(kd.getName()); // 有column family 就無法修改 client.system_update_keyspace(kd);// InvalidRequestException(why:Keyspace // update must not contain any // column family definitions.) System.out.println("modify keyspace"); }
/** * list all column familys * * @throws Exception */ @Test public void listColumnFamilys() throws Exception { long beg = System.currentTimeMillis(); // String KEYSPACE = "UIH"; client.set_keyspace(KEYSPACE); // KsDef kd = client.describe_keyspace(KEYSPACE); List<CfDef> result = kd.getCf_defs(); for (CfDef cd : result) { StringBuilder buffer = new StringBuilder(); buffer.append("["); buffer.append(cd.getName()); buffer.append("] "); // List<ColumnDef> cols = cd.getColumn_metadata(); int size = cols.size(); int i = 0; for (ColumnDef column : cols) { buffer.append(ByteHelper.toString(column.getName())); if (i < size - 1) { buffer.append(", "); } i++; } // System.out.println(buffer); } long end = System.currentTimeMillis(); // System.out.println((end - beg) + " at mills."); }
@Test // 50 times: 527 mills. public void listKeyspaces() throws Exception { int count = 50; // long beg = System.currentTimeMillis(); for (int i = 0; i < count; i++) { TTransport ttransport = ctDataSource.getTTransport(); TProtocol tprotocol = new TBinaryProtocol(ttransport); Cassandra.Client client = new Cassandra.Client(tprotocol); // List<KsDef> kds = client.describe_keyspaces(); for (KsDef kd : kds) { StringBuilder buffer = new StringBuilder(); buffer.append("["); buffer.append(kd.getName()); buffer.append("] "); kd.getCf_defs(); List<CfDef> cds = kd.getCf_defs(); for (int j = 0; j < cds.size(); j++) { buffer.append(cds.get(j).getName()); if (j < cds.size() - 1) { buffer.append(", "); } } // System.out.println(buffer); } ttransport.close(); } long end = System.currentTimeMillis(); System.out.println(count + " times: " + (end - beg) + " mills. "); }
@Test public void getKeyspace() throws Exception { CtSession ctSession = ctSessionFactory.openSession(); // long beg = System.currentTimeMillis(); String KEYSPACE = "system"; // KsDef kd = ctSession.describe_keyspace(KEYSPACE);// NotFoundException ctSessionFactory.closeSession(); // long end = System.currentTimeMillis(); System.out.println((end - beg) + " at mills."); // System.out.println(kd); }
@Test // 50 times: 597 mills. public void listKeyspaces() throws Exception { int count = 50; // long beg = System.currentTimeMillis(); for (int i = 0; i < count; i++) { CtSession ctSession = ctSessionFactory.openSession(); System.out.println(ctSession); // List<KsDef> kds = ctSession.describe_keyspaces(); for (KsDef kd : kds) { StringBuilder buffer = new StringBuilder(); buffer.append("["); buffer.append(kd.getName()); buffer.append("] "); List<CfDef> cds = kd.getCf_defs(); int size = cds.size(); for (int j = 0; j < size; j++) { buffer.append(cds.get(j).getName()); if (j < size - 1) { buffer.append(", "); } } // System.out.println(buffer); } ctSessionFactory.closeSession(); } long end = System.currentTimeMillis(); System.out.println(count + " times: " + (end - beg) + " mills. "); }