【问题标题】:How to load Apache Ignite Cache when reading from a text file从文本文件读取时如何加载 Apache Ignite Cache
【发布时间】:2016-07-17 15:38:48
【问题描述】:

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


                } 


            } 
        } 
    } 

}

【问题讨论】:

    标签: java file caching gridgain ignite


    【解决方案1】:

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

    你有两个选择:

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

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

    【讨论】:

    • 我无法弄清楚如何从缓存中读取、写入文件并更新到文件中。在这种情况下如何获取存储的密钥??
    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 2018-03-03
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2015-07-14
    相关资源
    最近更新 更多