2017-11-28 5 views
0

私はRIAK KVにとって全く新しいです。私は実際に大学でそれを学び、5000のデータセットをRIAKクラスタに格納するためのJavaコードを書く必要があります。だから私はコードを始めました:なぜメッセージが欠落していますか?どのようにそれを修正することができますか?

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

以上、あなたは製品クラスを含むコードを見ることができます。ランダムに500の製品を生成して、私は次のように書いたリックの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 [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) 

私の考えを:私は生成"artikelnummer"ですが、Pojoクラスでは発生しないため、Pojoにはnull値があります。しかし、私はこの問題を解決する方法はありません。

+2

_コードがわかりにくくないことを願っています_ - そうではありません。しかし、読むのは難しいです。しばらく時間をとり、[最小、完全、および検証可能な例](http://stackoverflow.com/help/mcve)を読んでください。 – Clijsters

+0

ちょっと改善するためにヒルさんにありがとうございました:) – ElmoAll

+0

@Elmo問題はありませんが、コードを理解するために必要な '輸入物'のほとんどはないと思いますので、記事をお読みください。 Btw。私はJava Beautifierに入れました;) – Hille

答えて

1

問題は、RiakObjectに何も渡していないということです。

生成されたProductPojoのインスタンスを返し、変数、たとえばproductToSaveに保存します。その後

new StoreValue.Builder(productToSave)を呼び出す、または次のようにRiakObjectを使用し、次のいずれかの側の注意点としては

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

を、あなたのコードは、残念ながら、いくつかのプログラミングの問題に苦しんでいます。たとえば、静的メンバーを介してメソッド間でデータをやりとりするのは良くありません。必要なときにクライアントを作成してシャットダウンする必要もなく、すべてのクエリに対して1つのインスタンスを再利用できます。

+0

あなたのソリューションは多くの助けになりました。あなたが言及したように、多くのプログラミング上の問題があります。私はそれを少し修正し、完全なプログラムが完了した後にただ1つのインスタンスとシャットダウンを作成します。 – ElmoAll

関連する問題