Implement a Data Loader

To program a data loader and configure your region to use it:

  1. Program your loader.

  2. Install your loader in each member region where you need it.

Program your loader

To program your loader:

  1. Implement org.apache.geode.cache.CacheLoader.

  2. If you want to declare the loader in your cache.xml, implement the org.apache.geode.cache.Declarable interface as well.

  3. Program the single CacheLoader load method to do whatever your application requires for retrieving the value from outside the cache. If you need to run Region API calls from your loader, spawn separate threads for them. Do not make direct calls to Region methods from your load method implementation as it could cause the cache loader to block, hurting the performance of the distributed system. For example:

    public class SimpleCacheLoader implements CacheLoader, Declarable {
        public Object load(LoaderHelper helper) {
            String key = (String) helper.getKey();
            System.out.println(" Loader called to retrieve value for " + key);
            // Create a value using the suffix number of the key (key1, key2, etc.)
            return "LoadedValue" + (Integer.parseInt(key.substring(3)));
        }
        public void close() { // do nothing }
        public void init(Properties props) { // do nothing }
    }
    

Install your loader in each member region

To install your loader in each member region where you need it:

  1. In a partitioned region, install the cache loader in every data store for the region (partition-attributes local-max-memory > 0).

  2. In a distributed region, install the loader in the members where it makes sense to do so. Cache loaders are usually defined in only a subset of the members holding the region. You might, for example, assign the job of loading from a database to one or two members for a region hosted by many more members. This can be done to reduce the number of connections when the outside source is a database.

    Use one of these methods to install the loader:

    • XML:

      <region-attributes>
          <cache-loader>
              <class-name>myCacheLoader</class-name>
          </cache-loader>
      </region-attributes>
      
    • XML with parameters:

      <cache-loader>
          <class-name>com.company.data.DatabaseLoader</class-name>
          <parameter name="URL">
              <string>jdbc:cloudscape:rmi:MyData</string>
          </parameter>
      </cache-loader>
      
    • Java:

      RegionFactory<String,Object> rf = cache.createRegionFactory(REPLICATE);
      rf.setCacheLoader(new QuoteLoader());
      quotes = rf.create("NASDAQ Quotes");
      

Note: You can also configure Regions using the gfsh command-line interface, however you cannot configure a cache-loader using gfsh. See Region Commands.

results matching ""

    No results matching ""