2016-03-29 35 views
1

我創建了一個文件helloworld.txt。現在我正在讀取文件,然後我想將文件的內容加載到緩存中,並且每當緩存更新時,它也應該寫入文件。如何從文本文件讀取時加載Apache Ignite Cache

這是我到目前爲止的代碼:

請告訴我該怎麼加載緩存,然後從緩存中的文件寫,因爲指令不是來自Apache的點燃文檔清楚。

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

import org.apache.ignite.Ignite; 
import org.apache.ignite.IgniteCache; 
import org.apache.ignite.IgniteDataStreamer; 
import org.apache.ignite.IgniteException; 
import org.apache.ignite.Ignition; 
import org.apache.ignite.examples.ExampleNodeStartup; 
import org.apache.ignite.examples.ExamplesUtils; 

public class FileRead { 
    /** Cache name. */ 
    private static final String CACHE_NAME = "FileCache"; 


    /** Heap size required to run this example. */ 
    public static final int MIN_MEMORY = 512 * 1024 * 1024; 

    /** 
    * Executes example. 
    * 
    * @param args Command line arguments, none required. 
    * @throws IgniteException If example execution failed. 
    */ 
    public static void main(String[] args) throws IgniteException { 
     ExamplesUtils.checkMinMemory(MIN_MEMORY); 

     try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { 
      System.out.println(); 


      try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) { 
       long start = System.currentTimeMillis(); 

       try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer(CACHE_NAME)) { 
        // Configure loader. 
        stmr.perNodeBufferSize(1024); 
        stmr.perNodeParallelOperations(8); 

        ///FileReads(); 

        try { 
        BufferedReader in = new BufferedReader 
        (new FileReader("/Users/akritibahal/Desktop/helloworld.txt")); 
        String str; 
        int i=0; 
        while ((str = in.readLine()) != null) { 
        System.out.println(str); 
        stmr.addData(i,str); 
        i++; 
         } 
        System.out.println("Loaded " + i + " keys."); 
        } 
        catch (IOException e) { 
        } 


       } 


      } 
     } 
    } 

} 

回答

1

有關如何從持久存儲加載緩存,請參閱本網頁的信息:https://apacheignite.readme.io/docs/data-loading

你有兩個選擇:

  1. 啓動客戶端節點,創建IgniteDataStreamer和使用它加載數據。只需撥打addData()即可獲得文件中的每一行。
  2. 執行CacheStore.loadCache()方法,在緩存配置中提供實現並調用IgniteCache.loadCache()

第二種方法將需要在所有服務器節點上存在文件,因爲節點之間不會有通信,所以很可能它會更快。

+0

我不知道如何從緩存中讀取,寫入文件並更新到文件中。我如何獲得存儲在這種情況下的密鑰? – user3591433

相關問題