// Execute a CLI Statement public void executeCLIStmt(String stmt) throws TException, NotFoundException, InvalidRequestException, UnavailableException, TimedOutException, IllegalAccessException, ClassNotFoundException, InstantiationException { CommonTree ast = null; ast = CliCompiler.compileQuery(stmt); try { switch (ast.getType()) { case CliParser.NODE_EXIT: cleanupAndExit(); break; case CliParser.NODE_THRIFT_GET: executeGet(ast); break; case CliParser.NODE_HELP: printCmdHelp(); break; case CliParser.NODE_THRIFT_SET: executeSet(ast); break; case CliParser.NODE_THRIFT_DEL: executeDelete(ast); break; case CliParser.NODE_THRIFT_COUNT: executeCount(ast); break; case CliParser.NODE_SHOW_CLUSTER_NAME: executeShowProperty(ast, "cluster name"); break; case CliParser.NODE_SHOW_CONFIG_FILE: executeShowProperty(ast, "config file"); break; case CliParser.NODE_SHOW_VERSION: executeShowProperty(ast, "version"); break; case CliParser.NODE_SHOW_TABLES: executeShowTables(ast); break; case CliParser.NODE_DESCRIBE_TABLE: executeDescribeTable(ast); break; case CliParser.NODE_CONNECT: executeConnect(ast); break; case CliParser.NODE_NO_OP: // comment lines come here; they are treated as no ops. break; default: css_.err.println("Invalid Statement (Type: " + ast.getType() + ")"); break; } } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unable to encode string as UTF-8", e); } }
private void executeCount(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); ColumnParent colParent; if (columnSpecCnt == 0) { colParent = createColumnParent(columnFamily, null); } else { assert (columnSpecCnt == 1); colParent = createColumnParent( columnFamily, CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8")); } int count = thriftClient_.get_count(tableName, key, colParent, ConsistencyLevel.ONE); css_.out.printf("%d columns\n", count); }
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")); }
// 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."); }
// 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); }