为什么缺少一条消息?如何修复它?
Why is there a message missing? How can is fix it?
我对 RIAK KV 完全陌生。我实际上是在大学里学习的,并且必须编写一些 Java 代码来将 5000 个数据集存储在 RIAK 集群中。于是我开始写代码:
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!!!");
}
}
您可以在上面看到包含产品 Class 的代码。为了随机生成 500 个产品并将这些产品存储在 Rick 的 KV 数据库中,我写了以下内容:
package main;
import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.WaitStrategy;
import java.io.FileNotFoundException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.annotations.RiakKey;
import com.basho.riak.client.api.annotations.RiakLinks;
import com.basho.riak.client.api.commands.kv.StoreValue;
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.query.links.RiakLink;
import com.basho.riak.client.core.util.BinaryValue;
public class ProductRiakCluster {
static String artikelnummer;
public static void main(String args[]) throws FileNotFoundException, UnknownHostException, ExecutionException, InterruptedException {
System.out.println("main-method started...");
System.out.println("Starting create RiakCluster...");
for (int i = 5000; i > 0; i--) {
dataGenerator(i);
RiakClient client = RiakClient.newClient(8087, "127.0.0.1");
System.out.println("RiakClient " + client);
RiakObject riakObj = new RiakObject();
System.out.println("RiakObjekt " + riakObj);
Namespace productsBucket = new Namespace("products");
System.out.println("Bucket " + productsBucket);
Location productObjectLocation = new Location(productsBucket, artikelnummer);
System.out.println("Location " + productObjectLocation);
StoreValue storeP = new StoreValue.Builder(riakObj).withLocation(productObjectLocation).build();
StoreValue.Response response = client.execute(storeP);
client.shutdown();
}
System.out.println("RiakCluster setup finished...");
}
public static class ProductPojo {
@RiakKey
public String artikelnummer;
public String name;
public String color;
public Integer price;
@RiakLinks
public Collection < RiakLink > picture = new ArrayList < RiakLink > ();
}
private static void dataGenerator(int i) {
System.out.println("Started DataGenerator...");
try {
artikelnummer = String.valueOf(i);
generateRandomProduct(artikelnummer);
} catch (FileNotFoundException e) {
System.out.println("File not found...");
e.printStackTrace();
}
}
private static void generateRandomProduct(String artikelnummer) throws FileNotFoundException {
System.out.println("Method <generateRandomProduct> is running..." + artikelnummer);
Product product = new Product();
ProductPojo propo = new ProductPojo();
propo.artikelnummer = artikelnummer;
propo.name = product.name;
propo.color = product.color;
propo.price = product.price;
propo.picture.add(new RiakLink("pictures", product.picture, "Produktbild"));
product.printProduct();
}
}
我启动这个程序后出现以下错误:
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)
我对此的想法:我生成了一个 "artikelnummer" 但它没有出现在 Pojo class 中,因此 Pojo 具有空值。但是我不知道如何解决这个问题。
问题是您没有将任何东西传递给 RiakObject。
Return 生成的 ProductPojo 实例并将其保存在变量中,比如 productToSave
。
然后,调用 new StoreValue.Builder(productToSave)
,或使用 RiakObject
,如下所示:
RiakObject riakObj = new RiakObject();
riakObj.setValue(BinaryValue.create(productToSave));
附带说明一下,不幸的是,您的代码存在一些编程问题。例如,通过静态成员在方法之间交换数据是不好的。您也不必在每次需要时都创建和关闭客户端 - 为所有查询重复使用一个实例。
我对 RIAK KV 完全陌生。我实际上是在大学里学习的,并且必须编写一些 Java 代码来将 5000 个数据集存储在 RIAK 集群中。于是我开始写代码:
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!!!");
}
}
您可以在上面看到包含产品 Class 的代码。为了随机生成 500 个产品并将这些产品存储在 Rick 的 KV 数据库中,我写了以下内容:
package main;
import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.WaitStrategy;
import java.io.FileNotFoundException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.annotations.RiakKey;
import com.basho.riak.client.api.annotations.RiakLinks;
import com.basho.riak.client.api.commands.kv.StoreValue;
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.query.links.RiakLink;
import com.basho.riak.client.core.util.BinaryValue;
public class ProductRiakCluster {
static String artikelnummer;
public static void main(String args[]) throws FileNotFoundException, UnknownHostException, ExecutionException, InterruptedException {
System.out.println("main-method started...");
System.out.println("Starting create RiakCluster...");
for (int i = 5000; i > 0; i--) {
dataGenerator(i);
RiakClient client = RiakClient.newClient(8087, "127.0.0.1");
System.out.println("RiakClient " + client);
RiakObject riakObj = new RiakObject();
System.out.println("RiakObjekt " + riakObj);
Namespace productsBucket = new Namespace("products");
System.out.println("Bucket " + productsBucket);
Location productObjectLocation = new Location(productsBucket, artikelnummer);
System.out.println("Location " + productObjectLocation);
StoreValue storeP = new StoreValue.Builder(riakObj).withLocation(productObjectLocation).build();
StoreValue.Response response = client.execute(storeP);
client.shutdown();
}
System.out.println("RiakCluster setup finished...");
}
public static class ProductPojo {
@RiakKey
public String artikelnummer;
public String name;
public String color;
public Integer price;
@RiakLinks
public Collection < RiakLink > picture = new ArrayList < RiakLink > ();
}
private static void dataGenerator(int i) {
System.out.println("Started DataGenerator...");
try {
artikelnummer = String.valueOf(i);
generateRandomProduct(artikelnummer);
} catch (FileNotFoundException e) {
System.out.println("File not found...");
e.printStackTrace();
}
}
private static void generateRandomProduct(String artikelnummer) throws FileNotFoundException {
System.out.println("Method <generateRandomProduct> is running..." + artikelnummer);
Product product = new Product();
ProductPojo propo = new ProductPojo();
propo.artikelnummer = artikelnummer;
propo.name = product.name;
propo.color = product.color;
propo.price = product.price;
propo.picture.add(new RiakLink("pictures", product.picture, "Produktbild"));
product.printProduct();
}
}
我启动这个程序后出现以下错误:
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)
我对此的想法:我生成了一个 "artikelnummer" 但它没有出现在 Pojo class 中,因此 Pojo 具有空值。但是我不知道如何解决这个问题。
问题是您没有将任何东西传递给 RiakObject。
Return 生成的 ProductPojo 实例并将其保存在变量中,比如 productToSave
。
然后,调用 new StoreValue.Builder(productToSave)
,或使用 RiakObject
,如下所示:
RiakObject riakObj = new RiakObject();
riakObj.setValue(BinaryValue.create(productToSave));
附带说明一下,不幸的是,您的代码存在一些编程问题。例如,通过静态成员在方法之间交换数据是不好的。您也不必在每次需要时都创建和关闭客户端 - 为所有查询重复使用一个实例。