簡體   English   中英

com.basho.riak.client.core.RiakNode錯誤-在RiakNode上寫入失敗

[英]ERROR com.basho.riak.client.core.RiakNode - Write failed on RiakNode

我已將Riak KV與Java客戶端一起使用,盡管我已使用要創建對象的空間名稱創建了Bucket,但無法在RiakNode上進行寫操作。

我基本上有一個由Basho開發者網站提供的TasteOfRiak.java類: https ://raw.githubusercontent.com/basho/basho_docs/master/extras/code-examples/TasteOfRiak.java

import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.DeleteValue;
import com.basho.riak.client.api.commands.kv.FetchValue;
import com.basho.riak.client.api.commands.kv.StoreValue;
import com.basho.riak.client.api.commands.kv.UpdateValue;
import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakNode;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.query.RiakObject;
import com.basho.riak.client.core.util.BinaryValue;

import java.net.UnknownHostException;

public class TasteOfRiak {
    // A basic POJO class to demonstrate typed exchanges with Riak
    public static class Book {
        public String title;
        public String author;
        public String body;
        public String isbn;
        public Integer copiesOwned;
    }

    // This will allow us to update the book object handling the
    // entire fetch/modify/update cycle.
    public static class BookUpdate extends UpdateValue.Update<Book> {
        private final Book update;
        public BookUpdate(Book update){
            this.update = update;
        }

        @Override
        public Book apply(Book t) {
            if(t == null) {
                t = new Book();
            }

            t.author = update.author;
            t.body = update.body;
            t.copiesOwned = update.copiesOwned;
            t.isbn = update.isbn;
            t.title = update.title;

            return t;
        }
    }

    // This will create a client object that we can use to interact with Riak
    private static RiakCluster setUpCluster() throws UnknownHostException {
        // This example will use only one node listening on localhost:10017
        RiakNode node = new RiakNode.Builder()
                .withRemoteAddress("127.0.0.1")
                .withRemotePort(8087)
                .build();

        // This cluster object takes our one node as an argument
        RiakCluster cluster = new RiakCluster.Builder(node)
                .build();

        // The cluster must be started to work, otherwise you will see errors
        cluster.start();

        return cluster;
    }

    public static void main( String[] args ) {
        try {
            // First, we'll create a basic object storing a movie quote
            RiakObject quoteObject = new RiakObject()
                    // We tell Riak that we're storing plaintext, not JSON, HTML, etc.
                    .setContentType("text/plain")
                            // Objects are ultimately stored as binaries
                    .setValue(BinaryValue.create("You're dangerous, Maverick"));
            System.out.println("Basic object created");

            // In the new Java client, instead of buckets you interact with Namespace
            // objects, which consist of a bucket AND a bucket type; if you don't
            // supply a bucket type, "default" is used; the Namespace below will set
            // only a bucket, without supplying a bucket type
            Namespace quotesBucket = new Namespace("quotes");

            // With our Namespace object in hand, we can create a Location object,
            // which allows us to pass in a key as well
            Location quoteObjectLocation = new Location(quotesBucket, "Iceman");
            System.out.println("Location object created for quote object");

            // With our RiakObject in hand, we can create a StoreValue operation
            StoreValue storeOp = new StoreValue.Builder(quoteObject)
                    .withLocation(quoteObjectLocation)
                    .build();
            System.out.println("StoreValue operation created");

            // And now we can use our setUpCluster() function to create a cluster
            // object which we can then use to create a client object and then
            // execute our storage operation
            RiakCluster cluster = setUpCluster();
            RiakClient client = new RiakClient(cluster);
            System.out.println("Client object successfully created");

            StoreValue.Response storeOpResp = client.execute(storeOp);
            System.out.println("Object storage operation successfully completed");

            // Now we can verify that the object has been stored properly by
            // creating and executing a FetchValue operation
            FetchValue fetchOp = new FetchValue.Builder(quoteObjectLocation)
                    .build();
            RiakObject fetchedObject = client.execute(fetchOp).getValue(RiakObject.class);
            assert(fetchedObject.getValue().equals(quoteObject.getValue()));
            System.out.println("Success! The object we created and the object we fetched have the same value");

            // Now update the fetched object
            fetchedObject.setValue(BinaryValue.create("You can be my wingman any time."));
            StoreValue updateOp = new StoreValue.Builder(fetchedObject)
                    .withLocation(quoteObjectLocation)
                    .build();
            StoreValue.Response updateOpResp = client.execute(updateOp);
            updateOpResp = client.execute(updateOp);

            // And we'll delete the object
            DeleteValue deleteOp = new DeleteValue.Builder(quoteObjectLocation)
                    .build();
            client.execute(deleteOp);
            System.out.println("Quote object successfully deleted");

            Book mobyDick = new Book();
            mobyDick.title = "Moby Dick";
            mobyDick.author = "Herman Melville";
            mobyDick.body = "Call me Ishmael. Some years ago...";
            mobyDick.isbn = "1111979723";
            mobyDick.copiesOwned = 3;
            System.out.println("Book object created");

            // Now we'll assign a Location for the book, create a StoreValue
            // operation, and store the book
            Namespace booksBucket = new Namespace("books");
            Location mobyDickLocation = new Location(booksBucket, "moby_dick");
            StoreValue storeBookOp = new StoreValue.Builder(mobyDick)
                    .withLocation(mobyDickLocation)
                    .build();
            client.execute(storeBookOp);
            System.out.println("Moby Dick information now stored in Riak");

            // And we'll verify that we can fetch the info about Moby Dick and
            // that that info will match the object we created initially
            FetchValue fetchMobyDickOp = new FetchValue.Builder(mobyDickLocation)
                    .build();
            Book fetchedBook = client.execute(fetchMobyDickOp).getValue(Book.class);
            System.out.println("Book object successfully fetched");

            assert(mobyDick.getClass() == fetchedBook.getClass());
            assert(mobyDick.title.equals(fetchedBook.title));
            assert(mobyDick.author.equals(fetchedBook.author));
            // And so on...

            // Now to update the book with additional copies
            mobyDick.copiesOwned = 5;
            BookUpdate updatedBook = new BookUpdate(mobyDick);
            UpdateValue updateValue = new UpdateValue.Builder(mobyDickLocation)
                    .withUpdate(updatedBook).build();
            UpdateValue.Response response = client.execute(updateValue);

            System.out.println("Success! All of our tests check out");

            // Now that we're all finished, we should shut our cluster object down
            cluster.shutdown();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

每當Eclipse執行以下代碼時:“ StoreValue.Response storeOpResp = client.execute(storeOp); System.out.println(“對象存儲操作成功完成”);“

我收到一個錯誤“ ERROR com.basho.riak.client.core.RiakNode-在RiakNode上寫入失敗”。

在運行該程序之前,我已經創建了quotesBucket存儲桶並已將其激活。

有人知道問題出在哪里嗎?

您可以通過http存儲對象嗎? 在終端中嘗試以下操作:

curl -XPUT \
-H "Content-Type: text/plain" \
-d "You're dangerous, Maverick" \
http://localhost:8098/types/default/buckets/quotes/keys/Iceman?returnbody=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM