Geode Region 管理

数据是以键/值对(key/value)的形式存于regions中的 
键和值可以是任何类型 
org.apache.geode.cache.Region实现了java.util.Map接口 
可以通过XML配置、API以及gfsh命令的方式创建、删除、清除或使无效操作Region 
Region的属性定义了区域中的数据如何存储,分发和管理 
数据区域可以在分布式系统成员之间划分,也可以是本地的 

* 如果更改定义Region的属性,则必须重新启动成员才能使更改生效 

创建Region

gfsh命令行

gfsh>create region --name=region1 --type=REPLICATE

通过cache.xml文件

通常是使用cache.xml文件配置Region,当使用该cache.xml创建member时,Region将同时会被创建。

<region name="myRegion" refid="REPLICATE">
    <region-attributes statistics-enabled="true">
        <entry-time-to-live>
            <expiration-attributes timeout="60" action="destroy"/>
        </entry-time-to-live>
        <cache-listener>
            <class-name>myPackage.MyCacheListener</class-name>
        </cache-listener>
    </region-attributes>
</region>

通过API接口

Geode的region APIs为不同类型的成员提供了不同的方法 
1.Peer/Server Region APIs. 在org.apache.geode.cache包下, 对应cache.xml中<cache>配置内的声明。 
org.apache.geode.cache.Cache.createRegionFactory. 
该方法需要一个RegionShortcut 参数,参考RegionShortcut枚举,使用返回RegionFactory。 
org.apache.geode.cache.RegionFactory。提供设置Region属性并创建Region的方法。返回一个Region。 
org.apache.geode.cache.RegionShortcut。 定义了基本的Region配置 

2.Client Region APIs. 在org.apache.geode.cache.client包下,对应cache.xml中<client-cache>配置内的声明。 
org.apache.geode.cache.clientCache.createRegionFacoty 
该方法需要一个ClientRegionShortcut参数,参考ClientRegionShortcut枚举。返回ClientRegionFactory。 
org.apache.geode.cache.client.ClientRegionFactory. 提供设置Region属性并创建Region的方法。返回一个Region. 
org.apache.geode.cache.client.ClientRegionShortcut. 定义了基本的Client Region配置 

3.用于所有成员类型的Region APIs, 在org.apache.geode.cache包下。 
org.apache.geode.cache.Region 提供管理Regions和数据的方法 
org.apache.geode.cache.RegionAttributes 提供管理Regions属性方法。 

创建Region流程 1.创建Region Factory. 
a)Peers/Servers: 用org.apache.geode.cache.RegionFactory. 
b)Cliets:用org.apache.geode.cache.client.ClientRegionFactory. 
2.合用RegionFactory进一步的配置Region. 
3.通过前面配置创建Region 

例1:创建Replicated Region,Region命名为myregion

Cache cache = CacheFactory.create();
RegionFactory rf = cache.createRegionFactory(RegionShortcut .REPLICATE);
Region region = rf.create(“myregion”)

例2:创建Partitioned Region并添加一个listener;

Cache cache = CacheFactory.create();
RegionFactory rf = cache.createRegionFactory(RegionShortcut.PARTITION);
Rf.addCacheListener(new LoggingCacheListener());
Region region = rf.create(“myregion”);

例3:Create a partitioned region with a partition resolver for colocated regions:

PartitionAttributesFactory paf = new PartitionAttributesFactory<CustomerId, String>();
paf.setPartitionResolver(new CustomerOrderResolver());
RegionFactory rf = cache.createRegionFactory(RegionShortcut.PARTITION);
rf.setPartitionAttributes(paf.create());
rf.addCacheListener(new LoggingCacheListener());
custRegion = rf.create("customer");

例4: 创建Client Region

ClientRegionFactory<String,String> cRegionFactory = cache.createClientRegionFactory(PROXY);
Region<String, String> region = cRegionFactory.setPoolName("Pool3").create("DATA");

更新Region配置

可以通过alter region命令,API或cache.xml文件中声明的方式更新region的属性内容 
1. 使用gfsh alter region命令 
2. API方式,使用Cache和Region提供的方法更新配置参数,region structure和数据 
3. 使用Cache.loadCacheXml 方法加载XML更新Region配置,如果XML中配置的Region已经存在,其中易变的属性将会被更新,不易变的属性不会被修改。如果Region不存在则会按XML中的声明创建 

Invalidate a Region

Region.invalidateRegion(); 
invalidateRegion操作将会清除Region中所有的values,keys将保持不变。该操作只能通过Region API进行操作。 

Region.localInvalidateRegion(); 
localInvalidateRegion方法仅仅对local cache有效。不能清除Replicated Region中的数据。 

清空Region

API: Region.clear(); 
gfsh> remove –region=Region1 –all 
删除Region中所有的数据。该方法对 Partitioned Region无效。 

销毁Region

API: Region.destroyRegion(); 
gfsh> destroy region –name=Region1 
销毁操作中会传播到当前在线的members,如果在执行销毁操作有部会成员已经被关闭,在重启分布式系统的时候会报错。所以最好是在所有成员都在线的状态下执行该操作。当被关闭的成员重启的时候,会等待已经被销毁的Region。解决该问题的方式是,关闭所有的阻塞的成员,然后依次使用gfsh alter disk-store –remove命令删除这些成员的region。然后再重启这些Region。 

关闭Region

Region.close(); 
停止持久化和分区区域的本地缓存,而不关闭整个高速缓存: 
•The close method is called for every callback installed on the region. 
•No events are invoked. Of particular note, the entry events, beforeDestroy and afterDestroy, and the region events, beforeRegionDestroy and afterRegionDestroy, are not invoked. SeeEvents and Event Handling. 
•If persistent, the region is removed from memory but its disk files are retained. 
•If partitioned, the region is removed from the local cache. If the partitioned region is redundant, local data caching fails over to another cache. Otherwise, local data is lost. 

猜你喜欢

转载自blog.csdn.net/huanghuitan/article/details/71632420