예제 #1
0
  public static void createKeyspace(
      Cluster cluster, String name, String strategy, int replicationFactor, List<CfDef> cfDefList) {
    try {
      KsDef ksDef = new KsDef(name, strategy, replicationFactor, cfDefList);
      cluster.addKeyspace(new ThriftKsDef(ksDef));
      return;
    } catch (Throwable e) {
      System.out.println(
          "exception while creating keyspace, "
              + name
              + " - probably already exists : "
              + e.getMessage());
    }

    for (CfDef cfDef : cfDefList) {
      try {
        cluster.addColumnFamily(new ThriftCfDef(cfDef));
      } catch (Throwable e) {
        System.out.println(
            "exception while creating CF, "
                + cfDef.getName()
                + " - probably already exists : "
                + e.getMessage());
      }
    }
  }
 @Override
 public void destroy() {
   logger.info("dropping keyspaces");
   cluster.dropKeyspace(CassandraService.SYSTEM_KEYSPACE);
   cluster.dropKeyspace(CassandraService.STATIC_APPLICATION_KEYSPACE);
   logger.info("keyspaces dropped");
 }
예제 #3
0
 public static Keyspace getKeySpace(Cluster myCluster, boolean drop) {
   KeyspaceDefinition keyspaceDefinition = myCluster.describeKeyspace("deneme");
   if (keyspaceDefinition != null && drop) {
     myCluster.dropKeyspace("deneme");
     addKeySpace(myCluster);
   }
   Keyspace createKeyspace = HFactory.createKeyspace("deneme", myCluster);
   return createKeyspace;
 }
예제 #4
0
  private void init(Cluster cluster) {
    if (cluster.describeKeyspace("SiddhiSnapshots") == null) {
      log.info("Adding  keyspace SiddhiSnapshots");
      cluster.addKeyspace(HFactory.createKeyspaceDefinition("SiddhiSnapshots"));
      keyspace = HFactory.createKeyspace("SiddhiSnapshots", cluster);
      cluster.addColumnFamily(
          HFactory.createColumnFamilyDefinition(keyspace.getKeyspaceName(), COLUMN_FAMILY_NAME));
      cluster.addColumnFamily(
          HFactory.createColumnFamilyDefinition(
              keyspace.getKeyspaceName(), INDEX_COLUMN_FAMILY_NAME));

    } else {
      if (log.isDebugEnabled()) {
        log.debug("keyspace SiddhiSnapshots exists");
      }
      keyspace = HFactory.createKeyspace("SiddhiSnapshots", cluster);
    }
  }
예제 #5
0
  public static Keyspace createKeySpace(Cluster cluster, String keySpace) {

    Keyspace keyspace;

    // Define the keySpace
    KeyspaceDefinition definition = new ThriftKsDef(keySpace);

    KeyspaceDefinition def = cluster.describeKeyspace(keySpace);
    if (def == null) {
      // Adding keySpace to the cluster
      cluster.addKeyspace(definition, true);
    }

    keyspace = HFactory.createKeyspace(keySpace, cluster);
    CassandraConsistencyLevelPolicy policy = new CassandraConsistencyLevelPolicy();
    keyspace.setConsistencyLevelPolicy(policy);
    return keyspace;
  }
예제 #6
0
 public Map<String, Map<String, String>> getKeyspaces() {
   Map<String, Map<String, String>> rval = new HashMap<String, Map<String, String>>();
   for (KeyspaceDefinition k : cluster.describeKeyspaces()) {
     if (!k.getName().equals("system")) {
       rval.put(keyPrefix + '/' + k.getName(), k.getStrategyOptions());
     }
   }
   // TODO add empty check
   return rval;
 }
예제 #7
0
 void setEnvironment() throws InvalidRequestException, TException {
   try {
     myCluster = HFactory.getOrCreateCluster("Test Sample", "localhost:9160");
     ksdef = myCluster.describeKeyspace(keyspaceName);
     if (ksdef == null) addKeyspacetoCassandra();
     keyspace = HFactory.createKeyspace("mostkeyspace", myCluster);
   } catch (Exception e) {
     System.out.print("Unalble to setup environment");
     e.printStackTrace();
   }
 }
