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

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


                } 


            } 
        } 
    } 

}

最佳答案

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

您有两个选择:

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

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

关于java - 从文本文件读取时如何加载 Apache Ignite Cache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295200/

相关文章:

php - 注销后浏览器后退按钮

java - 在 premain 方法中传递字符串有什么值(value)

java - Discord 机器人中的命令问题

java - 使用哪些集合?

c - 从文件中读取时,我得到特殊字符,这些字符不在我的文本文件中

javascript - EJS 中 webpack 包的版本控制

c# - 使用缓存机制进行节流有问题吗?

java - 同步适配器类中的单例对象

file - Xamarin 形式 : How to create folder and a file in device external storage?

c++ - 转换 SRTM 数据时的负高度