/** * get 讀取1個column * * @throws Exception */ @Test public void get() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // 讀取1個column String COLUMN_FAMILY = "student"; ColumnPath columnPath = new ColumnPath(COLUMN_FAMILY); // String COLUMN = "grad"; columnPath.setColumn(ByteBufferHelper.toByteBuffer(COLUMN)); String ROW_KEY = "Jack"; // key, column_path, consistency_level ColumnOrSuperColumn cos = client.get( ByteBufferHelper.toByteBuffer(ROW_KEY), columnPath, ConsistencyLevel.ONE);// NotFoundException Column column = cos.getColumn(); System.out.println(ROW_KEY + ", " + ByteHelper.toString(column.getName()) + ": " + ByteHelper.toString(column.getValue()) + ", " + column.getTimestamp()); // Jack, grad: 5, 1380932164492000 }
private String getColumnValue(String ks, String cf, String colName, String key, String validator) throws AuthenticationException, AuthorizationException, InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException, IOException { Cassandra.Client client = getClient(); client.set_keyspace(ks); ByteBuffer key_user_id = ByteBufferUtil.bytes(key); long timestamp = System.currentTimeMillis(); ColumnPath cp = new ColumnPath(cf); ColumnParent par = new ColumnParent(cf); cp.column = ByteBufferUtil.bytes(colName); // read ColumnOrSuperColumn got = client.get(key_user_id, cp, ConsistencyLevel.ONE); return parseType(validator).getString(got.getColumn().value); }
public void open() throws Exception { TSocket sock = new TSocket(Config.getCassandraIP(), Config.getCassandraPort()); sock.setTimeout(1000000); transport = new TFramedTransport(sock); client = new Cassandra.Client(new TBinaryProtocol(transport)); transport.open(); client.set_keyspace(Config.getCassandraKeyspace()); cl = ConsistencyLevel.valueOf(Config.getCassandraConsistencyLevel()); writeColumn = new Column(); writeColumnFamily = new ColumnParent(Config.getCassandraColumnFamily()); columnPath = new ColumnPath(Config.getCassandraColumnFamily()); bytesRead = bytesWritten = readLatency = writeLatency = 0; }
private String getColumnValue(String ks, String cf, String colName, String key, String validator) throws AuthenticationException, AuthorizationException, InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException, IOException { Cassandra.Client client = getClient(); client.set_keyspace(ks); ByteBuffer key_user_id = ByteBufferUtil.bytes(key); ColumnPath cp = new ColumnPath(cf); cp.column = ByteBufferUtil.bytes(colName); // read ColumnOrSuperColumn got = client.get(key_user_id, cp, ConsistencyLevel.ONE); return parseType(validator).getString(got.getColumn().value); }
/** * remove * * 當remove後,key還會存在,所以再insert會無法新增 * * @throws Exception */ @Test public void remove() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // String COLUMN_FAMILY = "student"; ColumnPath columnPath = new ColumnPath(COLUMN_FAMILY); // String ROW_KEY = "Jack"; // key, column_path, timestamp, consistency_level client.remove(ByteBufferHelper.toByteBuffer(ROW_KEY), columnPath, System.nanoTime(), ConsistencyLevel.ONE); }
private void insertPOISpringTraining() throws Exception { Map<ByteBuffer, Map<String, List<Mutation>>> outerMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); List<Mutation> columnsToAdd = new ArrayList<Mutation>(); long timestamp = System.nanoTime(); String keyName = "Spring Training"; Column descCol = new Column(bytes("desc")); Column phoneCol = new Column(bytes("phone")); List<Column> cols = new ArrayList<Column>(); cols.add(descCol); cols.add(phoneCol); Map<String, List<Mutation>> innerMap = new HashMap<String, List<Mutation>>(); Mutation columns = new Mutation(); ColumnOrSuperColumn descCosc = new ColumnOrSuperColumn(); SuperColumn sc = new SuperColumn(); sc.name = bytes(CAMBRIA_NAME); sc.columns = cols; descCosc.super_column = sc; columns.setColumn_or_supercolumn(descCosc); columnsToAdd.add(columns); String superCFName = "PointOfInterest"; ColumnPath cp = new ColumnPath(); cp.column_family = superCFName; cp.setSuper_column(CAMBRIA_NAME.getBytes()); cp.setSuper_columnIsSet(true); innerMap.put(superCFName, columnsToAdd); outerMap.put(bytes(keyName), innerMap); client.batch_mutate(outerMap, CL); LOG.debug("Done inserting Spring Training."); }
private void insertPOICentralPark() throws Exception { Map<ByteBuffer, Map<String, List<Mutation>>> outerMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); List<Mutation> columnsToAdd = new ArrayList<Mutation>(); long ts = System.nanoTime(); String keyName = "Central Park"; Column descCol = new Column(bytes("desc")); // no phone column for park List<Column> cols = new ArrayList<Column>(); cols.add(descCol); Map<String, List<Mutation>> innerMap = new HashMap<String, List<Mutation>>(); Mutation columns = new Mutation(); ColumnOrSuperColumn descCosc = new ColumnOrSuperColumn(); SuperColumn waldorfSC = new SuperColumn(); waldorfSC.name = bytes(WALDORF_NAME); waldorfSC.columns = cols; descCosc.super_column = waldorfSC; columns.setColumn_or_supercolumn(descCosc); columnsToAdd.add(columns); String superCFName = "PointOfInterest"; ColumnPath cp = new ColumnPath(); cp.column_family = superCFName; cp.setSuper_column(WALDORF_NAME.getBytes()); cp.setSuper_columnIsSet(true); innerMap.put(superCFName, columnsToAdd); outerMap.put(bytes(keyName), innerMap); client.batch_mutate(outerMap, CL); LOG.debug("Done inserting Central Park."); }
private void commitDeletes(DBTransaction dbTran, long timestamp) { Map<String, Set<ByteBuffer>> rowDeleteMap = CassandraTransaction.getRowDeletionMap(dbTran); if (rowDeleteMap.size() == 0) { return; } // Iterate through all ColumnFamilies for (String colFamName : rowDeleteMap.keySet()) { // Delete each row in this key set. Set<ByteBuffer> rowKeySet = rowDeleteMap.get(colFamName); for (ByteBuffer rowKey : rowKeySet) { removeRow(timestamp, rowKey, new ColumnPath(colFamName)); } } }
@Override public EnumSet<Permission> authorize(ByteBuffer inputKey, AuthenticatedUser user, List<Object> resources, CassandraServer server) { EnumSet<Permission> authorized = Permission.NONE; readPolicyFile(); ConsistencyLevel consistency_level = ConsistencyLevel.findByValue(1); try { // 1. Read patient's name ByteBuffer key1 = ByteBuffer.allocate(1024); key1.put("name".getBytes(ISO_8859_1)); ColumnPath column_path1 = new ColumnPath(); column_path1.setColumn_family("Patient"); column_path1.setColumn("username".getBytes(ISO_8859_1)); server.get(key1, column_path1, consistency_level); ByteBuffer key = ByteBuffer.allocate(1024); key.put("curr_patients".getBytes(ISO_8859_1)); ColumnPath column_path = new ColumnPath(); column_path.setColumn_family("Doctor"); column_path.setColumn("username".getBytes(ISO_8859_1)); server.get(key, column_path, consistency_level); } catch (Exception exp) { exp.printStackTrace(); } return authorized; }
@Test public void testInProcessCassandraServer() throws UnsupportedEncodingException, InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException, AuthenticationException, AuthorizationException { Cassandra.Client client = getClient(); client.set_keyspace("Keyspace1"); String key_user_id = "1"; long timestamp = System.currentTimeMillis(); // insert ColumnParent colParent = new ColumnParent("Standard1"); Column column = new Column(ByteBufferUtil.bytes("name"), ByteBufferUtil.bytes("Ran"), timestamp); client.insert(ByteBufferUtil.bytes(key_user_id), colParent, column, ConsistencyLevel.ONE); // read ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(ByteBufferUtil.bytes("name")); ColumnOrSuperColumn got = client.get(ByteBufferUtil.bytes(key_user_id), cp, ConsistencyLevel.ONE); // assert assertNotNull("Got a null ColumnOrSuperColumn", got); assertEquals("Ran", new String(got.getColumn().getValue(), "utf-8")); }
/** * Query and Check item is exist or not * * return times of item */ public int QueryIpUser(String ip, String name){ String IP_KEY = ip; String NAME_SUPER_KEY = name; String COLUMN_NAME = "times"; String COLUMN_FAMILY = "IpUser"; try{ ColumnPath columnPath = new ColumnPath(); columnPath.column_family = COLUMN_FAMILY; columnPath.super_column = this.cassandraUtil.toByteBuffer(NAME_SUPER_KEY); ColumnOrSuperColumn columnOrSuperColumn = client.get(this.cassandraUtil.toByteBuffer(IP_KEY), columnPath, ConsistencyLevel.ONE); SuperColumn superColumn = columnOrSuperColumn.getSuper_column(); List<Column> columns = superColumn.getColumns(); for(Column col : columns) { String title = new String(col.getName(), "UTF-8") ; if(title.equals(COLUMN_NAME)){ return Integer.parseInt(new String(col.getValue(), "UTF-8")); // get "times" column and return } } }catch(Exception e){ return 0; } return 0; }
/** * Query and Check item is exist or not * * return times of item */ public int QueryUserIp(String ip, String name){ String NAME_KEY = name; String IP_SUPER_KEY = ip; String COLUMN_NAME = "times"; String COLUMN_FAMILY = "UserIp"; try{ ColumnPath columnPath = new ColumnPath(); columnPath.column_family = COLUMN_FAMILY; columnPath.super_column = this.cassandraUtil.toByteBuffer(IP_SUPER_KEY); ColumnOrSuperColumn columnOrSuperColumn = client.get(this.cassandraUtil.toByteBuffer(NAME_KEY), columnPath, ConsistencyLevel.ONE); SuperColumn superColumn = columnOrSuperColumn.getSuper_column(); List<Column> columns = superColumn.getColumns(); for(Column col : columns) { String title = new String(col.getName(), "UTF-8") ; if(title.equals(COLUMN_NAME)){ return Integer.parseInt(new String(col.getValue(), "UTF-8")); // get "times" column and return } } }catch(Exception e){ return 0; } return 0; }
/** * Query and Check BookIp * * return times of column is exist, 0 or not */ public int QueryBookIp(String bookid, String ip) { String BOOKID_KEY = bookid; String IP_SUPER_KEY = ip; String COLUMN_NAME = "times"; String COLUMN_FAMILY = "BookIp"; try { ColumnPath columnPath = new ColumnPath(); columnPath.column_family = COLUMN_FAMILY; columnPath.super_column = this.cassandraUtil.toByteBuffer(IP_SUPER_KEY); ColumnOrSuperColumn columnOrSuperColumn = client.get(this.cassandraUtil.toByteBuffer(BOOKID_KEY), columnPath,ConsistencyLevel.ONE); SuperColumn superColumn = columnOrSuperColumn.getSuper_column(); List<Column> columns = superColumn.getColumns(); for (Column col : columns) { String title = new String(col.getName(), "UTF-8"); if (title.equals(COLUMN_NAME)) { return Integer.parseInt(new String(col.getValue(), "UTF-8")); } } } catch (Exception e) { return 0; } return 0; }
/** * Query and Check BookUser * * return times of column is exist, 0 or not */ public int QueryBookUser(String bookid, String user) { String BOOKID_KEY = bookid; String USER_SUPER_KEY = user; String COLUMN_NAME = "times"; String COLUMN_FAMILY = "BookUser"; try{ ColumnPath columnPath = new ColumnPath(); columnPath.column_family = COLUMN_FAMILY; columnPath.super_column = this.cassandraUtil.toByteBuffer(USER_SUPER_KEY); ColumnOrSuperColumn columnOrSuperColumn = client.get(this.cassandraUtil.toByteBuffer(BOOKID_KEY), columnPath, ConsistencyLevel.ONE); SuperColumn superColumn = columnOrSuperColumn.getSuper_column(); List<Column> columns = superColumn.getColumns(); for(Column col : columns) { String title = new String(col.getName(), "UTF-8"); if(title.equals(COLUMN_NAME)) { return Integer.parseInt(new String(col.getValue(), "UTF-8")); } } }catch(Exception e) { return 0; } return 0; }
/** * Query and Check IpBookPage * * return times of column is exist, 0 or not */ public int QueryIpBookPage(String ip, String book) { String IP_KEY = ip; String BOOKID_SUPER_KEY = book; String COLUMN_FAMILY = "IpBookPage"; String COLUMN_NAME = "times"; try{ ColumnPath columnPath = new ColumnPath(); columnPath.column_family = COLUMN_FAMILY; columnPath.super_column = this.cassandraUtil.toByteBuffer(BOOKID_SUPER_KEY); ColumnOrSuperColumn columnOrSuperColumn = client.get(this.cassandraUtil.toByteBuffer(IP_KEY), columnPath, ConsistencyLevel.ONE); SuperColumn superColumn = columnOrSuperColumn.getSuper_column(); List<Column> columns = superColumn.getColumns(); for(Column col : columns) { String title = new String(col.getName(), "UTF-8"); if(title.equals(COLUMN_NAME)) { return Integer.parseInt(new String(col.getValue(), "UTF-8")); } } }catch(Exception e){ return 0; } return 0; }
/** * Query and Check UserBookPage * * return times of column is exist, 0 or not */ public int QueryUserBookPage(String user, String book) { String USER_KEY = user; String BOOK_SUPER_KEY = book; String COLUMN_FAMILY = "UserBookPage"; String COLUMN_NAME = "times"; try{ ColumnPath columnPath = new ColumnPath(); columnPath.column_family = COLUMN_FAMILY; columnPath.super_column = this.cassandraUtil.toByteBuffer(BOOK_SUPER_KEY); ColumnOrSuperColumn columnOrSuperColumn = client.get(this.cassandraUtil.toByteBuffer(USER_KEY), columnPath, ConsistencyLevel.ONE); SuperColumn superColumn = columnOrSuperColumn.getSuper_column(); List<Column> columns = superColumn.getColumns(); for(Column col : columns) { String title = new String(col.getName(), "UTF-8"); if(title.equals(COLUMN_NAME)) { return Integer.parseInt(new String(col.getValue(), "UTF-8")); } } }catch(Exception e) { return 0; } return 0; }
/** * usage: java -cp <configpath> org.apache.cassandra.client.TestRingCache [keyspace row-id-prefix row-id-int] * to test a single keyspace/row, use the parameters. row-id-prefix and row-id-int are appended together to form a * single row id. If you supply now parameters, 'Keyspace1' is assumed and will check 9 rows ('row1' through 'row9'). * @param args * @throws Exception */ public static void main(String[] args) throws Throwable { int minRow; int maxRow; String rowPrefix, keyspace = "Keyspace1"; if (args.length > 0) { keyspace = args[0]; rowPrefix = args[1]; minRow = Integer.parseInt(args[2]); maxRow = minRow + 1; } else { minRow = 1; maxRow = 10; rowPrefix = "row"; } TestRingCache tester = new TestRingCache(keyspace); for (int nRows = minRow; nRows < maxRow; nRows++) { ByteBuffer row = ByteBufferUtil.bytes((rowPrefix + nRows)); ColumnPath col = new ColumnPath("Standard1").setSuper_column((ByteBuffer)null).setColumn("col1".getBytes()); ColumnParent parent = new ColumnParent("Standard1").setSuper_column((ByteBuffer)null); Collection<InetAddress> endpoints = tester.ringCache.getEndpoint(row); InetAddress firstEndpoint = endpoints.iterator().next(); System.out.printf("hosts with key %s : %s; choose %s%n", new String(row.array()), StringUtils.join(endpoints, ","), firstEndpoint); // now, read the row back directly from the host owning the row locally tester.setup(firstEndpoint.getHostAddress(), DatabaseDescriptor.getRpcPort()); tester.thriftClient.set_keyspace(keyspace); tester.thriftClient.insert(row, parent, new Column(ByteBufferUtil.bytes("col1")).setValue(ByteBufferUtil.bytes("val1")).setTimestamp(1), ConsistencyLevel.ONE); Column column = tester.thriftClient.get(row, col, ConsistencyLevel.ONE).column; System.out.println("read row " + new String(row.array()) + " " + new String(column.name.array()) + ":" + new String(column.value.array()) + ":" + column.timestamp); } System.exit(1); }
public static void main(String[] args) throws UnsupportedEncodingException, InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException { TTransport tr = new TSocket(HOST, PORT); //new default in 0.7 is framed transport TFramedTransport tf = new TFramedTransport(tr); TProtocol proto = new TBinaryProtocol(tf); Cassandra.Client client = new Cassandra.Client(proto); tf.open(); client.set_keyspace("Keyspace1"); String cfName = "Standard1"; ByteBuffer userIDKey = ByteBuffer.wrap("1".getBytes()); //this is a row key // Clock clock = new Clock(System.currentTimeMillis()); ColumnParent cp = new ColumnParent(cfName); //insert the name column log.debug("Inserting row for key {}" , userIDKey.toString()); Column nameCol = new Column(ByteBuffer.wrap("name".getBytes(UTF8))); nameCol.setValue(ByteBuffer.wrap("George Clinton".getBytes())); client.insert(userIDKey, cp, nameCol, CL); //insert the Age column Column ageCol = new Column(ByteBuffer.wrap("name".getBytes(UTF8))); ageCol.setValue(ByteBuffer.wrap("69".getBytes())); client.insert(userIDKey, cp, ageCol, CL); log.debug("Row insert done."); // read just the Name column log.debug("Reading Name Column:"); //create a representation of the Name column ColumnPath colPathName = new ColumnPath(cfName); colPathName.setColumn("name".getBytes(UTF8)); Column col = client.get(userIDKey, colPathName, CL).getColumn(); /*LOG.debug("Column name: " + new String(col.name, UTF8)); LOG.debug("Column value: " + new String(col.value, UTF8)); LOG.debug("Column timestamp: " + col.clock.timestamp);*/ //create a slice predicate representing the columns to read //start and finish are the range of columns--here, all SlicePredicate predicate = new SlicePredicate(); SliceRange sliceRange = new SliceRange(); sliceRange.setStart(new byte[0]); sliceRange.setFinish(new byte[0]); predicate.setSlice_range(sliceRange); log.debug("Complete Row:"); // read all columns in the row ColumnParent parent = new ColumnParent(cfName); List<ColumnOrSuperColumn> results = client.get_slice(userIDKey, parent, predicate, CL); //loop over columns, outputting values for (ColumnOrSuperColumn result : results) { Column column = result.column; log.info("Column: {}, Value: {}", new String(column.getName(), UTF8), new String(column.getValue(), UTF8)); } tf.close(); log.debug("All done."); }
private void insertPOIPhoenixZoo() throws Exception { Map<ByteBuffer, Map<String, List<Mutation>>> outerMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); List<Mutation> columnsToAdd = new ArrayList<Mutation>(); long ts = System.currentTimeMillis(); String keyName = "Phoenix Zoo"; Column descCol = new Column(bytes("desc")); Column phoneCol = new Column(bytes("phone")); List<Column> cols = new ArrayList<Column>(); cols.add(descCol); cols.add(phoneCol); Map<String, List<Mutation>> innerMap = new HashMap<String, List<Mutation>>(); String cambriaName = "Cambria Suites Hayden"; Mutation columns = new Mutation(); ColumnOrSuperColumn descCosc = new ColumnOrSuperColumn(); SuperColumn sc = new SuperColumn(); sc.name = bytes(cambriaName); sc.columns = cols; descCosc.super_column = sc; columns.setColumn_or_supercolumn(descCosc); columnsToAdd.add(columns); String superCFName = "PointOfInterest"; ColumnPath cp = new ColumnPath(); cp.column_family = superCFName; cp.setSuper_column(cambriaName.getBytes()); cp.setSuper_columnIsSet(true); innerMap.put(superCFName, columnsToAdd); outerMap.put(bytes(keyName), innerMap); client.batch_mutate(outerMap, CL); LOG.debug("Done inserting Phoenix Zoo."); }
private void insertPOIEmpireState() throws Exception { Map<ByteBuffer, Map<String, List<Mutation>>> outerMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); List<Mutation> columnsToAdd = new ArrayList<Mutation>(); long ts = System.nanoTime(); String esbName = "Empire State Building"; Column descCol = new Column(bytes("desc")); Column phoneCol = new Column(bytes("phone")); List<Column> esbCols = new ArrayList<Column>(); esbCols.add(descCol); esbCols.add(phoneCol); Map<String, List<Mutation>> innerMap = new HashMap<String, List<Mutation>>(); Mutation columns = new Mutation(); ColumnOrSuperColumn descCosc = new ColumnOrSuperColumn(); SuperColumn waldorfSC = new SuperColumn(); waldorfSC.name = bytes(WALDORF_NAME); waldorfSC.columns = esbCols; descCosc.super_column = waldorfSC; columns.setColumn_or_supercolumn(descCosc); columnsToAdd.add(columns); String superCFName = "PointOfInterest"; ColumnPath cp = new ColumnPath(); cp.column_family = superCFName; cp.setSuper_column(WALDORF_NAME.getBytes()); cp.setSuper_columnIsSet(true); innerMap.put(superCFName, columnsToAdd); outerMap.put(bytes(esbName), innerMap); client.batch_mutate(outerMap, CL); LOG.debug("Done inserting Empire State."); }
public QueryPath(ColumnPath column_path) { this(column_path.column_family, column_path.super_column, column_path.column); }
private void removeRow(long timestamp, ByteBuffer key, ColumnPath colPath) { // Prerequisites: assert key != null; assert colPath != null; m_logger.debug("Removing row {} from {}", Utils.toString(Utils.copyBytes(key)), toString(colPath)); // The remove will be retried up to MAX_COMMIT_RETRIES times. boolean bSuccess = false; for (int attempts = 1; !bSuccess; attempts++) { try { // Attempt to remove the requested row. Date startDate = new Date(); m_client.remove(key, colPath, timestamp, ConsistencyLevel.ONE); timing("remove", startDate); if (attempts > 1) { // Since we had a failure and warned about it, confirm which commit succeeded. m_logger.info("remove() succeeded on attempt #{}", attempts); } bSuccess = true; } catch (InvalidRequestException ex) { // No point in retrying this one. String errMsg = "remove() failed for table: " + colPath.getColumn_family(); m_bFailed = true; m_logger.error(errMsg, ex); throw new RuntimeException(errMsg, ex); } catch (Exception ex) { // For a timeout exception, Cassandra may be very busy, so we retry up // to the configured limit. if (attempts >= m_max_commit_attempts) { m_bFailed = true; String errMsg = "All retries exceeded; abandoning remove() for table: " + colPath.getColumn_family(); m_logger.error(errMsg, ex); throw new RuntimeException(errMsg, ex); } // Report retry as a warning. m_logger.warn("remove() attempt #{} failed: {}", attempts, ex); try { // We wait more with each failure. Thread.sleep(attempts * m_retry_wait_millis); } catch (InterruptedException e1) { // ignore } // Reconnect since the connection may be bad. This throws an DBNotAvailableException // if unsuccessful. reconnect(ex); } } }
private static String toString(ColumnPath colPath) { return "CF '" + colPath.getColumn_family() + "'"; }