예제 #8
0
  void createColumnfamily(String cfname) {

    if (cfname.equals("") || cfname.trim().equals(null)) {
      return;
    } else {
      cfcreate = cfname.toLowerCase();
      if (checkExist(cfcreate) == false) {
        cfdef = HFactory.createColumnFamilyDefinition(keyspaceName, cfcreate);
        myCluster.addColumnFamily(cfdef, true);
      }
    }
  }
예제 #9
0
  /**
   * Create a Column family for cassandra counters in a given Cluster intance
   *
   * @param name ColumnFamily Name
   * @param keySpace KeySpace name
   * @param cluster Cluster instance
   * @throws CassandraDataAccessException In case of an Error accessing database or data error
   */
  public static void createCounterColumnFamily(String name, String keySpace, Cluster cluster)
      throws CassandraDataAccessException {
    KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace);

    if (ksDef == null) {
      throw new CassandraDataAccessException(
          "Can't create Column family, keyspace " + keySpace + " does not exist");
    }
    ColumnFamilyDefinition cfDef =
        HFactory.createColumnFamilyDefinition(keySpace, name, ComparatorType.COUNTERTYPE);
    cfDef.setComparatorType(ComparatorType.UTF8TYPE);
    cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE.getClassName());
    cfDef.setColumnType(ColumnType.STANDARD);

    List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs();
    HashSet<String> cfNames = new HashSet<String>();
    for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) {
      cfNames.add(columnFamilyDefinition.getName());
    }
    if (!cfNames.contains(name)) {
      cluster.addColumnFamily(cfDef, true);
    }
  }
예제 #10
0
  /**
   * Create a Column family in a Given Cluster instance
   *
   * @param name ColumnFamily Name
   * @param keySpace KeySpace name
   * @param cluster Cluster instance
   * @param comparatorType Comparator
   * @throws CassandraDataAccessException In case of an Error accessing database or data error
   */
  public static void createColumnFamily(
      String name, String keySpace, Cluster cluster, String comparatorType)
      throws CassandraDataAccessException {

    KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace);

    if (ksDef == null) {
      throw new CassandraDataAccessException(
          "Can't create Column family, keyspace " + keySpace + " does not exist");
    }

    ColumnFamilyDefinition cfDef =
        new ThriftCfDef(keySpace, /*"Queue"*/ name, ComparatorType.getByClassName(comparatorType));

    List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs();
    HashSet<String> cfNames = new HashSet<String>();
    for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) {
      cfNames.add(columnFamilyDefinition.getName());
    }
    if (!cfNames.contains(name)) {
      cluster.addColumnFamily(cfDef, true);
    }
  }
  private static Credentials getCredentials(Cluster cluster) {
    Map<String, String> credentials = cluster.getCredentials();

    Credentials creds = null;
    for (Map.Entry<String, String> entry : credentials.entrySet()) {
      String userName = entry.getKey();
      String password = entry.getValue();
      String tenantDomain = MultitenantUtils.getTenantDomain(userName);

      creds = new Credentials(userName, password, tenantDomain);
    }

    return creds;
  }
예제 #12
0
  public Map<String, Map<String, String>> getColumnFamilies(String keyspace) {
    KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspace.toString());
    List<ColumnFamilyDefinition> cfDef = keyspaceDef.getCfDefs();
    Map<String, Map<String, String>> rval = new HashMap<String, Map<String, String>>();
    for (ColumnFamilyDefinition cf : cfDef) {
      String cfurl = keyPrefix + '/' + keyspace + '/' + cf.getName();

      String comment = cf.getComment();
      Map<String, String> commentMap = new HashMap<String, String>();
      commentMap.put("comment", comment);
      rval.put(cfurl, commentMap);
    }
    return rval;
  }
