// 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."); }
// process a statement of the form: describe table <tablename> private void executeDescribeTable(CommonTree ast) throws TException { if (!CliMain.isConnected()) return; // Get table name int childCount = ast.getChildCount(); assert (childCount == 1); String tableName = ast.getChild(0).getText(); if (tableName == null) { css_.out.println("Keyspace argument required"); return; } // Describe and display Map<String, Map<String, String>> columnFamiliesMap; try { columnFamiliesMap = thriftClient_.describe_keyspace(tableName); for (String columnFamilyName : columnFamiliesMap.keySet()) { Map<String, String> columnMap = columnFamiliesMap.get(columnFamilyName); String desc = columnMap.get("Desc"); String columnFamilyType = columnMap.get("Type"); String sort = columnMap.get("CompareWith"); String flushperiod = columnMap.get("FlushPeriodInMinutes"); css_.out.println(desc); css_.out.println("Column Family Type: " + columnFamilyType); css_.out.println("Column Sorted By: " + sort); css_.out.println("flush period: " + flushperiod + " minutes"); css_.out.println("------"); } } catch (NotFoundException e) { css_.out.println("Keyspace " + tableName + " could not be found."); } }
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")); }
// process a statement of the form: connect hostname/port private void executeConnect(CommonTree ast) { int portNumber = Integer.parseInt(ast.getChild(1).getText()); Tree idList = ast.getChild(0); StringBuilder hostName = new StringBuilder(); int idCount = idList.getChildCount(); for (int idx = 0; idx < idCount; idx++) { hostName.append(idList.getChild(idx).getText()); } // disconnect current connection, if any. // This is a no-op, if you aren't currently connected. CliMain.disconnect(); // now, connect to the newly specified host name and port css_.hostName = hostName.toString(); css_.thriftPort = portNumber; CliMain.connect(css_.hostName, css_.thriftPort); }
// 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); }