玩个JSONDB吧
有人玩NOSQL,有人玩内存数据库,哥来玩个JSONDB。曾经看到一个客户端的JSONDB,可惜作者好久没更新了,我手头刚好需要一个存取JSON字符串的需求。利用JSONlib自己弄了个JAVA版弱弱的JSONDB:
package com.linkage.cmgrx.experiment;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.util.Iterator;import net.sf.json.JSONArray;import net.sf.json.JSONObject;/** * JSON数据库,全局单例 * @author zhangxf * @since 2010-10-09 * @version 1.0 */public class JSONDB {/** * 完整的文件路径 */private String fullFilePath;/** * 所有数据 */private JSONArray allData=new JSONArray();/** * 构造单例 */private static JSONDB instance;private JSONDB(String fullFilePath){this.fullFilePath=fullFilePath;this.loadData();//尝试加载现有数据}/** * 获取一个JSON数据库实例 * @param fullFilePath完整的文件路径 * @return */public static JSONDB getInstance(String fullFilePath){if(instance==null){instance=new JSONDB(fullFilePath);}return instance;}/** * 插入一个JSONObject * @param jsonObj */public void insert(JSONObject jsonObj){this.allData.add(jsonObj);}/** * 插入一个JSONArray * @param jsonArr */public void insert(JSONArray jsonArr){Iterator iter=jsonArr.iterator();while(iter.hasNext()){JSONObject jsonObj=(JSONObject) iter.next();this.insert(jsonObj);}}/** * 插入JSON字符串,字符串的格式必须符合标准的JSON格式 * 否则会抛出异常 * @param jsonStr */public void insert(String jsonStr){this.allData.add(JSONObject.fromObject(jsonStr));}/** * 删除指定位置的JSON对象 * @param index */public void delete(int index){this.allData.remove(index);}/** * 根据JSON字符串删除对象,jsonStr包含的是过滤条件 * @param jsonStr */public void delete(String jsonStr){JSONObject jsonObj=JSONObject.fromObject(jsonStr);int len=jsonObj.size();Iterator iter2=this.allData.iterator();while(iter2.hasNext()){JSONObject dataObj=(JSONObject) iter2.next();int counter=0;Iterator iter=jsonObj.keys();while(iter.hasNext()){String key=(String) iter.next();Object val=jsonObj.get(key);if(!dataObj.containsKey(key)){break;}if(dataObj.get(key).equals(val)){counter++;}}//所有条件都匹配,删除此JSON对象if(counter==len){iter2.remove();}counter=0;}}/** * 截断 */public void truncate(){this.allData=null;this.allData=new JSONArray();}/** * 更新 * @param jsonObj */public void update(JSONObject jsonObj){}/** * 根据JSON字符串更新JSON对象,jsonStr为条件 * @param jsonStr */public void update(String jsonStr){JSONObject jsonObj=JSONObject.fromObject(jsonStr);}/** * 根据jsonStr查询 * @param jsonStr * @return */public JSONArray select(String jsonStr){JSONArray result=new JSONArray();JSONObject jsonObj=JSONObject.fromObject(jsonStr);return result;}/** * 写入文件 * @param append是否使用追加方式 */public void commit(Boolean append){try{File file=new File(this.fullFilePath);if(!file.exists()){file.createNewFile();}System.out.println(file);OutputStream os=new FileOutputStream(file);OutputStreamWriter ow=new OutputStreamWriter(os,"UTF-8");PrintWriter pr=new PrintWriter(ow);pr.println(this.allData.toString());pr.close();}catch(Exception e){e.printStackTrace();}}/** * 加载文件 */private void loadData(){try{File file=new File(this.fullFilePath);if(!file.exists()){System.out.println("读取文件..."+this.fullFilePath+"失败,指定路径下的文件不存在。"); }else{ if(file.canRead()){InputStream in=new FileInputStream(file);InputStreamReader ir=new InputStreamReader(in,"UTF-8");BufferedReader br=new BufferedReader(ir);StringBuffer sb=new StringBuffer();String line="";while((line=br.readLine())!=null){sb.append(line);}this.allData=JSONArray.fromObject(sb.toString());br.close();System.out.println("读取文件["+this.fullFilePath+"]成功结束......"); }else{ System.out.println("读取文件["+this.fullFilePath+"]失败,文件无法读取。"); }}}catch(Exception e){e.printStackTrace();}}public String getFullFilePath() {return fullFilePath;}public JSONArray getAllData() {return allData;}public static void main(String[] args){JSONDB jsonDb=JSONDB.getInstance("E:\\Program Files\\Apache Software Foundation\\Tomcat-5.5.23\\webapps\\cmgrx\\modules\\BOMC模型.plan");//jsonDb.insert("{'name':'大漠','age':'你猜'}");//jsonDb.delete("{'name':'大漠1'}");System.out.println(jsonDb.getAllData().toString());}} 用法示例:
JSONDB jsonDb=JSONDB.getInstance("C:\\test.json"); jsonDb.delete("{'targetId':'"+targetId+"'}");System.out.println("删除指标>"+targetId);JSONObject jsonObj=JSONObject.fromObject(json);jsonDb.insert(jsonObj);jsonDb.commit(false); 效果截图:
<img alt="">
http://dl.iteye.com/upload/attachment/327825/3cc35289-5245-3da5-bd14-7ce9db6e28ac.jpg
【注意】:
这个只是符合我手头这个简单的需求,没有考虑多线程、事务等等复杂的东东。
如果你刚好也有个简单的东东,不想写SQL和数据库交互的话,可以试着弄弄看,或许有点意思。
【前景】:
如果利用JSON直接和文件进行交互的话,可以直接和Lucene结合起来,直接对.json文件进行索引。这是我目前的想法。有想法可以给我mail,一起研究研究。
json-lib的包附在后面。
页:
[1]