couchbeam应用 博客分类: couchdb CouchDBErlangjsonSecurity
程序员文章站
2024-03-23 17:12:28
...
1、couchbeam是基于erlang的couchdb客户端.couchbeam是一个otp应用程序,所以在使用前必需先启动couchbeam
couchbeam:start().
2、创建数据库的连接
%% 如需要权限,請看下方Option的basic_auth Host = "localhost", Port = 5984, Prefix = "", Options = [], S = couchbeam:server_connection(Host, Port, Prefix, Options). %% @spec server_connection(Host::string(), Port::integer(), %% Prefix::string(), Options::optionList()) %% -> Server::server() %% optionList() = [option()] %% option() = %% {is_ssl, boolean()} | %% {ssl_options, [SSLOpt]} | %% {pool_name, atom()} | %% {proxy_host, string()} | %% {proxy_port, integer()} | %% {proxy_user, string()} | %% {proxy_password, string()} | %% {basic_auth, {username(), password()}} | %% {cookie, string()} | %% {oauth, oauthOptions()} %% %% username() = string() %% password() = string() %% SSLOpt = term() %% oauthOptions() = [oauth()] %% oauth() = %% {consumer_key, string()} | %% {token, string()} | %% {token_secret, string()} | %% {consumer_secret, string()} | %% {signature_method, string()} %%
3、打开或创建一个数据库
ptions = [], {ok, Db} = couchbeam:open_db(Server, "testdb", Options). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Options = [], {ok, Db} = couchbeam:create_db(Server, "testdb", Options).
4. 创建一个文档
Doc = {[ {<<"_id">>, <<"test">>}, {<<"content">>, <<"some text">>} ]}. {ok, Doc1} = couchbeam:save_doc(Db, Doc).
5. 检索文件
{ok, Doc2} = couchbeam:open_doc(Db, "test"). %%If you want a specific revision: Rev = couchbeam_doc:get_rev(Doc1), Options = [{rev, Rev}], {ok, Doc3} = couchbeam:open_doc(Db, "test", Options).
6. 取得所有文档
Options = [{include_docs, "true"}], {ok, AllDocs} = couchbeam:all_docs(Db, Options).
或者:
{ok, Results} = couchbeam_view:fetch(AllDocs).
7. Couchdb views - Map/Reduce
Options = [], DesignNam = "designname", ViewName = "viewname", {ok, View} = couchbeam:view(Db, {DesignNam, ViewName}, Options).
8. Put,Fetch and Delete documents attachments
%% 上传一个附件 DocID = "test", AttName = "test.txt", Att = "some content I want to attach", Options = [] {ok, _Result} = couchbeam:put_attachment(Db, DocId, AttName, Att, Options). %% 取出一个附件 {ok Att1} = couchbeam:fetch_attachment(Db, DocId, AttName). %% 删除一个附件 {ok, Doc4} = couchbeam:open_doc(Db, DocID), ok = couchbeam:delete_attachment(Db, Doc4, AttName).
9. Changes
Options = [], {ok, Changes} = couchbeam:changes_wait_once(Db, Options).
10、如想修改数据库的admins/readers,在couchbeam没找到现成方法,望知道的留言相告.简单实现了下
%% add auth to database _json = "{\"admins\":{\"names\":[\"user1\"],\"roles\":[]},\"readers\":{\"names\":[\"user2\"],\"roles\":[]}}", Result = couchbeam:db_request(put, "http://127.0.0.1:5984/d2/_security",[], Options,[],erlang:list_to_binary(_json)),
推荐阅读
-
couchbeam应用 博客分类: couchdb CouchDBErlangjsonSecurity
-
对redis数据持久化的一些想法 博客分类: redis RedisOSTokyoCabinet应用服务器多线程
-
基于HTTP协议的轻量级开源简单队列服务:HTTPSQS 博客分类: 互联网服务端 应用服务器金山PHPTokyoCabinetLinux
-
体验TokyoCabinet和TokyoTyrant 博客分类: 持久化 TokyoCabinet应用服务器配置管理数据结构多线程
-
【备份】对redis数据持久化的一些想法 博客分类: Nosql RedisOSTokyoCabinet应用服务器多线程
-
tokyocabinet与lucene在搜索上的应用 博客分类: java TokyoCabinetlucene搜索引擎应用服务器框架
-
本人从今天开始开发移动应用 博客分类: 大杂烩 中国移动
-
Composite模式及其在JSF中的应用 博客分类: JSF composite模式及其在jsf中的应用
-
Composite模式及其在JSF中的应用 博客分类: JSF composite模式及其在jsf中的应用
-
Java遍历的区别及应用 博客分类: 数据结构 遍历Java