Coherence ValueExtractor Query Example

Querying data from cache using ValueExtractor API is very useful and you could use it in many ways. Below example will connect to the cache (Product) and pulls data from it based on Product Id where product id index number in the cache is 10:

  • ValueExtractorExample.java:
import java.util.Map.Entry;
import java.util.Set;

import com.tangosol.net.NamedCache;
import com.tangosol.util.Filter;
import com.tangosol.util.ValueExtractor;
import com.tangosol.util.extractor.PofExtractor;
import com.tangosol.util.filter.EqualsFilter;
import com.javahonk.datautil.CacheRegistry;
import com.model.product.OptionModel;

public class ValueExtractorExample {

	@SuppressWarnings("unchecked")
	public void checkValueExtractor() {

		NamedCache cache = (NamedCache) CacheRegistry.getNamedCache("Products");
		
		//If you know cache key then you could use below
		Object obj = cache.get("4526");
		System.out.println(obj);

		//Below is pulling data base on index id and here index id is 10: 
		try {

			ValueExtractor valueExtractor = new PofExtractor(null, 10);
			Filter filterValue = new EqualsFilter(valueExtractor, "2471");
			Set<Entry<String, OptionModel>> value = cache.entrySet(filterValue);
			for (Entry<String, OptionModel> entry : value) {
				System.out.println(entry.getValue());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *