本文环境:

  1. Apache ignite 2.8.0
  2. jdk 1.8.0_241

一、开启持久化

java方式:

// Ignite persistence configuration.
DataStorageConfiguration storageCfg = new DataStorageConfiguration();
// Enabling the persistence.
storageCfg.getDefaultDataRegionConfiguration().setPersistenceEnabled(true);
// Applying settings.
cfg.setDataStorageConfiguration(storageCfg);

xml方式:

<property name="dataStorageConfiguration">
	<bean class="org.apache.ignite.configuration.DataStorageConfiguration">
		<property name="defaultDataRegionConfiguration">
			<bean class="org.apache.ignite.configuration.DataRegionConfiguration">
				<property name="persistenceEnabled" value="true"/>
			</bean>
		</property>
	</bean>
</property>

二、配置持久化存储路径

java方式

storageCfg.setStoragePath("/opt/storage");

xml方式

    <property name="dataStorageConfiguration">
        <bean class="org.apache.ignite.configuration.DataStorageConfiguration">
            <property name="defaultDataRegionConfiguration">
                <bean class="org.apache.ignite.configuration.DataRegionConfiguration">
                    <property name="persistenceEnabled" value="true"/>
                </bean>
            </property>
            <!-- 持久化存储路径 -->
            <property name="storagePath" value="/opt/storage"/>
        </bean>
    </property>

三、读写数据

IgniteCache<Integer, String> igniteCache = ignite.getOrCreateCache(cacheName);

// 添加一个缓存
igniteCache.put(id, vertex);
// 添加一组缓存
igniteCache.putAll(map);
// 异步添加
igniteCache.putAsync(id, vertex);
igniteCache.putAllAsync(map);

...

四、IgniteDataStreamer添加数据

IgniteDataStreamer添加数据的效率相比IgniteCache较高。

IgniteDataStreamer<Long, byte[]> igniteStreamer = ignite.dataStreamer("testStream");

igniteStreamer.addData(i, bytes);