@Test public void testAddDropKeyspace() throws Exception { ThriftCluster cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator, user1Credentials); ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("DynKeyspace", "DynCf"); cassandraCluster.addKeyspace( new ThriftKsDef("DynKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef))); String ksid2 = cassandraCluster.dropKeyspace("DynKeyspace"); assertNotNull(ksid2); }
@Test public void testAddDropColumnFamily() throws Exception { ThriftCluster cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator, user1Credentials); ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "DynCf"); cassandraCluster.addColumnFamily(cfDef); String cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf"); assertNotNull(cfid2); }
private Set<CassandraHost> buildRingInfo() { ThriftCluster cluster = (ThriftCluster) HFactory.getCluster(connectionManager.getClusterName()); // ThriftCluster is not exclusively created & cached by HFactory. E.g. Some users instantiate directly via Spring. if(cluster != null) { Set<CassandraHost> ringInfo = new HashSet<CassandraHost>(); for(KeyspaceDefinition keyspaceDefinition: cluster.describeKeyspaces()) { if (!keyspaceDefinition.getName().equals(Keyspace.KEYSPACE_SYSTEM)) { List<TokenRange> tokenRanges = cluster.describeRing(keyspaceDefinition.getName()); for (TokenRange tokenRange : tokenRanges) { for (String host : tokenRange.getRpc_endpoints()) { CassandraHost aHost = new CassandraHost(host, cassandraHostConfigurator.getPort()); if (!ringInfo.contains(aHost) ) { ringInfo.add(aHost); } } } break; } } return ringInfo; } else { return null; } } }
@Test public void testDescribeKeyspaces() throws Exception { ThriftCluster cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator, user1Credentials); List<KeyspaceDefinition> keyspaces = cassandraCluster.describeKeyspaces(); assertEquals(2,keyspaces.size()); }
@Test public void testAddDropColumnFamily() throws Exception { ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "DynCf"); cassandraCluster.addColumnFamily(cfDef); String cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf"); assertNotNull(cfid2); // Let's wait for agreement cassandraCluster.addColumnFamily(cfDef, true); cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf", true); assertNotNull(cfid2); }
@Test public void testEditBasicColumnFamilyDefinition() throws Exception { BasicKeyspaceDefinition ksDef = new BasicKeyspaceDefinition(); ksDef.setName("Keyspace2"); ksDef.setReplicationFactor(1); ksDef.setStrategyClass("SimpleStrategy"); cassandraCluster.addKeyspace(ksDef); BasicColumnFamilyDefinition cfDef = new BasicColumnFamilyDefinition(); cfDef.setName("DynCf2"); cfDef.setKeyspaceName("Keyspace2"); cassandraCluster.addColumnFamily(cfDef); KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace("Keyspace2"); cfDef = new BasicColumnFamilyDefinition(fromCluster.getCfDefs().get(0)); cfDef.setDefaultValidationClass(ComparatorType.LONGTYPE.getClassName()); cassandraCluster.updateColumnFamily(cfDef); String cfid2 = cassandraCluster.dropColumnFamily("Keyspace2", "DynCf2"); assertNotNull(cfid2); } }
@Test public void testEditBasicKeyspaceDefinition() throws Exception { BasicKeyspaceDefinition ksDef = new BasicKeyspaceDefinition(); ksDef.setName("DynKeyspace4"); ksDef.setReplicationFactor(1); ksDef.setStrategyClass("SimpleStrategy"); cassandraCluster.addKeyspace(ksDef); assertNotNull(cassandraCluster.describeKeyspace("DynKeyspace4")); ksDef.setReplicationFactor(2); cassandraCluster.updateKeyspace(ksDef); KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace("DynKeyspace4"); assertEquals(2, fromCluster.getReplicationFactor()); cassandraCluster.dropKeyspace("DynKeyspace4"); }
/** * Method looks in the cache for the cluster by name. If none exists, a new * ThriftCluster instance is created and added to the map of known clusters * * @param clusterName * The cluster name. This is an identifying string for the cluster, * e.g. "production" or "test" etc. Clusters will be created on * demand per each unique clusterName key. * @param cassandraHostConfigurator * @param credentials */ public static Cluster createCluster(String clusterName, CassandraHostConfigurator cassandraHostConfigurator, Map<String, String> credentials) { synchronized (clusters) { Cluster cluster = clusters.get(clusterName); if ( cluster == null ) { cluster = new ThriftCluster(clusterName, cassandraHostConfigurator, credentials); clusters.put(clusterName, cluster); cluster.onStartup(); } return cluster; } }
public Map<String,List<String>> describeSchemaVersions() throws HectorException { Operation<Map<String,List<String>>> op = new Operation<Map<String,List<String>>>(OperationType.META_READ, getCredentials()) { @Override public Map<String,List<String>> execute(Cassandra.Client cassandra) throws HectorException { try { return cassandra.describe_schema_versions(); } catch (Exception e) { throw xtrans.translate(e); } } }; connectionManager.operateWithFailover(op); return op.getResult(); }
@Before public void setup() { cassandraHostConfigurator = new CassandraHostConfigurator("localhost"); connectionManager = Mockito.mock(HConnectionManager.class); cluster = Mockito.mock(ThriftCluster.class); Mockito.when(cluster.getConfigurator()).thenReturn(cassandraHostConfigurator); final KeyspaceDefinition kdef = Mockito.mock(KeyspaceDefinition.class); Mockito.when(kdef.getName()).thenReturn("TestKeyspace"); Mockito.when(cluster.describeKeyspaces()).thenReturn(new LinkedList<KeyspaceDefinition>() {{ add(kdef); }}); }
@Test public void testEditColumnFamily() throws Exception { BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition(); columnFamilyDefinition.setKeyspaceName("DynKeyspace3"); columnFamilyDefinition.setName("DynamicCF"); ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition); KeyspaceDefinition keyspaceDefinition = HFactory.createKeyspaceDefinition("DynKeyspace3", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef)); cassandraCluster.addKeyspace(keyspaceDefinition); KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace("DynKeyspace3"); cfDef = fromCluster.getCfDefs().get(0); columnFamilyDefinition = new BasicColumnFamilyDefinition(cfDef); BasicColumnDefinition columnDefinition = new BasicColumnDefinition(); columnDefinition.setName(StringSerializer.get().toByteBuffer("birthdate")); columnDefinition.setIndexName("birthdate_idx"); columnDefinition.setIndexType(ColumnIndexType.KEYS); columnDefinition.setValidationClass(ComparatorType.LONGTYPE.getClassName()); columnFamilyDefinition.addColumnDefinition(columnDefinition); columnDefinition = new BasicColumnDefinition(); columnDefinition.setName(StringSerializer.get().toByteBuffer("nonindexed_field")); columnDefinition.setValidationClass(ComparatorType.LONGTYPE.getClassName()); columnFamilyDefinition.addColumnDefinition(columnDefinition); cassandraCluster.updateColumnFamily(new ThriftCfDef(columnFamilyDefinition)); fromCluster = cassandraCluster.describeKeyspace("DynKeyspace3"); assertEquals("birthdate",StringSerializer.get().fromByteBuffer(fromCluster.getCfDefs().get(0).getColumnMetadata().get(0).getName())); assertEquals("birthdate_idx",fromCluster.getCfDefs().get(0).getColumnMetadata().get(0).getIndexName()); assertEquals("nonindexed_field",StringSerializer.get().fromByteBuffer(fromCluster.getCfDefs().get(0).getColumnMetadata().get(1).getName())); }
@Test public void testAddEmptyKeyspace() throws Exception { cassandraCluster.addKeyspace(new ThriftKsDef("DynKeyspaceEmpty")); assertNotNull(cassandraCluster.describeKeyspace("DynKeyspaceEmpty")); String ksid2 = cassandraCluster.dropKeyspace("DynKeyspaceEmpty"); assertNotNull(ksid2); }
@Test public void testNoneAdded() { List<TokenRange> tokens = createRange("localhost"); Mockito.when(cluster.describeRing("TestKeyspace")).thenReturn(tokens); Mockito.when(cluster.getName()).thenReturn(TEST_CLUSTER_NAME); Mockito.when(connectionManager.getClusterName()).thenReturn(TEST_CLUSTER_NAME); Mockito.when(connectionManager.getHosts()).thenReturn(Sets.newHashSet(new CassandraHost("localhost",9160))); HFactory.setClusterForTest(cluster); NodeDiscovery q = new NodeDiscovery(cassandraHostConfigurator, connectionManager); Assert.assertEquals(0, q.discoverNodes().size()); }
@Test public void testDescribeKeyspace() throws Exception { ThriftCluster cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator, user1Credentials); KeyspaceDefinition keyspaceDetail = cassandraCluster.describeKeyspace("Keyspace1"); assertNotNull(keyspaceDetail); assertEquals(22, keyspaceDetail.getCfDefs().size()); }
@Override public String addKeyspace(final KeyspaceDefinition ksdef) throws HectorException { return addKeyspace(ksdef, false); }
@Test public void testAddDropKeyspace() throws Exception { ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("DynKeyspace", "DynCf"); cassandraCluster.addKeyspace( new ThriftKsDef("DynKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef))); String ksid2 = cassandraCluster.dropKeyspace("DynKeyspace"); assertNotNull(ksid2); // Now let's wait for schema agreement. cassandraCluster.addKeyspace(new ThriftKsDef("DynKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef)), true); ksid2 = cassandraCluster.dropKeyspace("DynKeyspace", true); assertNotNull(ksid2); }
@Override public void onStartup() { if (getConfigurator().getRunAutoDiscoveryAtStartup()) { connectionManager.doAddNodes(); } }
@Override public String updateKeyspace(final KeyspaceDefinition ksdef) throws HectorException { return updateKeyspace(ksdef, false); }
@Override public String updateColumnFamily(final ColumnFamilyDefinition cfdef) throws HectorException { return updateColumnFamily(cfdef, false); }
@Override public String addColumnFamily(final ColumnFamilyDefinition cfdef) throws HectorException { return addColumnFamily(cfdef, false); }