Hazelcast技术问询:如何通过单个键字段获取IMap中的值
Alright, let's break down why your original person.get("school_id='0001'") call isn't working first:
Hazelcast's
IMap.get()method requires a full instance of your key class (tblHeaders) to match entries. It uses the key'sequals()andhashCode()methods to find matches, so passing a string like"school_id='0001'"can't possibly match any of yourtblHeaderskeys.
Here are the most practical solutions to query by a single field like school_id:
1. Use Hazelcast Predicates for Filtering
Hazelcast provides a powerful query API that lets you filter entries based on key (or value) fields. The simplest way is using SqlPredicate:
// Import required classes import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IMap; import com.hazelcast.query.SqlPredicate; import com.hazelcast.core.HazelcastJsonValue; // Get your IMap instance IMap<tblHeaders, HazelcastJsonValue> headerMap = hazelcastInstance.getMap("your-map-name"); // Define the predicate to match school_id = '0001' SqlPredicate schoolIdPredicate = new SqlPredicate("school_id = '0001'"); // Retrieve all matching values (returns a Collection) Collection<HazelcastJsonValue> matchingValues = headerMap.values(schoolIdPredicate); // Handle the results (example: get the first match) if (!matchingValues.isEmpty()) { HazelcastJsonValue targetValue = matchingValues.iterator().next(); // Do something with targetValue }
You can also use the type-safe PredicateBuilder if you prefer avoiding string-based queries:
import com.hazelcast.query.PredicateBuilder; import com.hazelcast.query.EntryObject; EntryObject entryObject = new PredicateBuilder().getEntryObject(); Predicate<tblHeaders, HazelcastJsonValue> predicate = entryObject.get("school_id").equal("0001"); Collection<HazelcastJsonValue> matchingValues = headerMap.values(predicate);
2. Add an Index to Boost Query Performance
If your IMap has a large number of entries, predicate queries will perform a full scan by default. To optimize this, add an index on the school_id field:
// Add a non-unique index (use true if school_id is unique across entries) headerMap.addIndex("school_id", false);
This index will make subsequent predicate queries on school_id much faster, as Hazelcast can directly look up matching entries instead of scanning every key.
3. Optional: Create a Reverse Map for Frequent Queries
If you regularly query by school_id, you can maintain a secondary IMap that maps school_id strings directly to HazelcastJsonValue:
// Secondary map for quick lookups by school_id IMap<String, HazelcastJsonValue> schoolIdToValueMap = hazelcastInstance.getMap("school-id-to-value"); // When inserting into the main map, also update the secondary map tblHeaders key = new tblHeaders("0001", "Some School", "unique-123"); HazelcastJsonValue value = new HazelcastJsonValue("{...}"); headerMap.put(key, value); schoolIdToValueMap.put(key.getSchool_id(), value); // Now you can get the value directly HazelcastJsonValue quickGetValue = schoolIdToValueMap.get("0001");
Just remember to keep both maps in sync when updating or deleting entries to avoid data inconsistencies.
内容的提问来源于stack exchange,提问作者manu

