Centos6.5裏安裝Erlang 並安裝riak

一.Erlang安装:

1 首先进入www.erlang.org 下载页面,下载otp_src_17.5.tar.gz. IT网,http://www.it.net.cn

2 解压缩:tar -xzvf otp_src_17.5.tar.gz Linux学习,http:// linux.it.net.cn

3 进入解压缩后的文件夹:cd otp_src_17.5.tar.gz

4 如果直接运行./configure,会提示没有curses库,所以首先还得安装这个库:yum install ncurses-devel,运行此命令需要具备root权限。

5 运行./congigure命令。

6 运行make命令。

7 运行make install命令。

 

安装成功后,在命令行输入erl,则erlang shell便会运行起来。至此,安装完全成功。

yum install gcc


riak安装:

http://docs.basho.com/riak/1.3.2/tutorials/installation/Installing-on-RHEL-and-CentOS/

备注:(不知道有什么卵用)

Centos6.5裏安裝Erlang 並安裝riak
Centos6.5裏安裝Erlang 並安裝riak
设置ulimit:
echo "session required pam_limits.so" >> /etc/pam.d/common-session
echo "ulimit -SHn 51200" >> /etc/profile
source /etc/profile
ulimit -n
View Code

 

修改成局域网可以访问:
/etc/riak/app.config
如下
{http, [ {"192.168.1.117", 8098 } ]},

/etc/rc.d/init.d/riak restart

vim /etc/riak/vm.args

vim /etc/riak/app.config 


sudo yum install http://yum.basho.com/gpg/basho-release-6-1.noarch.rpm
sudo yum install riak
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8098 -j ACCEPT
vim /etc/sysconfig/iptables
/etc/init.d/iptables restart
/etc/init.d/iptables status

Centos6.5裏安裝Erlang 並安裝riak
 

Centos6.5裏安裝Erlang 並安裝riak

其他参考:

http://docs.basho.com/riak/2.0.0/ops/building/installing/rhel-centos/

https://github.com/basho/riak

http://www.oschina.net/p/riak/

http://www.ibm.com/developerworks/cn/opensource/os-riak1/

http://www.ibm.com/developerworks/cn/opensource/os-riak2/

http://blog.csdn.net/freewebsys/article/details/12605

http://www.xuebuyuan.com/1252471.html

http://docs.basho.com/riak/1.3.2/tutorials/installation/Installing-on-RHEL-and-CentOS/ 

http://www.lupaworld.com/thread-61933-1-1.html

http://blog.csdn.net/freewebsys/article/details/12617379

客户端连接测试:(https://github.com/super-d2/riak_demo)

 {pb, [ {"192.168.1.117", 8087 } ]}

pom.xml

Centos6.5裏安裝Erlang 並安裝riak
Centos6.5裏安裝Erlang 並安裝riak
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>riak_demo</groupId>
    <artifactId>riak_demo</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.basho.riak</groupId>
            <artifactId>riak-client</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.basho.riak.protobuf</groupId>
            <artifactId>riak-pb</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>

</project>
View Code

UserInfo.java

Centos6.5裏安裝Erlang 並安裝riak
Centos6.5裏安裝Erlang 並安裝riak
package com.demo;

/**
 * 用户信息.
 */
public class UserInfo {
    private String uid;
    private String name;
    private String city;
    private String nickName;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "UserInfo [uid=" + uid + ", name=" + name + ", city=" + city
                + ", nickName=" + nickName + "]";
    }

}
View Code

ClientTest.java

Centos6.5裏安裝Erlang 並安裝riak
Centos6.5裏安裝Erlang 並安裝riak
package com.demo;

import java.io.IOException;
import com.basho.riak.client.IRiakClient;
import com.basho.riak.client.RiakException;
import com.basho.riak.client.RiakFactory;
import com.basho.riak.client.RiakRetryFailedException;
import com.basho.riak.client.bucket.Bucket;

public class ClientTest {

