Riak HTTP API Commands/Services

We can access RIAK database through Protocol Buffer API and HTTP API. In this post, I am going to show how we can use HTTP API to check the data, bucket properties, create index and and query the data.

Replace RIAK_SERVER_NAME with the appropriate Riak server name

To view the statistics data:
http://RIAK_SERVER_NAME:8098/stats

List out all the buckets:
http://RIAK_SERVER_NAME:8098/types/default/buckets?buckets=true

Bucket Keys:
http://RIAK_SERVER_NAME:8098/types/default/buckets/BUCKET_NAME/keys?keys=true

Replace BUCKET_NAME with the actual bucket name

Check the bucket properties:
http://RIAK_SERVER_NAME:8098/types/default/buckets/BUCKET_NAME/props

Replace BUCKET_NAME with the actual bucket name

Create an index:

Each bucket should be assigned with a search index for querying purpose. Run the below command to create the index.

curl –XPUT http://RIAK_SERVER_NAME:8098/search/index/INDEX_NAME

Install CURL first then run the above command or otherwise install the Advanced Rest Client Chrome Plugin. then access http://RIAK_SERVER_NAME:8098/search/index/INDEX_NAME with PUT method.

To check whether the index created or not is by accessing the below service and check the response.

http://RIAK_SERVER_NAME:8098/search/index/INDEX_NAME

Assign the index to a bucket:

curl -XPUT http://RIAK_SERVER_NAME:8098/buckets/BUCKET_NAME/props -H  ‘Content-Type:application/json’  -d  ‘{“props”:{“search_index”:”INDEX_NAME”}}’

Delete an object with key:
curl -X “DELETE” http://RIAK_SERVER_NAME:8098/buckets/BUCKET_NAME/keys/KEY

Replace BUCKET_NAME and INDEX_NAME appropriately

To check whether the index is assigned to the bucket or not is by accessing the below service and check the response.

http://RIAK_SERVER_NAME:8098/types/default/buckets/BUCKET_NAME/props

Check the response and make sure that the search_index property value is your INDEX_NAME

Retrieve the data with key:

http://RIAK_SERVER_NAME:8098/types/default/buckets/BUCKET_NAME/keys/KEY_VALUE

Replace BUCKET_NAME and KEY_VALUE appropriately.

Query with search index:

Assume that you have stored the below model in RIAK database with the key as customer id or any other unique id.


import java.io.Serializable;

public class RiakCustomerData implements Serializable {

    private static final long serialVersionUID = 7818749365067437253L;
    
    private String customerId_s;

    private long timestamp_l;

    private String firstName_s;

    private String lastName_s;

    private String orderData_s;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RiakCustomerData that = (RiakCustomerData) o;

        if (timestamp_l != that.timestamp_l) return false;
        if (customerId_s != null ? !customerId_s.equals(that.customerId_s) : that.customerId_s != null) return false;
        if (firstName_s != null ? !firstName_s.equals(that.firstName_s) : that.firstName_s != null) return false;
        if (lastName_s != null ? !lastName_s.equals(that.lastName_s) : that.lastName_s != null) return false;
        return !(orderData_s != null ? !orderData_s.equals(that.orderData_s) : that.orderData_s != null);

    }

    @Override
    public int hashCode() {
        int result = customerId_s != null ? customerId_s.hashCode() : 0;
        result = 31 * result + (int) (timestamp_l ^ (timestamp_l >>> 32));
        result = 31 * result + (firstName_s != null ? firstName_s.hashCode() : 0);
        result = 31 * result + (lastName_s != null ? lastName_s.hashCode() : 0);
        result = 31 * result + (orderData_s != null ? orderData_s.hashCode() : 0);
        return result;
    }
}

Now you have a scenario and you want to list out all the customers who has the name as John. So to do this, access the below service

http://RIAK_SERVER_NAME:8098/search/query/INDEX_NAME?q=firstName_s:John&sort:timeStamp_l%20desc