Creating a simple Utils class

This simple Utils class is more of a demonstrator than a widely-reusable class. It’s genesis was the result of working towards a Thrift API loader for a Cassandra evaluation implementation. You can read bout that, here: Cassandra – A Use case examined (IP data)

Here is the source code:

/*
------------------------------------------------------------------------
 $Id: Util.java,v 1.1 2012/03/14 16:16:49 ddemartini Exp $
 $Revision: 1.1 $
 $Date: 2012/03/14 16:16:49 $
 $Name:  $
------------------------------------------------------------------------
 Utilities
------------------------------------------------------------------------
*/

package base;
import java.io.UnsupportedEncodingException;
import java.util.*;
import org.apache.cassandra.thrift.*;

public class Util {
      
  /* Returns a list of keyspaces, good for keyspace lookup */
  public static List listKeySpaces(Cassandra.Client c) throws Exception {
    List results = new ArrayList();
    for (KsDef k : c.describe_keyspaces()) {
      results.add(k.getName());
    }
    return results;
  } /* listKeySpaces */

  /* create KsDef CfDef ready for use with system_add_keyspace() */
  public static KsDef createSimpleKSandCF(String ksname, String cfname, int replication) {
    KsDef newKs = new KsDef();
    newKs.setStrategy_class("org.apache.cassandra.locator.SimpleStrategy");
    newKs.setName(ksname);
    newKs.setReplication_factor(replication);
    CfDef cfDef = new CfDef();
    cfDef.setKeyspace(ksname);
    cfDef.setName(cfname);
    newKs.addToCf_defs(cfDef);
    return newKs;
  } /* createSimpleKSandCF */ 
  
  /* retrieve environment or -D options passed by implementer */
  public static String envOrProp(String name) {
    if (System.getenv(name) != null) {
      return System.getenv(name);
    }
    else if (System.getProperty(name) != null) {
      return System.getProperty(name);
    }
    else {
      return null;
    }
  } /* envOrProp */

} /* Util */

1 comment for “Creating a simple Utils class

Comments are closed.