예제 #13
0
 void addKeyspacetoCassandra() throws InvalidRequestException, TException {
   try {
     ColumnFamilyDefinition cfDef =
         HFactory.createColumnFamilyDefinition(
             keyspaceName, cfdatapoint, ComparatorType.BYTESTYPE);
     int replicationFactor = 1;
     KeyspaceDefinition newKeyspace =
         HFactory.createKeyspaceDefinition(
             keyspaceName,
             ThriftKsDef.DEF_STRATEGY_CLASS,
             replicationFactor,
             Arrays.asList(cfDef));
     // Add the schema to the cluster.
     myCluster.addKeyspace(newKeyspace);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
예제 #14
0
 public static void addKeySpace(Cluster myCluster) {
   List<ColumnFamilyDefinition> cfDefs = new ArrayList<ColumnFamilyDefinition>();
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "global", ComparatorType.getByClassName("DateType")));
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "campaign1", ComparatorType.getByClassName("DateType")));
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "campaign2", ComparatorType.getByClassName("DateType")));
   cfDefs.add(
       HFactory.createColumnFamilyDefinition(
           "deneme", "campaign3", ComparatorType.getByClassName("DateType")));
   KeyspaceDefinition ksdef =
       HFactory.createKeyspaceDefinition(
           "deneme", "org.apache.cassandra.locator.SimpleStrategy", 1, cfDefs);
   myCluster.addKeyspace(ksdef, true);
 }
예제 #15
0
  public Map getColumnFamily(String keyspace, String cf) throws CharacterCodingException {
    KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspace.toString());
    List<ColumnFamilyDefinition> cfDef = keyspaceDef.getCfDefs();
    Map rval = new HashMap();
    for (ColumnFamilyDefinition cfentry : cfDef) {
      if (cfentry.getName().equals(cf)) {
        // Handle Metadata
        Charset charset = Charset.forName("UTF8"); // TODO add handling for other types
        CharsetDecoder decoder = charset.newDecoder();
        List<Map<String, String>> cfmetaList = new ArrayList<Map<String, String>>(3);
        for (ColumnDefinition colDef : cfentry.getColumnMetadata()) {
          Map<String, String> cfmeta = new HashMap<String, String>();
          String colName = decoder.decode(colDef.getName()).toString();
          cfmeta.put("column_name", colName);
          cfmeta.put("validation_class", colDef.getValidationClass());
          ColumnIndexType idxType = colDef.getIndexType();
          if (idxType != null) {
            cfmeta.put("index_type", idxType.toString());
            cfmeta.put("index_name", colDef.getIndexName());
          }
          cfmetaList.add(cfmeta);
        }

        rval.put("column_metadata", cfmetaList);
        // rval.put("metadata", cfmeta);
        // Handle page link
        Map<String, String> link = new HashMap<String, String>();
        link.put(
            "href",
            keyPrefix + '/' + keyspace + '/' + cf + "/?" + numRowsVar + "=" + defaultNumRows);
        rval.put("browse", link);
        // Handle search template
        // TODO check for existence of indexed column, use one as example col.
        Map<String, String> search = new HashMap<String, String>();
        search.put("href", keyPrefix + '/' + keyspace + '/' + cf + "[email protected]");
        // TODO I mean it, make the above link better.
        rval.put("search", search);
        break;
      }
    }
    return rval;
  }
예제 #16
0
  static {
    if (configuration.getDataStore().equals(DataStore.CASSANDRA)) {
      // TODO make a CassandraFacade class that encapsulates all the cassandra stuff
      dataCluster = HFactory.getOrCreateCluster("data-cluster", tuskCassConf.getCluster());
      LOG.debug("Hector dataCluster=" + dataCluster);

      // This is the keyspace to use for the data we are storing
      KeyspaceDefinition keyspaceDef = dataCluster.describeKeyspace(tuskCassConf.getKeyspace());
      LOG.debug("Hector keyspaceDef=" + keyspaceDef);

      ksp = HFactory.createKeyspace(tuskCassConf.getKeyspace(), dataCluster);
      LOG.debug("Hector keyspace=" + ksp);

      cfTemplate =
          new ThriftColumnFamilyTemplate<String, String>(
              ksp, tuskCassConf.getColumnFamily(), StringSerializer.get(), StringSerializer.get());
      LOG.debug("Hector cfTemplate=" + cfTemplate);
    } else if (configuration.getDataStore().equals(DataStore.HBASE)) {
      // init is in ctor
    }

    //		System.setProperty("java.net.preferIPv4Stack", "true");
    //		ispnService = new InfinispanService();
  }
예제 #17
0
 void dropkeyspace() {
   myCluster.dropColumnFamily(keyspaceName, cfdatapoint);
   myCluster.dropKeyspace(keyspaceName, true);
 }
예제 #18
0
 @Override
 public void cleanup() {
   cluster.getConnectionManager().shutdown();
 }
 /** Closes the connection with cluster */
 public void close() {
   cluster.getConnectionManager().shutdown();
 }