public void run(final ThriftClient client) throws IOException { final SlicePredicate predicate = select().predicate(); final ByteBuffer key = getKey(); timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { List<?> r = client.get_slice(key, new ColumnParent(type.table), predicate, settings.command.consistencyLevel); return r != null && r.size() > 0; } @Override public int partitionCount() { return 1; } @Override public int rowCount() { return 1; } }); }
public State(Command type, StressSettings settings, StressMetrics metrics) { this.type = type; this.timer = metrics.getTiming().newTimer(); if (type == Command.MIXED) readWriteSelector = ((SettingsCommandMixed) settings.command).selector(); else readWriteSelector = null; this.settings = settings; this.keyGen = settings.keys.newKeyGen(); this.rowGen = settings.columns.newRowGen(); this.metrics = metrics; if (!settings.columns.useSuperColumns) columnParents = Collections.singletonList(new ColumnParent(settings.schema.columnFamily)); else { ColumnParent[] cp = new ColumnParent[settings.columns.superColumns]; for (int i = 0 ; i < cp.length ; i++) cp[i] = new ColumnParent("Super1").setSuper_column(ByteBufferUtil.bytes("S" + i)); columnParents = Arrays.asList(cp); } }
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 CasTimeReader() { try { TTransport tr = new TFramedTransport(new TSocket("10.15.61.111", 9160)); TProtocol proto = new TBinaryProtocol(tr); client = new Cassandra.Client(proto); tr.open(); client.set_keyspace("CadalSecTest"); predicate = new SlicePredicate(); SliceRange range = new SliceRange(); range.setStart(new byte[0]); range.setFinish(new byte[0]); range.setCount(10000); predicate.setSlice_range(range); columnParent = new ColumnParent(); columnParent.setColumn_family("RecordMinute"); } catch (Exception e) { System.out.println(e); } }
public CasTimeBook() { try { TTransport tr = new TFramedTransport(new TSocket("10.15.61.111", 9160)); TProtocol proto = new TBinaryProtocol(tr); client = new Cassandra.Client(proto); tr.open(); client.set_keyspace("CadalSecTest"); predicate = new SlicePredicate(); SliceRange range = new SliceRange(); range.setStart(new byte[0]); range.setFinish(new byte[0]); range.setCount(10000); predicate.setSlice_range(range); columnParent = new ColumnParent(); columnParent.setColumn_family("RecordMinute"); } catch (Exception e) { System.out.println(e); } }
/** * Insert into IpUser Column Family */ public boolean InsertTest(String key, int val) { System.out.println("------------InsertTest--------------"); try { ColumnParent parent = new ColumnParent("test"); long timeStamp = System.currentTimeMillis(); Column idColumnPageid = new Column(); idColumnPageid.setName(this.cassandraUtil.toByteBuffer("signal")); idColumnPageid.setValue(this.cassandraUtil.toByteBuffer(String.valueOf(val))); idColumnPageid.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(key), parent, idColumnPageid, ConsistencyLevel.ONE); } catch (Exception e) { e.printStackTrace(); } return true; }
/** * Insert into IpUser Column Family */ public boolean InsertTest2(int key, String val) { System.out.println("------------InsertTest--------------"); try { ColumnParent parent = new ColumnParent("test"); long timeStamp = System.currentTimeMillis(); Column idColumnPageid = new Column(); idColumnPageid.setName(this.cassandraUtil.toByteBuffer("content")); idColumnPageid.setValue(this.cassandraUtil.toByteBuffer(val)); idColumnPageid.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(key)), parent, idColumnPageid, ConsistencyLevel.ONE); } catch (Exception e) { e.printStackTrace(); } return true; }
/** * Insert into IpUser Column Family */ public boolean InsertTest3(int userid, int key, String val) { System.out.println("------------InsertTest--------------"); try { ColumnParent parent = new ColumnParent("test"); long timeStamp = System.currentTimeMillis(); Column idColumnPageid = new Column(); idColumnPageid.setName(this.cassandraUtil.toByteBuffer(String.valueOf(key))); idColumnPageid.setValue(this.cassandraUtil.toByteBuffer(val)); idColumnPageid.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(userid)), parent, idColumnPageid, ConsistencyLevel.ONE); } catch (Exception e) { e.printStackTrace(); } return true; }
/** * Insert into CF -- "UserChapter" * @param int userid: the key of column like '119115' * @param int signal: chapter's signal like '23' * @param String content: chapter's value like '"07018720_1.0.0.0.0"' */ public boolean InsertIntoUserChapter(int userid, int signal, String content){ // System.out.println("------------InsertIntoUserChapter--------------"); try { ColumnParent parent = new ColumnParent("UserChapter"); long timeStamp = System.currentTimeMillis(); Column userChapter = new Column(); userChapter.setName(this.cassandraUtil.toByteBuffer(String.valueOf(signal))); userChapter.setValue(this.cassandraUtil.toByteBuffer(content)); userChapter.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(userid)), parent, userChapter, ConsistencyLevel.QUORUM); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * Insert into CF -- "SignalChapterMap" * @param int signal: chapter's signal like '23' * @param String content: chapter's value like '"07018720_1.0.0.0.0"' */ public boolean InsertIntoSignalChapterMap(int signal, String content){ // System.out.println("------------InsertIntoSignalChapterMap--------------"); try { ColumnParent parent = new ColumnParent("SignalChapterMap"); long timeStamp = System.currentTimeMillis(); Column signalChapter = new Column(); signalChapter.setName(this.cassandraUtil.toByteBuffer("content")); signalChapter.setValue(this.cassandraUtil.toByteBuffer(content)); signalChapter.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(signal)), parent, signalChapter, ConsistencyLevel.QUORUM); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * Update 'maxid' in CF 'ChapterSignalMap' */ public boolean UpdateMaxid(String signal) { try{ ColumnParent parent = new ColumnParent("ChapterSignalMap"); long timeStamp = System.currentTimeMillis(); Column signalCol = new Column(); signalCol.setName(this.cassandraUtil.toByteBuffer("maxid")); signalCol.setValue(this.cassandraUtil.toByteBuffer(signal)); signalCol.setTimestamp(timeStamp); this.client.insert(this.cassandraUtil.toByteBuffer("maxid"), parent, signalCol, ConsistencyLevel.QUORUM); }catch(Exception e) { e.printStackTrace(); } return true; }
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; }
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; } }); }
/** * 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 } }
private void insertColumnValue(String parentName, String id, String name, String value, long ts) throws TException, TimedOutException, UnavailableException, InvalidRequestException, UnsupportedEncodingException { ColumnParent parent = new ColumnParent(parentName); Column column = new Column(toByteBuffer(name)); column.setValue(toByteBuffer(value)); column.setTimestamp(ts); getClient().insert(toByteBuffer(id), parent, column, ConsistencyLevel.ONE); }
public void run(final ThriftClient client) throws IOException { SliceRange sliceRange = new SliceRange(); // start/finish sliceRange.setStart(new byte[] {}).setFinish(new byte[] {}); // reversed/count sliceRange.setReversed(false).setCount(state.settings.columns.maxColumnsPerKey); // initialize SlicePredicate with existing SliceRange final SlicePredicate predicate = new SlicePredicate().setSlice_range(sliceRange); final ByteBuffer key = getKey(); for (final ColumnParent parent : state.columnParents) { timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { return client.get_slice(key, parent, predicate, state.settings.command.consistencyLevel).size() != 0; } @Override public String key() { return new String(key.array()); } @Override public int keyCount() { return 1; } }); } }
public void run(final ThriftClient client) throws IOException { final SlicePredicate predicate = new SlicePredicate(); if (state.settings.columns.names == null) predicate.setSlice_range(new SliceRange() .setStart(new byte[] {}) .setFinish(new byte[] {}) .setReversed(false) .setCount(state.settings.columns.maxColumnsPerKey) ); else // see CASSANDRA-3064 about why this is useful predicate.setColumn_names(state.settings.columns.names); final ByteBuffer key = getKey(); for (final ColumnParent parent : state.columnParents) { timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { return client.get_slice(key, parent, predicate, state.settings.command.consistencyLevel).size() != 0; } @Override public String key() { return new String(key.array()); } @Override public int keyCount() { return 1; } }); } }
public void run(final ThriftClient client) throws IOException { final SlicePredicate predicate = new SlicePredicate().setSlice_range( new SliceRange( ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, state.settings.columns.maxColumnsPerKey ) ); final List<ByteBuffer> keys = getKeys(((SettingsCommandMulti) state.settings.command).keysAtOnce); for (final ColumnParent parent : state.columnParents) { timeWithRetry(new RunOp() { int count; @Override public boolean run() throws Exception { return (count = client.multiget_slice(keys, parent, predicate, state.settings.command.consistencyLevel).size()) != 0; } @Override public String key() { return keys.toString(); } @Override public int keyCount() { return count; } }); } }
public void run(final ThriftClient client) throws IOException { final SlicePredicate predicate = slicePredicate(); final ByteBuffer key = getKey(); for (final ColumnParent parent : state.columnParents) { timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { List<?> r = client.get_slice(key, parent, predicate, state.settings.command.consistencyLevel); return r != null && r.size() > 0; } @Override public String key() { return new String(key.array()); } @Override public int keyCount() { return 1; } }); } }
public void run(final ThriftClient client) throws IOException { final SlicePredicate predicate = new SlicePredicate().setSlice_range( new SliceRange( ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, state.settings.columns.maxColumnsPerKey ) ); final List<ByteBuffer> keys = getKeys(state.settings.command.keysAtOnce); for (final ColumnParent parent : state.columnParents) { timeWithRetry(new RunOp() { int count; @Override public boolean run() throws Exception { return (count = client.multiget_slice(keys, parent, predicate, state.settings.command.consistencyLevel).size()) != 0; } @Override public String key() { return keys.toString(); } @Override public int keyCount() { return count; } }); } }
private List<ColumnParent> columnParents(Command type, StressSettings settings) { if (!settings.columns.useSuperColumns) return Collections.singletonList(new ColumnParent(type.table)); else { ColumnParent[] cp = new ColumnParent[settings.columns.superColumns]; for (int i = 0 ; i < cp.length ; i++) cp[i] = new ColumnParent(type.supertable).setSuper_column(ByteBufferUtil.bytes("S" + i)); return Arrays.asList(cp); } }
@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")); }
/** * Insert into QueryBookKey CF */ public boolean InsertQueryBookKey(String key, String ip_user, String time) { System.out.println("------------InsertQueryBookKey--------------"); String KEY = key; String IP_USER_TITLE = ip_user; String IP_USER_VALUE = time; String COLUMN_FAMILY = "QueryBookKey"; try { ColumnParent parent = new ColumnParent(COLUMN_FAMILY); long timeStamp = System.currentTimeMillis(); Column idColumnIp = new Column(); idColumnIp.setName(this.cassandraUtil.toByteBuffer(IP_USER_TITLE)); idColumnIp.setValue(this.cassandraUtil.toByteBuffer(IP_USER_VALUE)); idColumnIp.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(KEY), parent, idColumnIp, ConsistencyLevel.QUORUM); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * Query from cf [RecordMinute] * @param queryWord * @return */ public int QueryRecordMinute(String queryWord) { TTransport tr = new TFramedTransport(new TSocket("10.15.61.118", 9160)); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); try { tr.open(); client.set_keyspace("CadalSecTest"); // read entire row SlicePredicate predicate = new SlicePredicate();// new SliceRange(new byte[0], new byte[0], false, 10) SliceRange range = new SliceRange(); range.start = Utils.toByteBuffer(""); range.finish = Utils.toByteBuffer(""); predicate.setSlice_range(range); ColumnParent parent = new ColumnParent(); parent.column_family = "RecordMinute"; List<ColumnOrSuperColumn> results = client.get_slice(Utils.toByteBuffer(queryWord), parent, predicate, ConsistencyLevel.ONE); tr.close(); return results.size(); } catch (Exception e) { e.printStackTrace(); } return 0; }
/** * Query From CF -- "UserChapter" * @param int userid: 'userid' is column key of this CF, like '119115' */ public List<String> QueryFromUserChapter(int userid){ // System.out.println("------------QueryFromUserChapter--------------"); List<String> listStr = new ArrayList<String>(); try { SlicePredicate predicate = new SlicePredicate(); SliceRange range = new SliceRange(); range.start = this.cassandraUtil.toByteBuffer(""); range.finish = this.cassandraUtil.toByteBuffer(""); range.setCount(10000000); predicate.setSlice_range(range); ColumnParent parent = new ColumnParent(); parent.column_family = "UserChapter"; // CF name List<ColumnOrSuperColumn> results = client.get_slice(this.cassandraUtil.toByteBuffer(String.valueOf(userid)), parent, predicate,ConsistencyLevel.ONE); for (ColumnOrSuperColumn result : results) { Column column1 = result.column; listStr.add(new String(column1.getName(), "UTF-8")); } return listStr; } catch (Exception e) { return listStr; } }
/** * Insert into CF -- "ChapterSignalMap" * @param String content: chapter's value like '"07018720_1.0.0.0.0"' * @param int signal: chapter's signal like '23' * This function will columate 'maxid' automatic. */ public boolean InsertIntoChapterSignalMap(String content, int signal){ // System.out.println("------------InsertIntoChapterSignalMap--------------"); try { ColumnParent parent = new ColumnParent("ChapterSignalMap"); long timeStamp = System.currentTimeMillis(); // insert into new column Column chapterSignal = new Column(); chapterSignal.setName(this.cassandraUtil.toByteBuffer("signal")); chapterSignal.setValue(this.cassandraUtil.toByteBuffer(String.valueOf(signal))); chapterSignal.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(content), parent, chapterSignal, ConsistencyLevel.QUORUM); // update 'maxid' column Column maxidColumn = new Column(); maxidColumn.setName(this.cassandraUtil.toByteBuffer("maxid")); maxidColumn.setValue(this.cassandraUtil.toByteBuffer(String.valueOf(signal + 1))); maxidColumn.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer("maxid"), parent, maxidColumn, ConsistencyLevel.QUORUM); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * 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); }
/** * get * * @throws Exception */ @Test public void getByKey() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); String COLUMN_FAMILY = "student"; // 讀取整筆 ColumnParent columnParent = new ColumnParent(COLUMN_FAMILY); // 術語 SlicePredicate predicate = new SlicePredicate(); // InvalidRequestException(why:predicate column_names and slice_range // may not both be present) // 範圍 // SliceRange sliceRange = new SliceRange(); // sliceRange.setStart(new byte[0]);// 開始 // sliceRange.setFinish(new byte[0]);// 結束 // predicate.setSlice_range(sliceRange); // 讀取1個column predicate.addToColumn_names(ByteBufferHelper.toByteBuffer("grad")); // key範圍 KeyRange keyRange = new KeyRange(); keyRange.setStart_key(new byte[0]); keyRange.setEnd_key(new byte[0]); keyRange.setCount(100); // 結果 // column_parent, predicate, range, consistency_level List<KeySlice> results = client.get_range_slices(columnParent, predicate, keyRange, ConsistencyLevel.ONE); for (KeySlice keySlice : results) { for (ColumnOrSuperColumn cos : keySlice.getColumns()) { Column column = cos.column; System.out.println(ByteHelper.toString(keySlice.getKey()) + ", " + ByteHelper.toString(column.getName()) + ": " + ByteHelper.toString(column.getValue()) + ", " + column.getTimestamp()); // Rose, grad, 4, 1380931646061000 // Jack, art, 87, 1380933848350 // Jack, grad, 5, 1380932164492000 // Jack, math, 97, 1380933848305 } } }