Пример #1
0
  public static void main(String[] args)
      throws InvalidRequestException, NotFoundException, UnavailableException, TimedOutException,
          TException, UnsupportedEncodingException {
    TTransport tr = new TSocket("192.168.10.2", 9160);
    TProtocol proto = new TBinaryProtocol(tr);

    Cassandra.Client client = new Cassandra.Client(proto);
    tr.open();

    String keyspace = "Keyspace1";
    String cf = "Standard2";
    String key = "studentA";
    // Insert
    long timestamp = System.currentTimeMillis();
    ColumnPath path = new ColumnPath(cf);
    path.setColumn("age".getBytes("UTF-8"));
    client.insert(keyspace, key, path, "18".getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE);

    path.setColumn("height".getBytes("UTF-8"));
    client.insert(keyspace, key, path, "172cm".getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE);

    // Read
    path.setColumn("height".getBytes("UTF-8"));
    ColumnOrSuperColumn cc = client.get(keyspace, key, path, ConsistencyLevel.ONE);
    Column c = cc.getColumn();

    String v = new String(c.value, "UTF-8");

    System.out.println("Read studentA height:" + v);

    tr.close();
  }
Пример #2
0
 @Override
 public InternalCacheEntry load(Object key) throws CacheLoaderException {
   String hashKey = hashKey(key);
   Cassandra.Client cassandraClient = null;
   try {
     cassandraClient = dataSource.getConnection();
     ColumnOrSuperColumn column =
         cassandraClient.get(ByteBufferUtil.bytes(hashKey), entryColumnPath, readConsistencyLevel);
     InternalCacheEntry ice = unmarshall(column.getColumn().getValue(), key);
     if (ice != null && ice.isExpired()) {
       remove(key);
       return null;
     }
     return ice;
   } catch (NotFoundException nfe) {
     log.debugf("Key '%s' not found", hashKey);
     return null;
   } catch (Exception e) {
     throw new CacheLoaderException(e);
   } finally {
     dataSource.releaseConnection(cassandraClient);
   }
 }
Пример #3
0
  // Execute GET statement
  private void executeGet(CommonTree ast)
      throws TException, NotFoundException, InvalidRequestException, UnavailableException,
          TimedOutException, UnsupportedEncodingException, IllegalAccessException,
          InstantiationException, ClassNotFoundException {

    if (!CliMain.isConnected()) return;

    // This will never happen unless the grammar is broken
    assert (ast.getChildCount() == 1) : "serious parsing error (this is a bug).";

    CommonTree columnFamilySpec = (CommonTree) ast.getChild(0);
    if (!(columnFamilySpec.getType() == CliParser.NODE_COLUMN_ACCESS)) return;

    String tableName = CliCompiler.getTableName(columnFamilySpec);
    String key = CliCompiler.getKey(columnFamilySpec);
    String columnFamily = CliCompiler.getColumnFamily(columnFamilySpec);
    int columnSpecCnt = CliCompiler.numColumnSpecifiers(columnFamilySpec);

    if (!(getCFMetaData(tableName).containsKey(columnFamily))) {
      css_.out.println("No such column family: " + columnFamily);
      return;
    }

    boolean isSuper =
        getCFMetaData(tableName).get(columnFamily).get("Type").equals("Super") ? true : false;

    byte[] superColumnName = null;
    byte[] columnName = null;

    // table.cf['key'] -- row slice
    if (columnSpecCnt == 0) {
      doSlice(tableName, key, columnFamily, superColumnName);
      return;
    }

    // table.cf['key']['column'] -- slice of a super, or get of a standard
    if (columnSpecCnt == 1) {
      if (isSuper) {
        superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
        doSlice(tableName, key, columnFamily, superColumnName);
        return;
      } else {
        columnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      }
    }
    // table.cf['key']['column']['column'] -- get of a sub-column
    else if (columnSpecCnt == 2) {
      superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      columnName = CliCompiler.getColumn(columnFamilySpec, 1).getBytes("UTF-8");
    }
    // The parser groks an arbitrary number of these so it is possible to get here.
    else {
      css_.out.println("Invalid row, super column, or column specification.");
      return;
    }

    // Perform a get(), print out the results.
    ColumnPath path = createColumnPath(columnFamily, superColumnName, columnName);
    Column column = thriftClient_.get(tableName, key, path, ConsistencyLevel.ONE).column;
    css_.out.printf(
        "=> (column=%s, value=%s, timestamp=%d)\n",
        formatColumnName(tableName, columnFamily, column),
        new String(column.value, "UTF-8"),
        column.timestamp);
  }