2

我创建了一个文件 helloworld.txt。现在我正在从文件中读取,然后我想将文件的内容加载到缓存中,并且每当更新缓存时,它也应该写入文件。

到目前为止,这是我的代码:

请告诉我如何加载缓存然后从缓存写入文件,因为 Apache Ignite 文档中的说明不清楚。

   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) { 
                   } 


                } 


            } 
        } 
    } 

}
4

1 回答 1

2

有关如何从持久存储加载缓存的信息,请参阅此页面:https ://apacheignite.readme.io/docs/data-loading

你有两个选择:

  1. 启动一个客户端节点,创建IgniteDataStreamer并使用它来加载数据。只需调用addData()文件中的每一行。
  2. 实现CacheStore.loadCache()方法,在缓存配置和调用中提供实现IgniteCache.loadCache()

第二种方法需要在所有服务器节点上都有文件,因为节点之间没有通信,所以很可能会更快。

于 2016-03-29T23:21:09.807 回答