/** * 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); }
private Mutation getMutation(String columnName, Object value, long timestamp) { byte[] columnNameBytes; try { columnNameBytes = columnName.getBytes("UTF-8"); } catch (UnsupportedEncodingException exc) { throw new StorageException("Unsupported character encoding for column name", exc); } byte[] valueBytes = convertValueToBytes(value); Column column = new Column(); column.setName(columnNameBytes); column.setValue(valueBytes); column.setTimestamp(timestamp); ColumnOrSuperColumn columnOrSuperColumn = new ColumnOrSuperColumn(); columnOrSuperColumn.setColumn(column); Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(columnOrSuperColumn); return mutation; }
@Override public List<DColumn> getColumns(String storeName, String rowKey, String startColumn, String endColumn, int count) { DBConn dbConn = getDBConnection(); try { List<ColumnOrSuperColumn> columns = dbConn.getSlice( CassandraDefs.columnParent(storeName), CassandraDefs.slicePredicateStartEndCol(Utils.toBytes(startColumn), Utils.toBytes(endColumn), count), Utils.toByteBuffer(rowKey)); List<DColumn> result = new ArrayList<>(columns.size()); for(ColumnOrSuperColumn column: columns) { result.add(new DColumn(column.getColumn().getName(), column.getColumn().getValue())); } return result; } finally { returnDBConnection(dbConn); } }
@Override public List<DColumn> getColumns(String storeName, String rowKey, Collection<String> columnNames) { DBConn dbConn = getDBConnection(); try { List<byte[]> colNameList = new ArrayList<>(columnNames.size()); for (String colName : columnNames) { colNameList.add(Utils.toBytes(colName)); } List<ColumnOrSuperColumn> columns = dbConn.getSlice( CassandraDefs.columnParent(storeName), CassandraDefs.slicePredicateColNames(colNameList), Utils.toByteBuffer(rowKey)); List<DColumn> result = new ArrayList<>(columns.size()); for(ColumnOrSuperColumn column: columns) { result.add(new DColumn(column.getColumn().getName(), column.getColumn().getValue())); } return result; } finally { returnDBConnection(dbConn); } }
private static Mutation createMutation(byte[] colName, byte[] colValue, long timestamp) { if (colValue == null) { colValue = EMPTY_BYTES; } Column col = new Column(); col.setName(colName); col.setValue(colValue); col.setTimestamp(timestamp); ColumnOrSuperColumn cosc = new ColumnOrSuperColumn(); cosc.setColumn(col); Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(cosc); return mutation; }
@Override protected Pair<ByteBuffer, SortedMap<ByteBuffer, IColumn>> computeNext() { maybeInit(); if (rows == null) { return endOfData(); } KeySlice ks = rows.get(0); SortedMap<ByteBuffer, IColumn> map = new TreeMap<ByteBuffer, IColumn>(comparator); for (ColumnOrSuperColumn cosc : ks.columns) { IColumn column = unthriftify(cosc); map.put(column.name(), column); } // return new Pair<ByteBuffer, SortedMap<ByteBuffer, IColumn>>(ks.key, map); return Pair.create(ks.key, map); }
public List<String> QueryOneMinute(String min) { List<String> userList = new ArrayList<String>(); try { List<ColumnOrSuperColumn> results = client.get_slice(Utils .toByteBuffer(min), columnParent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn cc : results) { SuperColumn superColumn = cc.getSuper_column(); List<Column> list = superColumn.getColumns(); for (Column c : list) { String columnName = new String(c.getName(), "UTF-8"); if (columnName.equals("username")) { String value = new String(c.getValue(), "UTF-8"); if (!userList.contains(value)) { userList.add(value); } } } } } catch (Exception e) { System.out.println(e); } return userList; }
public List<String> QueryOneMinute(String min){ List<String> bookList = new ArrayList<String>(); try { List<ColumnOrSuperColumn> results = client.get_slice(Utils .toByteBuffer(min), columnParent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn cc : results) { SuperColumn superColumn = cc.getSuper_column(); List<Column> list = superColumn.getColumns(); for (Column c : list) { String columnName = new String(c.getName(), "UTF-8"); if (columnName.equals("bookno")) { String value = new String(c.getValue(), "UTF-8"); if (!bookList.contains(value)) { bookList.add(value); } } } } } catch (Exception e) { System.out.println(e); } return bookList; }
public void run(final ThriftClient client) throws IOException { List<CounterColumn> columns = new ArrayList<>(); for (ByteBuffer name : select().select(settings.columns.names)) columns.add(new CounterColumn(name, counteradd.next())); List<Mutation> mutations = new ArrayList<>(columns.size()); for (CounterColumn c : columns) { ColumnOrSuperColumn cosc = new ColumnOrSuperColumn().setCounter_column(c); mutations.add(new Mutation().setColumn_or_supercolumn(cosc)); } Map<String, List<Mutation>> row = Collections.singletonMap(type.table, mutations); final ByteBuffer key = getKey(); final Map<ByteBuffer, Map<String, List<Mutation>>> record = Collections.singletonMap(key, row); timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { client.batch_mutate(record, settings.command.consistencyLevel); return true; } @Override public int partitionCount() { return 1; } @Override public int rowCount() { return 1; } }); }
public void run(final ThriftClient client) throws IOException { final ColumnSelection select = select(); final ByteBuffer key = getKey(); final List<ByteBuffer> expect = getColumnValues(select); timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { List<ColumnOrSuperColumn> row = client.get_slice(key, new ColumnParent(type.table), select.predicate(), settings.command.consistencyLevel); if (expect == null) return !row.isEmpty(); if (row == null) return false; if (row.size() != expect.size()) return false; for (int i = 0 ; i < row.size() ; i++) if (!row.get(i).getColumn().bufferForValue().equals(expect.get(i))) return false; return true; } @Override public int partitionCount() { return 1; } @Override public int rowCount() { return 1; } }); }
public void run(final ThriftClient client) throws IOException { final ByteBuffer key = getKey(); final List<Column> columns = getColumns(); List<Mutation> mutations = new ArrayList<>(columns.size()); for (Column c : columns) { ColumnOrSuperColumn column = new ColumnOrSuperColumn().setColumn(c); mutations.add(new Mutation().setColumn_or_supercolumn(column)); } Map<String, List<Mutation>> row = Collections.singletonMap(type.table, mutations); final Map<ByteBuffer, Map<String, List<Mutation>>> record = Collections.singletonMap(key, row); timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { client.batch_mutate(record, settings.command.consistencyLevel); return true; } @Override public int partitionCount() { return 1; } @Override public int rowCount() { return 1; } }); }
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); }
/** * get 讀取所有column * * @throws Exception */ @Test public void get2() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // 讀取所有column String COLUMN_FAMILY = "student"; ColumnParent columnParent = new ColumnParent(COLUMN_FAMILY); // 術語 SlicePredicate predicate = new SlicePredicate(); // 範圍 SliceRange sliceRange = new SliceRange(); // sliceRange.setStart(ByteBufferHelper.toByteBuffer(new byte[0]));//開始 sliceRange.setStart(new byte[0]);// 開始 sliceRange.setFinish(new byte[0]);// 結束 sliceRange.setCount(100);// 筆數 // predicate.setSlice_range(sliceRange); String ROW_KEY = "Jack"; // 結果 // key, column_parent, predicate, consistency_level List<ColumnOrSuperColumn> results = client.get_slice( ByteBufferHelper.toByteBuffer(ROW_KEY), columnParent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn cos : results) { Column column = cos.getColumn(); System.out.println(ROW_KEY + ", " + ByteHelper.toString(column.getName()) + ": " + ByteHelper.toString(column.getValue()) + ", " + column.getTimestamp()); // Jack, art, 87, 1380788003220 // Jack, grad, 5, 1380788003203 // Jack, math, 97, 1380788003214 } }
/** * update * * @throws Exception */ @Test public void update() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); List<Mutation> mutations = new LinkedList<Mutation>(); // <columnFamily,mutations> Map<String, List<Mutation>> columnfamilyMutaions = new HashMap<String, List<Mutation>>();// keyMutations // <rowKey,keyMutations> Map<ByteBuffer, Map<String, List<Mutation>>> rowKeyMutations = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); long timestamp = System.nanoTime(); // Column column = new Column(); column.setName(ByteBufferHelper.toByteBuffer("grad")); column.setValue(ByteBufferHelper.toByteBuffer("9")); column.setTimestamp(timestamp); // ColumnOrSuperColumn cos = new ColumnOrSuperColumn(); cos.setColumn(column); // Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(cos); mutations.add(mutation); String COLUMN_FAMILY = "student"; columnfamilyMutaions.put(COLUMN_FAMILY, mutations); String ROW_KEY = "Jack"; rowKeyMutations.put(ByteBufferHelper.toByteBuffer(ROW_KEY), columnfamilyMutaions); // mutation_map, consistency_level client.batch_mutate(rowKeyMutations, 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 List<Map<String,Object>> convertKeySliceList(List<KeySlice> keySliceList, String primaryKeyName) { List<Map<String,Object>> rowList = new ArrayList<Map<String,Object>>(); try { for (KeySlice keySlice: keySliceList) { List<ColumnOrSuperColumn> columnList = keySlice.getColumns(); if (!columnList.isEmpty()) { byte[] keyBytes = keySlice.getKey(); String key = new String(keyBytes, "UTF-8"); Map<String,Object> columnMap = new HashMap<String,Object>(); columnMap.put(primaryKeyName, key); for (ColumnOrSuperColumn columnOrSuperColumn: columnList) { Column column = columnOrSuperColumn.getColumn(); byte[] columnNameBytes = column.getName(); String columnName = new String(columnNameBytes, "UTF-8"); byte[] valueBytes = column.getValue(); String value = new String(valueBytes, "UTF-8"); if (value.equals(NULL_VALUE_STRING)) value = null; columnMap.put(columnName, value); } rowList.add(columnMap); } } return rowList; } catch (UnsupportedEncodingException exc) { throw new StorageException("Character encoding exception with key range", exc); } }
private static Mutation getMutation(Text word, int sum) { Column c = new Column(); c.setName(Arrays.copyOf(word.getBytes(), word.getLength())); c.setValue(ByteBufferUtil.bytes(String.valueOf(sum))); c.setTimestamp(System.currentTimeMillis()); Mutation m = new Mutation(); m.setColumn_or_supercolumn(new ColumnOrSuperColumn()); m.column_or_supercolumn.setColumn(c); return m; }
private IColumn unthriftify(ColumnOrSuperColumn cosc) { if (cosc.counter_column != null) { return unthriftifyCounter(cosc.counter_column); } if (cosc.counter_super_column != null) { return unthriftifySuperCounter(cosc.counter_super_column); } if (cosc.super_column != null) { return unthriftifySuper(cosc.super_column); } assert cosc.column != null; return unthriftifySimple(cosc.column); }
@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; }