Пример #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
  // Execute SET statement
  private void executeSet(CommonTree ast)
      throws TException, InvalidRequestException, UnavailableException, TimedOutException,
          UnsupportedEncodingException {
    if (!CliMain.isConnected()) return;

    assert (ast.getChildCount() == 2) : "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);
    String value = CliUtils.unescapeSQLString(ast.getChild(1).getText());

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

    // table.cf['key']
    if (columnSpecCnt == 0) {
      css_.err.println("No column name specified, (type 'help' or '?' for help on syntax).");
      return;
    }
    // table.cf['key']['column'] = 'value'
    else if (columnSpecCnt == 1) {
      // get the column name
      columnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
    }
    // table.cf['key']['super_column']['column'] = 'value'
    else {
      assert (columnSpecCnt == 2) : "serious parsing error (this is a bug).";

      // get the super column and column names
      superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      columnName = CliCompiler.getColumn(columnFamilySpec, 1).getBytes("UTF-8");
    }

    // do the insert
    thriftClient_.insert(
        tableName,
        key,
        createColumnPath(columnFamily, superColumnName, columnName),
        value.getBytes(),
        timestampMicros(),
        ConsistencyLevel.ONE);

    css_.out.println("Value inserted.");
  }