Oracle nosql代码Demo留存

学习使用Oracle Nosql

把代码Demo留存一下,后续继续学习时参考用



public class TestNosql {
 public static void main(String[] args) {
  ArrayList<String> majorComponents = new ArrayList<String>();
  ArrayList<String> minorComponents = new ArrayList<String>();
  
  // Define the major and minor path components for the key
  majorComponents.add("Smith");
  majorComponents.add("11");
  Key myKey = Key.createKey(majorComponents, minorComponents);//key创建
//  Flight flt = new Flight();
//  flt.setFlightNo("MU5113");
//  flt.setFligthDate(new Date());
//  flt.setCabin(300);
  String data = "408 555 5555";
  Value myValue = Value.createValue(data.getBytes());//value创建
  
  // TODO Auto-generated method stub
  //连接配置
  KVStoreConfig kconfig = new KVStoreConfig("onstore","172.31.3.32:5000");
  KVStore kvstore = KVStoreFactory.getStore(kconfig);
  kvstore.put(myKey, myValue);//放入内存数据库
  majorComponents.removeAll(majorComponents);
  majorComponents.add("Smith");
  majorComponents.add("9");
  myKey = Key.createKey(majorComponents, minorComponents);//key创建
  data = "408 555 6666";
  myValue = Value.createValue(data.getBytes());//value创建
  kvstore.put(myKey, myValue);//放入内存数据库
  TestMulQuery(kvstore);
  
  ValueVersion vv = kvstore.get(myKey);//内存数据库中取出
  Value v = vv.getValue();
  String data2 = new String(v.getValue());
  System.out.println("****************data2:"+data2);
  //读取多条记录:
  //KVStore.multiGet()
  
  //复杂test
  final Schema.Parser parser = new Schema.Parser();
  try {
   //value按模板放对象
   parser.parse(new File("D:/workspace/Test/bin/PersonSchema.avsc"));
   final Schema personSchema =
     parser.getTypes().get("avro.PersonInformation");
   final AvroCatalog catalog = kvstore.getAvroCatalog();
   final GenericAvroBinding binding =
   catalog.getGenericBinding(personSchema);
   //读取:
//   final GenericRecord member;
//   final int ID;
//   if (vv != null) {
//   /* Deserialize the the value */
//   member = binding.toObject(vv.getValue());
//   /* Retrieve the contents of the ID field. Because we are
//   * using a generic binding, we must type cast the retrieved
//   * value.
//   */
//   ID = (Integer) member.get("ID");
//   }
   //写入:
   final GenericRecord person = new GenericData.Record(personSchema);
   final int id_id = 100011;
   person.put("ID", id_id);
   person.put("first", "TESTTEST");
   person.put("last", "TESTTEST2");
   /**
   * To serialize this information so that it can be written to
   * the store, use GenericBinding.toValue() as the value for the
   * store put(). That is, assuming you already have a store handle
   * and a key:
   */
   Key key = Key.createKey(majorComponents, minorComponents);
   kvstore.put(key, binding.toValue(person));
   GenericRecord member;
   ValueVersion vv2 = kvstore.get(key);
   member = binding.toObject(vv2.getValue());
//   ValueVersion vv2 = kvstore.get(key);
//   Value v2 = vv2.getValue();
//   String data3 = new String(v2.getValue());
   System.out.println("****************data3:"+member.get("ID")+",data4:"+member.get("first")+",data5:"+member.get("last"));
   member.put("ID", 1001);
   kvstore.put(key, binding.toValue(member));
   System.out.println("****************data6:"+member.get("ID")+",data7:"+member.get("first")+",data8:"+member.get("last"));
   ValueVersion vv3 = kvstore.get(key);
   member = binding.toObject(vv3.getValue());
   System.out.println("****************data9:"+member.get("ID")+",data10:"+member.get("first")+",data11:"+member.get("last"));
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 /**
  * 范围查找
  * @param kvstore
  */
 public static void TestMulQuery(KVStore kvstore){
  ArrayList<String> majorComponents = new ArrayList<String>();
  
  // Define the major and minor path components for the key
  majorComponents.add("Smith");
  // Create the retrieval key
  Key myKey = Key.createKey(majorComponents);
  KeyRange kr = new KeyRange("10", true, "111", true);
  // Now retrieve the records. Note that we do not show the creation of
  // the kvstore handle here.
  try {
  Iterator<KeyValueVersion> i =
  kvstore.storeIterator(Direction.UNORDERED, 0,
    myKey, kr, null);
  //System.out.println(i.);
  while (i.hasNext()) {
  Value v = i.next().getValue();
  System.out.println("Sub################:"+new String(v.getValue()));
  // Do some work with the Value here
  }
  } catch (RequestTimeoutException re) {
  // The operation was not completed within the
  // timeout value
  }
 }
 
}


PersonSchema.avsc

{
"type": "record",
"name": "PersonInformation",
"namespace": "avro",
"fields": [
{"name": "ID", "type": "int","default" : -1},
{ "name": "first", "type": "string","default" : "a" },
{ "name": "last", "type": "string","default" : "b" }
]
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值