Java 为什么缺少一条消息?你怎么能修好它?

Java 为什么缺少一条消息?你怎么能修好它?,java,riak,key-value-store,java-client,Java,Riak,Key Value Store,Java Client,我对里亚克绝对是新手。实际上,我是在大学里学的,必须编写一些Java代码来在RIAK集群中存储5000个数据集。所以我开始编写代码: package main; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collection; import java.util.Random; import java.util.Scanner;

我对里亚克绝对是新手。实际上,我是在大学里学的,必须编写一些Java代码来在RIAK集群中存储5000个数据集。所以我开始编写代码:

package main;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.Scanner;

import org.w3c.dom.NameList;

public class Product {
    String artikelnummer;
    String name;
    String color;
    Integer price;
    String picture;
    Integer max = 999;
    String filepath = "./assets/products.txt";
    ArrayList<String> productNames;

    public Product() throws FileNotFoundException{
        productNames = readFile(filepath);
        name = randomName();
        color = randomColor();
        price = randomPrice(max);
        picture = randomPic();
    }

    private String randomPic() {
        String[] picNames = {"ikea0.jpg","ikea1.jpg","ikea2.jpg","ikea3.jpg","ikea4.jpg","ikea5.jpg","ikea6.jpg","ikea7.jpg","ikea8.jpg","ikea9.jpg"};
        Random randPicIndex = new Random();
        int randomPicIndex = randPicIndex.nextInt(9);
        return picNames[randomPicIndex];
    }

    public Integer randomPrice(int max){
        Random rand = new Random();

        int randomNum = rand.nextInt(max);
        price = randomNum;
        return price;
    }

    public String randomColor(){
        String[] colorArray =  {"blue","red","yellow","magenta","green","black","white","cyan","purple","brown"};
        Random randIndex = new Random();
        int randomIndex = randIndex.nextInt(10);
        int i = randomIndex;
        color = colorArray[i];
        return color;
    }

    public String randomName(){
        Random randomName = new Random();
        name = productNames.get(randomName.nextInt(productNames.size()));
        return name;
    }

    public ArrayList<String> readFile(String filepath) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(filepath));
        ArrayList<String> nameList = new ArrayList<String>();
        while (scanner.hasNextLine()){
            nameList.add(scanner.nextLine());
        }
        scanner.close();
        return nameList;
    }

    //Class for testing purposes
    public void printProduct(){
        System.out.println("Produktdatenblatt: Artikelnummer --> "+ artikelnummer + " " + name + " mit Farbe: " + color + " mit dem Preis: " + price + " ein Bild --> " + picture);
        System.out.println("Hat funktioniert!!!");
    }
}

我的想法是:我生成了一个ArtikerNummer,但它不在Pojo类中出现,因此Pojo有一个空值。但是我没有解决这个问题的方法。

问题是您没有将任何内容传递给RiakObject

返回生成的ProductPojo实例并将其保存在变量中,例如productToSave

然后,调用new StoreValue.BuilderproductToSave,或按如下方式使用RiakObject:

RiakObject riakObj = new RiakObject();
riakObj.setValue(BinaryValue.create(productToSave));

作为旁注,不幸的是,您的代码存在一些编程问题。例如,通过静态成员在方法之间交换数据是不好的。您也不必在每次需要时都创建和关闭客户机—为所有查询重用一个实例。

我希望代码不难理解—事实并非如此。然而,它很难阅读。请花点时间阅读相关内容。感谢Hille提供的帮助,以改进一点:@ElmoAll没问题,但请阅读本文,因为我认为大多数导入对于理解代码来说都不是必需的。顺便说一句,我刚刚把它放在一个Java美化器中;System.out.printlnHat funktioniert!!!;你怎么知道?这只是对象创建者工作的信息。。。这是为了测试类产品。你的解决方案帮了大忙。正如您提到的,有许多编程问题。我修正了一点,只创建了一个实例,在完成整个程序后关闭。
RiakClient com.basho.riak.client.api.RiakClient@2096442d
RiakObjekt RiakObject{contentType: application/octet-stream, value: null, riakIndexes: null, links: null, userMeta: null, vtag: null, isDeleted: false, isModified: false, vclock: null, lastModified: 0}
Bucket {type: default, bucket: products}
Location {namespace: {type: default, bucket: products}, key: 1}

Exception in thread "main" shaded.com.google.protobuf.UninitializedMessageException: Message missing required fields: value
at shaded.com.google.protobuf.AbstractMessage$Builder.newUninitializedMessageException(AbstractMessage.java:372)
at shaded.com.basho.riak.protobuf.RiakKvPB$RpbContent$Builder.build(RiakKvPB.java:18352)
at com.basho.riak.client.core.converters.RiakObjectConverter.convert(RiakObjectConverter.java:198)
at com.basho.riak.client.core.operations.StoreOperation$Builder.withContent(StoreOperation.java:158)
at com.basho.riak.client.api.commands.kv.StoreValue.buildCoreOperation(StoreValue.java:151)
at com.basho.riak.client.api.commands.kv.StoreValue.buildCoreOperation(StoreValue.java:72)
at com.basho.riak.client.api.GenericRiakCommand.executeAsync(GenericRiakCommand.java:41)
at com.basho.riak.client.api.commands.kv.StoreValue.executeAsync(StoreValue.java:112)
at com.basho.riak.client.api.RiakCommand.execute(RiakCommand.java:91)
at com.basho.riak.client.api.RiakClient.execute(RiakClient.java:355)
at main.ProductRiakCluster.main(ProductRiakCluster.java:49)
RiakObject riakObj = new RiakObject();
riakObj.setValue(BinaryValue.create(productToSave));