Nosql Cassandra 0.6 key值的区间查询例子

前端之家收集整理的这篇文章主要介绍了Nosql Cassandra 0.6 key值的区间查询例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Nosql Cassandra 0.6 key值的区间查询

小记:
传入条件 如key区间a至c 一种有a-d的数据
List<KeySlice> sliceList = client.get_range_slice(keyspace,parent,
predicate,"a","d",1000,ConsistencyLevel.ONE);

  1. package com.sh2999.cassandra.testapp;
  2. import java.util.List;
  3. import org.apache.cassandra.thrift.Cassandra;
  4. import org.apache.cassandra.thrift.ColumnOrSuperColumn;
  5. import org.apache.cassandra.thrift.ColumnParent;
  6. import org.apache.cassandra.thrift.ColumnPath;
  7. import org.apache.cassandra.thrift.ConsistencyLevel;
  8. import org.apache.cassandra.thrift.KeySlice;
  9. import org.apache.cassandra.thrift.NotFoundException;
  10. import org.apache.cassandra.thrift.SlicePredicate;
  11. import org.apache.cassandra.thrift.SliceRange;
  12. import org.apache.thrift.protocol.TBinaryProtocol;
  13. import org.apache.thrift.protocol.TProtocol;
  14. import org.apache.thrift.transport.TSocket;
  15. import org.apache.thrift.transport.TTransport;
  16. /**
  17. *key 值的区间查询这里可以传入条件如key区间a至c一种有a-d的数据
  18. *
  19. *@sinceCassandra0.6
  20. *@authordb2admin
  21. *
  22. */
  23. public class Cassandra647TestApp{
  24. /**
  25. *
  26. *OrderPreservingPartitionershouldbeused.
  27. */
  28. public static void main(String[]args) throws Exception{
  29. Stringkeyspace="Keyspace1" ;
  30. Stringcf="sh2999.com" ;
  31. Stringkey="row1" ;
  32. byte []columnName= "colname" .getBytes( "UTF-8" );
  33. byte []data= "testdata" .getBytes( "UTF-8" );
  34. TTransporttransport=new TSocket( "localhost" , 9160 );
  35. TProtocolprotocol=new TBinaryProtocol(transport);
  36. Cassandra.Clientclient=new Cassandra.Client(protocol);
  37. transport.open();
  38. ColumnPathpath=new ColumnPath(cf);
  39. path.setColumn(columnName);
  40. client.insert(keyspace,key,path,data,System.currentTimeMillis(),
  41. ConsistencyLevel.ONE);
  42. key="testrow2" ;
  43. byte []data2= "testdata" .getBytes( "UTF-8" );
  44. client.insert(keyspace,data2,
  45. ConsistencyLevel.ONE);
  46. key="a" ;
  47. byte []data3= "testdata" .getBytes( "UTF-8" );
  48. client.insert(keyspace,data3,
  49. ConsistencyLevel.ONE);
  50. key="b" ;
  51. byte []data4= "testdata" .getBytes( "UTF-8" );
  52. client.insert(keyspace,data4,
  53. ConsistencyLevel.ONE);
  54. key="c" ;
  55. byte []data5= "testdata" .getBytes( "UTF-8" );
  56. client.insert(keyspace,data5,
  57. ConsistencyLevel.ONE);
  58. key="d" ;
  59. byte []data6= "testdata" .getBytes( "UTF-8" );
  60. client.insert(keyspace,data6,
  61. ConsistencyLevel.ONE);
  62. Thread.sleep(1000 );
  63. ColumnPathrowpath=new ColumnPath(cf);
  64. rowpath.setColumn(columnName);
  65. //删除通过
  66. //client.remove(keyspace,rowpath,
  67. //ConsistencyLevel.ONE);
  68. //Thread.sleep(1000);
  69. try {
  70. ColumnOrSuperColumncosc=client.get(keyspace,
  71. ConsistencyLevel.ONE);
  72. System.out.println("Whoops!NotFoundExceptionnotthrown!" );
  73. }catch (NotFoundExceptione){
  74. System.out.println("OK,wegotaNotFoundException" );
  75. }
  76. ColumnParentparent=new ColumnParent(cf);
  77. SlicePredicatepredicate=new SlicePredicate();
  78. SliceRangerange=new SliceRange();
  79. range.start=new byte [ 0 ];
  80. range.finish=new byte [ 10 ];
  81. predicate.slice_range=range;
  82. //这里可以传入条件如key区间a至c一种有a-d的数据
  83. List<KeySlice>sliceList=client.get_range_slice(keyspace,
  84. predicate,"a" , "d" , 1000 ,ConsistencyLevel.ONE);
  85. for (KeySlicek:sliceList){
  86. System.err.println("Foundkey" +k.key);
  87. if (key.equals(k.key)){
  88. System.out.println("butkey" +k.key
  89. +"shouldhavebeenremoved" );
  90. }
  91. }
  92. }
  93. }

<Keyspaces> <Keyspace Name="Keyspace1"> <ColumnFamily CompareWith="BytesType" Name="wingTable" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="Standard1" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="Standard2" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="Standardw" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="sh2999.com" KeysCached="10%" /> <ReplicaPlacementStrategy>org.apache.cassandra.locator.RackUnawareStrategy</ReplicaPlacementStrategy> <ReplicationFactor>1</ReplicationFactor> <EndPointSnitch>org.apache.cassandra.locator.EndPointSnitch</EndPointSnitch> </Keyspace>

原文链接:https://www.f2er.com/nosql/204814.html

猜你在找的NoSQL相关文章