com.hazelcast.core.IMap.addIndex()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(5.2k)|赞(0)|评价(0)|浏览(114)

本文整理了Java中com.hazelcast.core.IMap.addIndex()方法的一些代码示例,展示了IMap.addIndex()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IMap.addIndex()方法的具体详情如下:
包路径:com.hazelcast.core.IMap
类名称:IMap
方法名:addIndex

IMap.addIndex介绍

[英]Adds an index to this map for the specified entries so that queries can run faster.

Let's say your map values are Employee objects.

public class Employee implements Serializable { 
private boolean active = false; 
private int age; 
private String name = null; 
// other fields 
// getters setter 
}

If you are querying your values mostly based on age and active then you should consider indexing these fields.

IMap imap = Hazelcast.getMap("employees"); 
imap.addIndex("age", true);        // ordered, since we have ranged queries for this field 
imap.addIndex("active", false);    // not ordered, because boolean field cannot have range

Index attribute should either have a getter method or be public. You should also make sure to add the indexes before adding entries to this map.

Time to Index

Indexing time is executed in parallel on each partition by operation threads. The Map is not blocked during this operation.

The time taken in proportional to the size of the Map and the number Members.

Searches while indexes are being built

Until the index finishes being created, any searches for the attribute will use a full Map scan, thus avoiding using a partially built index and returning incorrect results.
[中]为此映射添加指定项的索引,以便查询可以更快地运行。
假设您的映射值是Employee对象。
<$$ 0 >如果您的查询值大多是基于Active和Active的,那么您应该考虑对这些字段进行索引。1$>>索引属性应该具有getter方法或是公共的。您还应该确保在向该映射添加条目之前添加索引。
索引时间
索引时间由操作线程在每个分区上并行执行。在此操作期间,地图未被阻止。
所花费的时间与地图的大小和成员的数量成正比。
在生成索引时进行搜索
在索引创建完成之前,对属性的任何搜索都将使用完整的映射扫描,从而避免使用部分构建的索引并返回错误的结果。

代码示例

代码示例来源:origin: hazelcast/hazelcast-jet

@Override
public void addIndex(String attribute, boolean ordered) {
  map.addIndex(attribute, ordered);
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public void run() {
    IMap map = hazelcast.getMap("myMap");
    map.addIndex("year", true);
  }
}, 1);

代码示例来源:origin: devicehive/devicehive-java-server

@PostConstruct
protected void init() {
  final IMap<String, HazelcastEntity> notificationsMap = hazelcastClient.getMap(NOTIFICATIONS_MAP);
  notificationsMap.addIndex(TIMESTAMP.getField(), true);
  final IMap<String, HazelcastEntity> commandsMap = hazelcastClient.getMap(COMMANDS_MAP);
  commandsMap.addIndex(TIMESTAMP.getField(), true);
  commandsMap.addIndex(LAST_UPDATED.getField(), true);
  
  mapsHolder.put(DeviceNotification.class, notificationsMap);
  mapsHolder.put(DeviceCommand.class, commandsMap);
}

代码示例来源:origin: com.hazelcast.stabilizer/stabilizer

private void initMap(){
  IMap map = targetInstance.getMap(basename);
  for(int i=0; i<keyCount; i++){
    map.put(i, new Employee(i));
  }
  long free = Runtime.getRuntime().freeMemory();
  long total =  Runtime.getRuntime().totalMemory();
  long used = total - free;
  //System.out.println("used = "+humanReadableByteCount(used, true));
  map.addIndex( "id", true );
  map.addIndex( "name", true );
  map.addIndex( "age", true );
  map.addIndex( "salary", true );
  map.addIndex( "active", false );
  free = Runtime.getRuntime().freeMemory();
  total =  Runtime.getRuntime().totalMemory();
  used = total - free;
  //System.out.println("used = "+humanReadableByteCount(used, true));
}

代码示例来源:origin: com.hazelcast.simulator/tests-common

@Prepare(global = true)
public void globalPrepare() {
  if (useIndex) {
    for (int i = 0; i < indexValuesCount; i++) {
      map.addIndex(format("payloadFromExtractor[%d]", i), true);
    }
  }
  loadInitialData();
}

代码示例来源:origin: SpringCloud/spring-cloud-admin

@Bean
@ConditionalOnMissingBean
public ApplicationStore applicationStore() {
  IMap<String, Application> map = hazelcastInstance.getMap(hazelcastMapName);
  map.addIndex("name", false);
  map.addEntryListener((MapListener) entryListener(), false);
  return new HazelcastApplicationStore(map);
}

代码示例来源:origin: com.hazelcast.simulator/tests-common

@Prepare(global = true)
public void prepare() {
  if (useIndex) {
    map.addIndex("payloadField[any]", true);
  }
  loadInitialData();
}

代码示例来源:origin: FlavioF/quartz-scheduler-hazelcast-jobstore

@Override
public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler)
 throws SchedulerConfigException {
 LOG.debug("Initializing Hazelcast Job Store..");
 this.schedSignaler = signaler;
 if (hazelcastClient == null) {
  LOG.warn("Starting new local hazelcast client since not hazelcast instance setted before starting scheduler.");
  hazelcastClient = Hazelcast.newHazelcastInstance();
 }
 // initializing hazelcast maps
 LOG.debug("Initializing hazelcast maps...");
 jobsByKey = getMap(HC_JOB_STORE_MAP_JOB);
 triggersByKey = getMap(HC_JOB_STORE_TRIGGER_BY_KEY_MAP);
 jobsByGroup = getMultiMap(HC_JOB_STORE_MAP_JOB_BY_GROUP_MAP);
 triggersByGroup = getMultiMap(HC_JOB_STORE_TRIGGER_KEY_BY_GROUP_MAP);
 pausedTriggerGroups = getSet(HC_JOB_STORE_PAUSED_TRIGGER_GROUPS);
 pausedJobGroups = getSet(HC_JOB_STORE_PAUSED_JOB_GROUPS);
 calendarsByName = getMap(HC_JOB_CALENDAR_MAP);
 triggersByKey.addIndex("nextFireTime", true);
 LOG.debug("Hazelcast Job Store Initialized.");
}

代码示例来源:origin: com.hazelcast.simulator/tests-common

@Prepare(global = true)
public void globalPrepare() {
  if (useIndex) {
    map.addIndex("salary", true);
  }
  Streamer<Integer, Employee> streamer = StreamerFactory.getInstance(map);
  for (int i = 0; i < keyCount; i++) {
    Employee employee = new Employee(i);
    streamer.pushEntry(employee.getId(), employee);
  }
  streamer.await();
}

相关文章