`
liujiawinds
  • 浏览: 131764 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

couchDB初级应用实例

阅读更多

在安装好couchDB之后,具体安装细节可以参考上一篇

 

在google code 上面下载最新版本的jdbc驱动,jcouchDB及其所依赖的一些jar包。

commons-beanutils.jar
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-io-1.3.1.jar
commons-logging-1.1.jar
easymock-2.3.jar
hamcrest-all-1.1.jar
junit-4.4.jar
log4j-1.2.14.jar
svenson-1.2.8.jar

 

引入这些jar包到项目里面去,然后开始动手写增删改查。。。

 

过程出了一些问题,因为过程无法重现,只能把evernote里面写的东西贴上了。

 

  • 在attachment中需要添加一个digest属性,所以自己重新下了源码打成了jar包
  • 在attachment中的getData方法是无实际意义的,使用了也获取不到数据。
路径如果为:
file:/F:/erp workspace/erp/WebRoot/WEB-INF/cfg/content-type.properties获取不到配置文件,
// Properties properties = Resources.getResourceAsProperties(getConfigFilePath());
改用
      Properties properties = Resources.getUrlAsPropertiesgetConfigFilePath());
解决
 
DesinDocument在createDocument的时候出错,error code 401,没有足够的权限。
改为使用  BaseDocument,正常
 
 
附上自己测试类代码,仅供借鉴,无法保证程序的性能:
 
package com.erp.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.log4j.Logger;
import org.jcouchdb.db.Database;
import org.jcouchdb.document.BaseDocument;
import org.jcouchdb.document.Document;
import org.jcouchdb.document.ValueRow;
import org.jcouchdb.document.ViewResult;


/**
 * @author Liu Jia
 * @version 1.0.0
 */
public class CouchTest {

    protected static Logger log = Logger.getLogger(CouchTest.class);
    private final static String CONTENT_TYPE_CONFIG_PATH = "cfg/content-type.properties";
    private static Database db = new Database("localhost", "couchdb_test");
    private static String path ="D:/software/jdk-7-windows-i586.zip";


    
    /**
     * 功能: 通过文件对象获取字节数组
     *
     * @param file 文件对象
     * @return 字节数组
     */
    public static byte[] getBytesFromFile(File file) {
        if (file == null) {
            return null;
        }
        try {
            FileInputStream stream = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream((int) file.length());
            byte[] byteArray = new byte[(int) file.length()];
            for (int n; (n = stream.read(byteArray)) != -1;) {
                out.write(byteArray, 0, n);
            }
            stream.close();
            out.close();
            return out.toByteArray();
        } catch (IOException e) {
        }
        return null;
    }

    
    /**
     * 功能: 把字节数组生成文件
     *
     * @param byteArray 字节数组
     * @param outputFile 输出文件名称,包括后缀名
     * @return 文件对象
     */
    public static File getFileFromBytes(byte[] byteArray, String outputFile) {
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File(outputFile);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(byteArray);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }

    /**
     * 功能: 根据docId获取文档
     * 
     * @param docId
     *            文档ID
     * @return 文档
     */
    public static Document getDocument(String docId) {
        BaseDocument doc = db.getDocument(BaseDocument.class, docId);
        return doc;
    }

    /**
     * 功能: 获取所有的值行,值行可以用于提取文档的ID
     * 
     * @return 值行列表
     */
    @SuppressWarnings("rawtypes")
    public static List<ValueRow<Map>> getAllValueRow() {
        ViewResult<Map> resultList = db.listDocuments(null, null);
        return resultList.getRows();
    }

    /**
     * 功能: 创建附件
     * 
     */
    @SuppressWarnings("rawtypes")
    public static void createAttachment() {
        List<ValueRow<Map>> resultList = getAllValueRow();
        List<String> idList = new ArrayList<String>();
        for (ValueRow<Map> row : resultList) {
            idList.add(row.getId());
        }
        path = "D:/software/jdk-7-windows-i586.zip";
        File file = new File(path);
        System.out.println("==================="+file.length());
        byte[] data = getBytesFromFile(file);
        String contentType = getContentType(file.getName());
        for (String docId : idList) {
            BaseDocument document = db.getDocument(BaseDocument.class, docId);
            db.createAttachment(docId, document.getRevision(), file.getName(), contentType, data);
        }
    }

    /**
     * 功能: 根据文档的后缀获取该文档的内容类型
     * 
     * @param suffix 后缀
     * @return 内容类型
     */
    public static String getContentType(String name) {
        String suffix = name.substring(name.indexOf("."));
        String contentType = "";
        try {
            Properties properties = Resources.getUrlAsProperties(getConfigFilePath());
            contentType = properties.getProperty(suffix, "text/plain");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contentType;
    }


    /**
     * 功能: 获取配置文件的
     * 
     * @return
     */
    public static String getConfigFilePath() {
        String path = CouchTest.class.getClassLoader().getResource("").toString();
        path = path.replace("classes/", "");
        path += CONTENT_TYPE_CONFIG_PATH;
        try {
            path = URLDecoder.decode(path, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(path);
        return path;
    }

    /**
    
     *  
     * @param userId
     */
    public static void createDocumentByUserId(String userId) {
        BaseDocument document = new BaseDocument();
        document.setId(userId);
        document.setProperty("privilege", "111 010");
        db.createDocument(document);
    }

    public static void main(String[] args) {
        createAttachment();  
        long start_time = System.currentTimeMillis();
        byte[] data = db.getAttachment("f3b34e9592ef0650544554de45003859", path);
        getFileFromBytes(data, "erp123.zip");
        long end_time = System.currentTimeMillis();
        System.out.println(end_time-start_time);
//        log.warn("===============" + data);
//        createDocumentByUserId("1");
     }
}
 
0
0
分享到:
评论
1 楼 winney117 2014-04-11  
您好,我的import org.apache.ibatis.io.Resources;  无法解析,应该下载什么jar包呢?

相关推荐

    CouchDB

    CouchDB

    CouchDB权威指南(带详细目录)PDF

    三位CouchDB的开发者向你展示了如何以独立应用框架的形式来使用这一面向文档的数据库,以及如何使用它来构建高容量、分布式的应用。 CouchDB简洁的存储,处理,以及读取数据的模型,让它成为了构建处理海量松散结构...

    Beginning CouchDB.pdf

    Beginning CouchDB.pdf

    CouchDB20 分钟入门

    学习couchDB 的入门教程

    Python-CouchApp是一个开发使用CouchDB的Web应用的小型框架

    CouchApp 是一个开发使用 CouchDB 的 Web 应用的小型框架。它的主要功能是可以把一个文件系统的目录转换成 CouchDB 中的一个设计文档。

    couchdb源码

    couchdb源码 

    Fabric 1.4基于couchdb环境搭建

    Fabric 1.4基于couchdb环境搭建步骤,以及基于couchdb的区块链多字段数据查询

    CouchDB独立博客sofa-CouchDB.zip

    sofa-CouchDB 是 CouchDB 的独立博客,使用 CouchDB 的书来做主要内容,这方便了所有用来在这博客上交流他们的想法,并且里面提供了很多帮助指导,这都是 HTML,Javascript 和 CouchDB 的结晶。目前支持任何人在上面...

    CouchDB.The.Definitive.Guide

    This book introduces you to Apache CouchDB, a document-oriented database that offers a different way to model your data. CouchDB is a schema-free database, designed to work with applications that ...

    CouchDB,Python

    Python CouchDB模块,使用:直接将其中的couchdb文件夹复制到Python27\Lib文件夹下,即可使用import couchdb按照文档进行后续开发。

    Scaling.CouchDB(第1版)

    中文名: Scaling CouchDB (第1版) 原名: Scaling CouchDB: Replication, Clustering, and Administration 作者: Bradley Holt 资源格式: PDF 版本: 英文文字版/更新EPUB版本 出版社: O'Reilly书号: 978-1-4493-0343-...

    Apache-CouchDB.zip

    CouchDB 是一个开源的面向文档的数据库管理系统,可以通过 RESTful JavaScript Object Notation (JSON) API 访问。术语 “Couch” 是 “Cluster Of Unreliable Commodity Hardware” 的首字母缩写,它反映了 CouchDB...

    apache-couchdb-2.3.1.msi

    CouchDB 是一个开源的面向文档的数据库管理系统,可以通过 RESTful JavaScript Object Notation (JSON) API 访问。术语 “Couch” 是 “Cluster Of Unreliable Commodity Hardware” 的首字母缩写,它反映了 CouchDB...

    Apress.Beginning.CouchDB.Dec.2009.pdf

    Apache CouchDB is an exciting database management system that is growing in popularity each day. This book introduces you to CouchDB, guiding you through its relatively short history and what makes it...

    awesome-couchdb, CouchDB精选元资源&最佳实践列表.zip

    awesome-couchdb, CouchDB精选元资源&最佳实践列表 出色的CouchDB 面向CouchDB的curated元资源&最佳实践列表。是一个面向文档的面向服务的数据库,它同步。欢迎请求请求。电子邮件内容模式&最佳实践。Map/Reduce连接...

    apache-couchdb-2.3.1.zip

    现在couchdb官网2.3.1这个版本不给下载了,需要的可以下载一下。解压缩就可以使用 所需积分为0

    couchdb-2.3.0.msi

    couchdb-2.3.0.msi,在window下的安装包,下载后就可以直接运行。详细见https://blog.csdn.net/qq_30505673/article/details/85039731

    couchdb富查询示例链码

    couchdb富查询示例链码

    abdera-couchdb-1.1.jar

    标签:abdera-couchdb-1.1.jar,abdera,couchdb,1.1,jar包下载,依赖包

    CouchDB:权威指南(第一版)CouchDB: The Definitive Guide (1st Edition)

    CouchDb的三位创建者向您展示了如何将此面向文档的数据库用作独立的应用程序框架或用于大量分布式应用程序。

Global site tag (gtag.js) - Google Analytics