2017-11-28 2 views
0

Ich bin absolut neu in RIAK KV. Ich lerne es tatsächlich in der Universität und muss Java-Code schreiben, um 5000 Datensätze in einem RIAK-Cluster zu speichern. Also fing ich an Code:Warum fehlt eine Nachricht? Wie kann es repariert werden?

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!!!"); 
    } 
} 

Above Sie den Code sehen können, dass die Produktklasse enthält. So generieren 500 Produkte nach dem Zufall und speichern diese Produkte in der KV-Datenbank von Rick schrieb ich folgendes:

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(); 
    } 
} 

Nachdem ich dieses Programm der folgende Fehler auftritt gestartet:

RiakClient [email protected] 
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) 

Meine Gedanken dazu: Ich erzeugen eine "artikelnummer" aber es kommt nicht in der Pojo-Klasse vor und so hat der Pojo einen Nullwert. Aber ich habe keine Lösung, wie ich dieses Problem beheben kann.

+2

_Ich hoffe, der Code ist nicht zu schwer zu verstehen_ - Es ist nicht. Es ist jedoch schwer zu lesen. Bitte nehmen Sie sich etwas Zeit und lesen Sie [Minimale, vollständige und überprüfbare Beispiele] (http://stackoverflow.com/help/mcve). – Clijsters

+0

Danke Hille für Hilfe, um ein wenig zu verbessern :) – ElmoAll

+0

@ElmoAll kein Problem, aber bitte lesen Sie den Artikel, becouse ich denke, die meisten der 'Importe' arent notwendig für das Verständnis des Codes. Übrigens. Ich habe es einfach in einen Java Beautifier gesteckt;) – Hille

Antwort

1

Das Problem ist, dass Sie nichts an RiakObject übergeben.

Geben Sie eine generierte Instanz von ProductPojo zurück und speichern Sie sie in einer Variablen, z. B. productToSave.

Dann entweder rufen new StoreValue.Builder(productToSave) oder RiakObject wie folgt verwenden:

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

Als Randbemerkung, Code von einigen Fragen zur Programmierung leidet, leider. Zum Beispiel ist der Austausch von Daten zwischen Methoden über ein statisches Member nicht gut. Sie müssen einen Client nicht jedes Mal neu erstellen und herunterfahren, wenn Sie ihn benötigen - Sie können eine Instanz für alle Ihre Abfragen erneut verwenden.

+0

Ihre Lösung hat sehr geholfen. Wie Sie erwähnt haben, gibt es viele Programmierprobleme. Ich habe es ein wenig repariert und ich erstelle nur eine Instanz und schalte ab, nachdem das komplette Programm fertig ist. – ElmoAll

Verwandte Themen