    public static void main(String[] args) throws IOException {

        IRiakClient client = null;
        try {// 使用pbc方式连接,而不是http,在/etc/riak/app.config
            client = RiakFactory.pbcClient("192.168.1.117", 8087);
        } catch (RiakException e) {
            e.printStackTrace();
        }
        // 显示.
        System.out.println(client);
        Bucket myBucket = null;
        String bucketName = "userInfo";
        try {
            myBucket = client.fetchBucket(bucketName).execute();
            if (myBucket == null) {
                myBucket = client.createBucket(bucketName).execute();
            }
        } catch (RiakRetryFailedException e) {
            e.printStackTrace();
        }
        // ################保存数据 .
        UserInfo info = new UserInfo();
        info.setUid("001");
        info.setName("张三");
        info.setCity("北京");
        try {
            myBucket.store(info.getUid(), info).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ################查询数据.
        UserInfo fetchedUserInfo = null;
        try {
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            System.out.println(fetchedUserInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // ################修改数据.
        try {
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            fetchedUserInfo.setName("李四");
            fetchedUserInfo.setNickName("老李");
            myBucket.store(info.getUid(), info).execute();
            // 保存 新数据
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            System.out.println("新数据:" + fetchedUserInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ################删除数据.
        try {
            myBucket.delete("001").execute();
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            System.out.println("删除收数据." + fetchedUserInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关闭。
        client.shutdown();

    }

}
View Code

运行:

Centos6.5裏安裝Erlang 並安裝riak
 

代码分析

在Riak当中,可以简单的把Bucket理解成一个表。
首先要创建一个这样的Bucket,然后把数据按照key放进去。

数据类型可以是字符串,基本类型,或是对象(如UserInfo)。

每次操作的时候都是通过执行Bucket的方法执行达到CRUD的操作。

StoreObject<IRiakObject> store(String key, byte[] value);  
StoreObject<IRiakObject> store(String key, String value);  
<T> StoreObject<T> store(T o);  
<T> StoreObject<T> store(String key, T o);  
FetchObject<IRiakObject> fetch(String key);  
<T> FetchObject<T> fetch(String key, Class<T> type);  
<T> FetchObject<T> fetch(T o);  
MultiFetchObject<IRiakObject> multiFetch(String[] keys);  
<T> MultiFetchObject<T> multiFetch(List<String> keys, Class<T> type);  
<T> MultiFetchObject<T> multiFetch(List<T> o);  
CounterObject counter(String counter);  
<T> DeleteObject delete(T o);  
DeleteObject delete(String key);  
StreamingOperation<String> keys() throws RiakException;  
<T> FetchIndex<T> fetchIndex(RiakIndex<T> index);  

总结

java通过使用Protocol Buffers方式调用Riak服务,直接操作对象进行CRUD。

有了这些,可以做一个简单的评论系统了。评论系统上面不需要事物,并且数量会随着业务增长,使用Rick可以平稳的进行扩展。

这个只是简单的,对Rick服务进行CRUD。最没有用到其他功能,同时没有关于key的设计。

Rick的其他功能,以后继续研究。

相关分布式理论:

http://freestorm.org/2015/04/25/Riak%E7%9A%84%E5%88%86%E5%B8%83%E5%BC%8F%E6%95%B0%E6%8D%AE%E5%BA%93%E6%A8%A1%E5%9E%8B.html

http://segmentfault.com/a/1192802797

java客户端调用实例:

pom.xml:

Centos6.5裏安裝Erlang 並安裝riak
Centos6.5裏安裝Erlang 並安裝riak
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>riak_demo</groupId>
    <artifactId>riak_demo</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.basho.riak</groupId>
            <artifactId>riak-client</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.basho.riak.protobuf</groupId>
            <artifactId>riak-pb</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>

</project>
View Code

UserInfo:

Centos6.5裏安裝Erlang 並安裝riak
Centos6.5裏安裝Erlang 並安裝riak
package com.demo;

/**
 * 用户信息.
 */
public class UserInfo {
    private String uid;
    private String name;
    private String city;
    private String nickName;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "UserInfo [uid=" + uid + ", name=" + name + ", city=" + city
                + ", nickName=" + nickName + "]";
    }

}
View Code

ClientTest:

Centos6.5裏安裝Erlang 並安裝riak
Centos6.5裏安裝Erlang 並安裝riak
package com.demo;

import java.io.IOException;
import com.basho.riak.client.IRiakClient;
import com.basho.riak.client.RiakException;
import com.basho.riak.client.RiakFactory;
import com.basho.riak.client.RiakRetryFailedException;
import com.basho.riak.client.bucket.Bucket;

public class ClientTest {

    public static void main(String[] args) throws IOException {

        IRiakClient client = null;
        try {// 使用pbc方式连接,而不是http,在/etc/riak/app.config
            client = RiakFactory.pbcClient("192.168.1.", 8087);
        } catch (RiakException e) {
            e.printStackTrace();
        }
        // 显示.
        System.out.println(client);
        Bucket myBucket = null;
        String bucketName = "userInfo";
        try {
            myBucket = client.fetchBucket(bucketName).execute();
            if (myBucket == null) {
                myBucket = client.createBucket(bucketName).execute();
            }
        } catch (RiakRetryFailedException e) {
            e.printStackTrace();
        }
        // ################保存数据 .
        UserInfo info = new UserInfo();
        info.setUid("001");
        info.setName("张三");
        info.setCity("北京");
        try {
            myBucket.store(info.getUid(), info).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ################查询数据.
        UserInfo fetchedUserInfo = null;
        try {
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            System.out.println(fetchedUserInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // ################修改数据.
        try {
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            fetchedUserInfo.setName("李四");
            fetchedUserInfo.setNickName("老李");
            myBucket.store(info.getUid(), info).execute();
            // 保存 新数据
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            System.out.println("新数据:" + fetchedUserInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ################删除数据.
        try {
            myBucket.delete("001").execute();
            fetchedUserInfo = myBucket.fetch("001", UserInfo.class).execute();
            System.out.println("删除收数据." + fetchedUserInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关闭。
        client.shutdown();

    }

}
View Code

 

Database error: [Table 'ac_search_cache' is marked as crashed and should be repaired]

SELECT * FROM ac_search_cache WHERE hash = '510252cc68189fb32a071414641e471189f44719' LIMIT 1;