Пример #1
0
  private void executeDelete(CommonTree ast)
      throws TException, InvalidRequestException, UnavailableException, TimedOutException,
          UnsupportedEncodingException {
    if (!CliMain.isConnected()) return;

    int childCount = ast.getChildCount();
    assert (childCount == 1);

    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);

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

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

      isSuper =
          getCFMetaData(tableName).get(columnFamily).get("Type").equals("Super") ? true : false;
    } catch (NotFoundException nfe) {
      css_.out.printf("No such keyspace: %s\n", tableName);
      return;
    }

    if ((columnSpecCnt < 0) || (columnSpecCnt > 2)) {
      css_.out.println("Invalid row, super column, or column specification.");
      return;
    }

    if (columnSpecCnt == 1) {
      // table.cf['key']['column']
      if (isSuper) superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      else columnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
    } else if (columnSpecCnt == 2) {
      // table.cf['key']['column']['column']
      superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      columnName = CliCompiler.getColumn(columnFamilySpec, 1).getBytes("UTF-8");
    }

    thriftClient_.remove(
        tableName,
        key,
        createColumnPath(columnFamily, superColumnName, columnName),
        timestampMicros(),
        ConsistencyLevel.ONE);
    css_.out.println(String.format("%s removed.", (columnSpecCnt == 0) ? "row" : "column"));
  }
Пример #2
0
  /**
   * Delete a record from the database.
   *
   * @param table The name of the table
   * @param key The record key of the record to delete.
   * @return Zero on success, a non-zero error code on error
   */
  public int delete(String table, String key) {
    if (!_table.equals(table)) {
      try {
        client.set_keyspace(table);
        _table = table;
      } catch (Exception e) {
        e.printStackTrace();
        e.printStackTrace(System.out);
        return Error;
      }
    }

    for (int i = 0; i < OperationRetries; i++) {
      try {
        client.remove(
            ByteBuffer.wrap(key.getBytes("UTF-8")),
            new ColumnPath(column_family),
            System.currentTimeMillis(),
            ConsistencyLevel.ONE);

        if (_debug) {
          System.out.println("Delete key: " + key);
        }

        return Ok;
      } catch (Exception e) {
        errorexception = e;
      }
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
      }
    }
    errorexception.printStackTrace();
    errorexception.printStackTrace(System.out);
    return Error;
  }