Skip to content

Creating and modifying buckets (v2.0)

Alex Moore edited this page Jun 24, 2015 · 1 revision

Note: This document is for the 2.x Java Client series.

Creating or modifying a bucket in Riak

import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.buckets.FetchBucketProperties;
import com.basho.riak.client.api.commands.buckets.StoreBucketProperties;
import com.basho.riak.client.core.operations.FetchBucketPropsOperation;
import com.basho.riak.client.core.query.BucketProperties;
import com.basho.riak.client.core.query.Namespace;

import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;

public class BucketTypeProperties
{
    public static void main(String [] args) throws UnknownHostException, ExecutionException, InterruptedException {

        RiakClient client = RiakClient.newClient(10017, "127.0.0.1");

        Namespace ns = new Namespace("TestBucket");

        // If the bucket does not exist in Riak, it will be created with the default properties when you query for them. 
        FetchBucketProperties fetchProps = new FetchBucketProperties.Builder(ns).build();

        FetchBucketPropsOperation.Response fetchResponse = client.execute(fetchProps);
        BucketProperties bp = fetchResponse.getBucketProperties();

        // By using the StoreBucketProperties command, 
        // you can specify properties' values. 
        //
        // If the bucket already exists in Riak, the bucket 
        // properties will be updated.
        //
        // Only those properties that you specify will be updated, 
        // there is no need to fetch the bucket properties to edit them.
        StoreBucketProperties storeProps = 
            new StoreBucketProperties.Builder(ns)
            .withNVal(2).withR(1).build();

        client.execute(storeProps);

        client.shutdown();
    }
}