如何从文本文件中读取时加载Apache Ignite Cache

时间:2016-03-29 21:14:35

标签: java file caching gridgain ignite

我创建了一个文件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) { 
                   } 


                } 


            } 
        } 
    } 

}

1 个答案:

答案 0 :(得分:1)

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

您有两种选择:

  1. 启动客户端节点,创建IgniteDataStreamer并使用它来加载数据。只需为文件中的每一行调用addData()即可。
  2. 实施CacheStore.loadCache()方法,在缓存配置中提供实现并调用IgniteCache.loadCache()
  3. 第二种方法需要在所有服务器节点上都有文件,因为节点之间不会进行通信,因此很可能会更快。