riak数据库初学1-set bucket
项目中需要用到riak数据库,大致学习了一下,安装并进行集群。
环境:ubuntu10.10 64bit ,jdk1.6.0_24,erlang5.8,riak-search_0.14.0-1_amd64.deb
1安装并修改配置文件
修改当前节点的配置文件
sudo gedit /etc/riaksearch/app.config
将{riak_web_ip, "127.0.0.1"}改为{riak_web_ip, "192.168.1.10"}//192.168.1.10是我本地ip
注:此配置文件中有几个地方需要配置ip,riak支持http接口、https接口,客户端请求时需要什么接口配置哪个接口即可,我这里用的是http
%% HTTPS interface will bind.
{https, [{ "127.0.0.1", 8098 }]},//这里是https
下面还有一个ip 地址,此地址为配置不同的存储后端,设为本地ip
%% pb_ip is the IP address that the Riak Protocol Buffers interface
%% will bind to. If this is undefined, the interface will not run.
{pb_ip, "192.168.1.100" },
修改节点参数配置文件
sudo gedit /etc/riaksearch/vm.args,修改-name 参数的值,即将 -name riak@127.0.0.1 改为-name riak@192.168.1.10
2启动riaksearch
riaksearch console启动riak并运行控制台,可查看到启动信息
riaksearch start后台启动无任何信息
riaksearch ping查看是否启动成功pong成功pang失败
3加入集群
riaksearch-admin join riak@192.168.1.101
查看集群内结点:erl内输入 nodes().
4建立一个bucket
启动一个客户端{ok,Client}=riak:local_client().
建立一个bucket
Client:set_bucket(<<"regions">>,[{precommit, [{struct,[{<<"mod">>,<<"riak_search_kv_hook">>}, {<<"fun">>,<<"precommit">>}]}]}]).
在建立bucket的时候set了一些属性,大致看了一下官方文档,有很多还不明白的地方
Available properties:
* n_val (integer > 0) – the number of replicas for objects in this bucket
* allow_mult (true or false) – whether to allow sibling objects to be created (concurrent updates)
* last_write_wins (true or false) – whether to ignore object history (vector clock) when writing
* precommit – precommit hooks
* postcommit – postcommit hooks
* r, w, dw, rw – default quorum values for operations on keys in the bucket. Valid values are:
o "all" – all nodes must respond
o "quorum" – (n_val/2) + 1 nodes must respond. This is the default.
o "one" – equivalent to 1
o Any integer – must be less than or equal to n_val
* backend – when using riak_kv_multi_backend, which named backend to use for the bucket
Overview #
Pre- and Post- Commit hooks are invoked before or after a riak_object is persisted and can greatly enhance the functionality of any application. Commit hooks can:
1. allow a write to occur with an unmodified object
2. modify the object
3. Fail the update and prevent any modifications
Post-commit hooks are notified after the fact and should not modify the riak_object. Updating riak_objects in post-commit hooks can cause nasty feedback loops which will wedge the hook into an infinite cycle unless the hook functions are carefully written to detect and short-circuit such cycles.
Pre- and post-commit hooks are defined on a per-bucket basis and are stored in the target bucket’s properties. They are run once per successful response to the client.
Configuration #
Configuring either pre- or post-commit hooks is very easy. Simply add a reference to your hook function to the list of functions stored in the correct bucket property. Pre-commit hooks are stored under the bucket property precommit. Post-commit hooks use the bucket property postcommit.
Pre-commit hooks can be implemented as named Javascript functions or as Erlang functions. The configuration for each is given below:
Javascript: {"name": "Foo.beforeWrite"}
Erlang: {"mod": "foo", "fun": "beforeWrite"}
Po