package com.iminido.nosql; import com.sequoiadb.base.CollectionSpace; import com.sequoiadb.base.DBCollection; import com.sequoiadb.base.DBCursor; import com.sequoiadb.base.Sequoiadb; import com.sequoiadb.base.SequoiadbDatasource; import com.sequoiadb.base.SequoiadbOption; import com.sequoiadb.exception.BaseException; import com.sequoiadb.net.ConfigOptions; import ist.Const; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Semaphore; import java.util.function.BiConsumer; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.logging.log4j.LogManager; import org.bson.BSONObject; import org.bson.BasicBSONObject; import org.bson.util.JSON; import org.json.JSONObject; /** * @author Jade */ public class Squ { private static SequoiadbDatasource sequoiadbDatasource; protected static final org.apache.logging.log4j.Logger log = LogManager.getLogger(Squ.class); private static Semaphore semaphore = new Semaphore(1); private static Map<String, Sequoiadb> dbs = new HashMap<String, Sequoiadb>(); public static void main(String[] args) { // String connString = Const.SEQUOIADB_HOST + ":" + Const.SEQUOIADB_PORT; try { // 建立 SequoiaDB 数据库连接 Sequoiadb sdb = new Sequoiadb(connString, "", ""); // 获取所有 Collection 信息,并打印出来 DBCursor cursor = sdb.listCollectionSpaces(); while (cursor.hasNext()) { System.out.println(cursor.getCurrent()); } } catch (BaseException e) { System.out.println("Sequoiadb driver error, error description:" + e.getErrorType()); } // initSequoiadbDatasource(); // getSequoiadb(); // BSONObject dbo = new BasicBSONObject(); //添加异常处理 // dbo.put("key_a", "value_a"); // dbo.put("key_b", "value_b"); // // DBCollection dBCollection = initCollection("test"); // dBCollection.save(dbo); // DBCursor dbc = dBCollection.query(); // while (dbc.hasNext()) { // System.out.println("" + dbc.getNext().toString()); // } // sequoiadbDatasource.close(dBCollection.getSequoiadb()); } static { } private static void initSequoiadbDatasource() { ArrayList<String> urls = new ArrayList<>(); ConfigOptions nwOpt = new ConfigOptions(); // 定义连接选项 SequoiadbOption dsOpt = new SequoiadbOption(); // 定义连接池选项 urls.add(Const.SEQUOIADB_HOST + ":" + Const.SEQUOIADB_PORT);// // urls.add("ubuntu-dev2:11810"); // urls.add("ubuntu-dev3:11810"); nwOpt.setConnectTimeout(500); // 设置若连接失败,超时时间(ms) nwOpt.setMaxAutoConnectRetryTime(0); // 设置若连接失败,重试次数 // 以下设置的都是 SequoiadbOption 的默认值 dsOpt.setMaxConnectionNum(500); // 设置连接池最大连接数 dsOpt.setInitConnectionNum(10); // 初始化连接池时,创建连接的数量 dsOpt.setDeltaIncCount(10); // 当池中没有可用连接时,增加连接的数量 dsOpt.setMaxIdeNum(10); // 周期清理多余的空闲连接时,应保留连接的数量 dsOpt.setTimeout(5 * 1000); // 当已使用的连接数到达设置的最大连接数时(500),请求连接的等待时间。 dsOpt.setAbandonTime(10 * 60 * 1000); // 连接存活时间,当连接空闲时间超过连接存活时间,将被连接池丢弃 dsOpt.setRecheckCyclePeriod(1 * 60 * 1000); // 清除多余空闲连接的周期 dsOpt.setRecaptureConnPeriod(10 * 60 * 1000); // 检测并取回异常地址的周期 sequoiadbDatasource = new SequoiadbDatasource(urls, Const.SEQUOIADB_USERNAME, Const.SEQUOIADB_PASSWORD, nwOpt, dsOpt); // 创建连接池 } private static synchronized Sequoiadb getSequoiadb() { return getSequoiadb("zy"); } private static synchronized Sequoiadb getSequoiadb(String dbName) { // Sequoiadb sdb = dbs.get(dbName); Sequoiadb sdb = null; try { sdb = sequoiadbDatasource.getConnection(); } catch (BaseException | InterruptedException ex) { Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex); } // while (sdb == null) { // try { // semaphore.tryAcquire(1, 2, TimeUnit.SECONDS); // } catch (InterruptedException ex) { // Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex); // } // try { // sdb = sequoiadbDatasource.getConnection(); // } catch (BaseException | InterruptedException ex) { // Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex); // } // } // dbs.put(dbName, sdb); semaphore.release(); return sdb; } private static CollectionSpace initCollectionSpace(String csName) { try { Sequoiadb sdb = getSequoiadb(); CollectionSpace cs; if (sdb.isCollectionSpaceExist(csName)) { cs = sdb.getCollectionSpace(csName); } else { cs = sdb.createCollectionSpace(csName); } return cs; } catch (BaseException ex) { Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex); return null; } } private static DBCollection initCollection(String collectionName) { CollectionSpace cs = initCollectionSpace(Const.SEQUOIADB_DATABASE); DBCollection cl; if (cs.isCollectionExist(collectionName)) { cl = cs.getCollection(collectionName); } else { cl = cs.createCollection(collectionName); } return cl; } public static void remove(String collectionName) { DBCollection cl = initCollection(collectionName); cl.delete(new BasicBSONObject()); sequoiadbDatasource.close(cl.getSequoiadb()); } public static boolean delete(String cl, String matcher) { try { DBCollection dbc = initCollection(cl); dbc.delete(matcher); sequoiadbDatasource.close(dbc.getSequoiadb()); return true; } catch (Exception e) { log.error("delete " + cl + "matcher=>" + matcher + "失败", e); return false; } } public static void delete(String cl, String matcher, String hint) { DBCollection dbc = initCollection(cl); dbc.delete(matcher, hint); sequoiadbDatasource.close(dbc.getSequoiadb()); } public static boolean insert(String collectionName, String... key_val) { String strJson = strs2json(key_val); if (strJson != null) { BSONObject dbo; try { dbo = (BasicBSONObject) JSON.parse(strJson); //添加异常处理 } catch (Exception e) { log.error("解析json字符串异常,传入的字符串为:" + strJson, e); return false; } try { DBCollection dbc = initCollection(collectionName); dbc.insert(dbo); sequoiadbDatasource.close(dbc.getSequoiadb()); return true; } catch (Exception e) { return false; } } return false; } public static boolean insert(String collectionName, String strJson) { BSONObject dbo = null; try { dbo = (BasicBSONObject) JSON.parse(strJson); //添加异常处理 } catch (Exception e) { log.error("解析json字符串异常,传入的字符串为:" + strJson, e); return false; } DBCollection dBCollection = initCollection(collectionName); dBCollection.insert(dbo); sequoiadbDatasource.close(dBCollection.getSequoiadb()); return true; } public static boolean insertNestJson(String collectionName, String strJson, String strJson2, String nameOfNest) { BSONObject dbo = null; try { dbo = (BasicBSONObject) JSON.parse(strJson); //添加异常处理 } catch (Exception e) { log.error("解析json字符串异常,传入的字符串为:" + strJson, e); return false; } BSONObject dbo2 = null; try { dbo2 = (BasicBSONObject) JSON.parse(strJson2); //添加异常处理 } catch (Exception e) { log.error("解析json字符串异常,传入的字符串为:" + strJson, e); return false; } dbo.put(nameOfNest, dbo2); DBCollection dBCollection = initCollection(collectionName); Object object = dBCollection.insert(dbo); sequoiadbDatasource.close(dBCollection.getSequoiadb()); return true; } //1 public static boolean isExist(String collectionName, String matcher) { if (null == queryOne(collectionName, matcher, matcher, matcher, null)) { return false; } else { return true; } } public static BasicBSONObject str2BSONObject(String jsonString) { return (BasicBSONObject) JSON.parse(jsonString); } public static List exec(String sql) { DBCursor c = null; Sequoiadb seq = null; try { seq = sequoiadbDatasource.getConnection(); c = seq.exec(sql); } catch (BaseException e) { e.printStackTrace(); } catch (InterruptedException ex) { Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex); } if (c != null && c.hasNext()) { List list = new ArrayList(); while (c.hasNext()) { list.add(c.getNext()); } if (seq != null) { sequoiadbDatasource.close(seq); } return list; } else { if (seq != null) { sequoiadbDatasource.close(seq); } return null; } } public static String exeSql(String sql) { return list2String(exec(sql)); } public static String exe(String sql) { return list2String(exec(sql)); } public static boolean execUpdate(String sql) { Sequoiadb seq = null; try { seq = sequoiadbDatasource.getConnection(); seq.execUpdate(sql); sequoiadbDatasource.close(seq); return true; } catch (BaseException e) { sequoiadbDatasource.close(seq); e.printStackTrace(); log.warn(sql, e); return false; } catch (InterruptedException ex) { Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex); return false; } } private static String _query(String cl, String matcher, String selector, String orderBy, String hint, long skipRows, long returnRows) { DBCollection dbc = initCollection(cl); String jsonString = cur2jsonstr(dbc.query(matcher, selector, orderBy, hint, skipRows, returnRows)); sequoiadbDatasource.close(dbc.getSequoiadb()); return jsonString; } public static String query(String cl, String matcher, String selector, String orderBy, String hint, long skipRows, long returnRows) { return _query(cl, matcher, selector, orderBy, hint, skipRows, returnRows); } //matcher test public static String query(String cl, String matcher) { return _query(cl, matcher, null, null, null, 0, 0); } //matcher test public static String query(String cl, String matcher, String selector) { return _query(cl, matcher, selector, null, null, 0, 0); } public static List query4roomids(String cl, String matcher, String selector) { DBCollection dbc = initCollection(cl); DBCursor dBCursor = dbc.query(matcher, selector, null, null, 0, 0); sequoiadbDatasource.close(dbc.getSequoiadb()); return cur2list(dBCursor); } //returnRows test public static String query(String cl, String matcher, long returnRows) { return _query(cl, matcher, null, null, null, 0L, returnRows); } // selector {filed:1} public static String query(String cl, String matcher, String selector, long returnRows) { return _query(cl, matcher, selector, null, null, 0L, returnRows); } // selector {filed:1} public static String query(String cl, String matcher, String selector, long skipRows, long returnRows) { return _query(cl, matcher, selector, null, null, skipRows, returnRows); } // orderBy {filed:1/-1} public static String query(String cl, String matcher, String selector, String orderBy, long returnRows) { return _query(cl, matcher, selector, orderBy, null, 0L, returnRows); } //hint (index) {} public static DBCursor query(String cl, String matcher, String selector, String orderBy, String hint, long returnRows) { DBCollection dbc = initCollection(cl); DBCursor dbcu = dbc.query(matcher, selector, orderBy, hint, 0L, returnRows); sequoiadbDatasource.close(dbc.getSequoiadb()); return dbcu; } public static List query2(String cl, String matcher, String selector, String orderBy, String hint, long returnRows, List<String> list) { DBCollection dbc = initCollection(cl); DBCursor c = dbc.query(matcher, selector, orderBy, hint, 0L, returnRows); if (c != null && c.hasNext()) { while (c.hasNext()) { String str1 = (String) c.getNext().get("acc"); for (int j = 0; j < list.size(); j++) { String str2 = list.get(j); if (str2.equals(str1)) { list.remove(str1); } } } } else { sequoiadbDatasource.close(dbc.getSequoiadb()); return list;//直接返回生成的推荐号 } sequoiadbDatasource.close(dbc.getSequoiadb()); return list; } public static String query(String cl, String matcher, long returnRows, long skipRows) { return _query(cl, matcher, null, null, null, skipRows, returnRows); } //1 public static BSONObject queryOne(String collectionName, String matcher, String selector, String orderBy, String hint) { DBCollection dBCollection = initCollection(collectionName); BSONObject bSONObject = dBCollection.queryOne(str2BSONObject(matcher), str2BSONObject(selector), str2BSONObject(orderBy), str2BSONObject(hint), 0); sequoiadbDatasource.close(dBCollection.getSequoiadb()); return bSONObject; } public static String query(String collectionName) { return _query(collectionName, null, null, null, null, 0, 0); } public static String query(String collectionName, String matcher, String selector, String orderBy, String hint, int limitNum) { return _query(collectionName, matcher, selector, orderBy, hint, limitNum, limitNum); } public static void update(String cl, String matcher, String modifier, String hint) { DBCollection dbc = initCollection(cl); dbc.update(matcher, modifier, hint); sequoiadbDatasource.close(dbc.getSequoiadb()); } public static void update$insert(String cl, String matcher, String modifier, String hint) { DBCollection dbc = initCollection(cl); dbc.update(matcher, modifier, hint); sequoiadbDatasource.close(dbc.getSequoiadb()); } public static void update$unsetAll(String cl, String matcher, String field, String hint) { DBCollection dbc = initCollection(cl); dbc.update(matcher, "{$unset:" + field + ":[]}", hint); // Squ.update("friend", "{}", "{$unset:{label:[]}}", "{}"); sequoiadbDatasource.close(dbc.getSequoiadb()); } public static void update$unset(String cl, String matcher, String modifier, String hint) { DBCollection dbc = initCollection(cl); dbc.update(matcher, "{$unset:" + modifier + "}", hint); // Squ.update("friend", "{}", "{$unset:{label:[33,44,55]}}", "{}"); sequoiadbDatasource.close(dbc.getSequoiadb()); } public static void update$addtoset(String cl, String matcher, String modifier, String hint) { DBCollection dbc = initCollection(cl); dbc.update(matcher, "{$addtoset:" + modifier + "}", hint); // Squ.upsert("friend", "{}", "{$addtoset:{label:[33,44,55]}}", "{}"); sequoiadbDatasource.close(dbc.getSequoiadb()); } public static boolean update(String collectionName, String matcher, String modifier) { DBCollection dbc = initCollection(collectionName); dbc.update(matcher, modifier, null); sequoiadbDatasource.close(dbc.getSequoiadb()); return true; } public static boolean update$set(String collectionName, String matcher, String modifier) { DBCollection dbc = null; try { dbc = initCollection(collectionName); dbc.update(matcher, "{$set:" + modifier + "}", null); sequoiadbDatasource.close(dbc.getSequoiadb()); } catch (Exception e) { log.debug("", e); sequoiadbDatasource.close(dbc.getSequoiadb()); return false; } return true; } /** * 不存在会自动插入新记录 * * @param cl * @param matcher * @param modifier * @param hint */ public static void upsert(String cl, String matcher, String modifier, String hint) { DBCollection dbc = initCollection(cl); BSONObject ma = null; BSONObject mo = null; BSONObject hi = null; if (matcher != null) { ma = (BSONObject) JSON.parse(matcher); } if (modifier != null) { mo = (BSONObject) JSON.parse(modifier); } if (hint != null) { hi = (BSONObject) JSON.parse(hint); } dbc.upsert(ma, mo, hi); sequoiadbDatasource.close(dbc.getSequoiadb()); } public static String strs2json(String... key_val) { if (key_val.length % 2 != 0) { } String strJson = null; if (key_val != null) { StringBuilder sb = new StringBuilder(); sb.append("{"); int i = 0; while (key_val[i] != null) { sb.append(key_val[i]).append(":\"").append(key_val[++i]).append("\""); if (i < key_val.length - 1) { sb.append(","); i++; } else { key_val[i] = null; } } sb.append("}"); strJson = sb.toString(); } return strJson; } public static List cur2list(DBCursor c) { if (c != null && c.hasNext()) { List list = new ArrayList(); while (c.hasNext()) { list.add(c.getNext()); } return list; } return null; } public static String cur2jsonstr(DBCursor c) { String jsonString = ""; if (c != null && c.hasNext()) { while (c.hasNext()) { jsonString = jsonString + (c.getNext().toString()); } c.close(); return jsonString; } return "{}"; } private static String list2String(List list) { if (list != null) { StringBuilder sb = new StringBuilder(); list.stream().forEach((Object s) -> { sb.append(s).append(","); }); return sb.toString(); } else { return null; } } public static List<String> queryList(String collectionName, String string, String acc1_id0, String string0, String string1, int i, int DB_SEARCH_REM_COUNT, List<String> list) { return query2(collectionName, string, string1, string1, string, i, list); } public static void remove(String collectionName, String match) { DBCollection cl = initCollection(collectionName); cl.delete(parse2BasicBSONObject(match)); sequoiadbDatasource.close(cl.getSequoiadb()); } public static JSONObject parse2JSONObject(String json) { return new JSONObject(json); } static class a implements BiConsumer { @Override public void accept(Object t, Object u) { // System.out.println("t=>>"+t); // System.out.println("u=>>"+u); } } public static BasicBSONObject parse2BasicBSONObject(String json) { BasicBSONObject bbo = new BasicBSONObject(); JSONObject obj = parse2JSONObject(json); // obj.forEach(new a() {}); obj.entrySet().forEach(key -> { System.out.println("c=>>" + obj.getString(key.getKey())); System.out.println("c=>>" + obj.optString(key.getKey())); }); return bbo; } public static void main2(String[] args) { parse2BasicBSONObject("{\n" + " \"_id\":\"testuser8\",\n" + " \"UID\":\"testuser\",\n" + " \"CDATE\":\"2015-05-10 17:28:50\",\n" + " \"IDATE\":\"2015-05-10 17:28:50\",\n" + " \"CARDTL\":\"{\\\"HEADPIC\\\":\\\"\\\",\\\"NICK\\\":\\\"1\\\",\\\"name\\\":\\\"2\\\",\\\"skill\\\":\\\"3\\\"}\",\n" + " \"NICK\":\"\",\n" + " \"NID\":\"8\",\n" + " \"HEADPIC\":\"\",\n" + " \"UDATE\":{\n" + " \"$date\":\"2015-05-10T09:28:50.668Z\"\n" + " },\n" + " \"UD\":\"testuser\",\n" + " \"URID\":\"urid\",\n" + " \"ORANGE\":\"uId:个人信息标签2\"\n" + "}"); } }
相关推荐
sequoiadb jdbc 驱动 sequoiadb.jar sequoiadb java 增删改查, 请看博客: http://ian.wang
2. **连接管理**:使用SequoiaDB Java驱动,首先需要建立与数据库的连接。这通常涉及提供SequoiaDB集群的地址、端口,以及认证信息(用户名和密码)。连接可以通过`SequoiaDBClient`类的`connect()`方法实现。 3. *...
在此次性能对比测试中,bankmarkUG使用了Yahoo Cloud Serving Benchmark(YCSB)作为测试平台,目的是为了比较SequoiaDB、Cassandra和MongoDB在不同工作负载场景下的性能表现。YCSB是一个用于评估云存储系统中数据库...
### SequoiaDB_架构指南知识点 #### 一、简介 **SequoiaDB(巨杉数据库)**是一款分布式非关系型文档数据库,专为处理海量非关系型数据设计。其核心特点在于分布式架构、高可用性、高性能及对动态数据类型的强大...
SequoiaDB和Spark是当前金融行业中较为重要的两种技术应用。SequoiaDB是巨杉数据库,是一款新一代的开源NoSQL数据库,由广州巨杉软件开发有限公司(简称巨杉软件)于2011年推出。它能够处理巨大的数据量,并且支持...
本项目为基于Python语言的SequoiaDB巨杉数据库设计源码,集成了59个文件,涵盖32个Python脚本、7个头文件、4个C语言源文件、3个C++源文件、2个Git忽略文件、2个头文件、1个输入文件、1个许可证文件、1个Markdown文件...
SequoiaDB是一款高性能的分布式关系型NoSQL数据库,它的内存模型是其核心架构和优化策略之一。本文将详细介绍SequoiaDB内存模型的三层架构、原理、开发指南、运维指南等知识点。 1. 内存模型架构 SequoiaDB内存模型...
SequoiaDB 架构指南深入解析 SequoiaDB 是一款高性能、大规模分布式数据库系统,专注于处理海量结构化和半结构化数据。本指南旨在详细介绍SequoiaDB的架构和核心特性,帮助读者理解其设计理念,以及如何利用这些...
本文案详细对比 MongoDB,MySQL 和 SequoiaDB 主备同步技术的优缺点; 并详细分析 SequoiaDB 的日志模型,并发同步方案等。
SequoiaDB 是一个分布式的面向文档的 NoSQL 数据库。 引擎: sequoiadb -- SequoiaDB Engine sdbstart -- SequoiaDB Engine start sdbstop -- SequoiaDB Engine stop sdblist -- SequoiaDB Engine list sdbfmp -- ...
分布式数据库SequoiaDB是一种专为大规模数据存储和处理设计的新型数据库系统,它通过将数据分散在多个服务器上,实现了水平扩展的能力,能够处理PB级别的数据量。本篇文章将对SequoiaDB进行初步介绍,包括其核心概念...
SequoiaDB 巨杉数据库是一款开源的金融级分布式关系型数据库。
SequoiaDB巨杉数据库是分布式数据库的代表之一,由两个主要模块构成:数据库存储引擎与数据库实例。这两个模块在功能上相互独立,共同构建了整个SequoiaDB巨杉数据库的运作机制和体系架构。 数据库存储引擎是整个...
测试过程中,sysbench向SequoiaDB集群导入32张包含1亿条记录的表,并对SequoiaSQL-MySQL实例施加压力,每个实例连接本地Coord节点。并发请求的数量逐步提升,每次测试持续5分钟。在SequoiaDB的所有组件保持默认配置...
巨杉数据库SequoiaDB v5.0 发布会直播回放
近期, 知名独立基准测评机构bankmark,针对SequoiaDB、MongoDB以及Cassandra三款NoSQL数据库产品做了性能对比测试。在所有的测试中,三款产品的表现各有千秋。 报道详情:http://code.csdn.net/news/2823026
本视频将学习MySQL向SequoiaDB批量迁移的方法,适合IT互联网从业者、计算机相关专业学生