1. The server side cache.xml is as follows which pre populates some data into the region to save having to write a client to add data.
<?xml version="1.0"?> <!DOCTYPE cache PUBLIC "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN" "http://www.gemstone.com/dtd/cache7_0.dtd"> <cache> <cache-server port="40001" notify-by-subscription="true"/> <region name="exampleRegion"> <region-attributes refid="PARTITION_REDUNDANT"/> <entry><key><string>1</string></key><value><string>MyValue1</string></value></entry> <entry><key><string>2</string></key><value><string>MyValue2</string></value></entry> <entry><key><string>3</string></key><value><string>MyValue3</string></value></entry> <entry><key><string>4</string></key><value><string>MyValue4</string></value></entry> <entry><key><string>5</string></key><value><string>MyValue5</string></value></entry> <entry><key><string>6</string></key><value><string>MyValue6</string></value></entry> <entry><key><string>7</string></key><value><string>MyValue7</string></value></entry> <entry><key><string>8</string></key><value><string>MyValue8</string></value></entry> <entry><key><string>9</string></key><value><string>MyValue9</string></value></entry> <entry><key><string>10</string></key><value><string>MyValue10</string></value></entry> <entry><key><string>11</string></key><value><string>MyValue11</string></value></entry> <entry><key><string>12</string></key><value><string>MyValue12</string></value></entry> <entry><key><string>13</string></key><value><string>MyValue13</string></value></entry> <entry><key><string>14</string></key><value><string>MyValue14</string></value></entry> <entry><key><string>15</string></key><value><string>MyValue15</string></value></entry> <entry><key><string>16</string></key><value><string>MyValue16</string></value></entry> <entry><key><string>17</string></key><value><string>MyValue17</string></value></entry> <entry><key><string>18</string></key><value><string>MyValue18</string></value></entry> <entry><key><string>19</string></key><value><string>MyValue19</string></value></entry> <entry><key><string>20</string></key><value><string>MyValue20</string></value></entry> <entry><key><string>21</string></key><value><string>MyValue21</string></value></entry> <entry><key><string>22</string></key><value><string>MyValue22</string></value></entry> <entry><key><string>23</string></key><value><string>MyValue23</string></value></entry> <entry><key><string>24</string></key><value><string>MyValue24</string></value></entry> <entry><key><string>25</string></key><value><string>MyValue25</string></value></entry> <entry><key><string>26</string></key><value><string>MyValue26</string></value></entry> <entry><key><string>27</string></key><value><string>MyValue27</string></value></entry> <entry><key><string>28</string></key><value><string>MyValue28</string></value></entry> <entry><key><string>29</string></key><value><string>MyValue29</string></value></entry> <entry><key><string>30</string></key><value><string>MyValue30</string></value></entry> </region> </cache>
2. When listing members we have one locator and 2 cache servers.
gfsh>list members; Name | Id -------- | ------------------------------------------------------- server2 | Pas-Apicellas-MacBook-Pro(server2:78035)<v2>:4641 server1 | Pas-Apicellas-MacBook-Pro(server1:78002)<v1>:13379 locator1 | Pas-Apicellas-MacBook-Pro(locator1:77992:locator):23631
3. Get a query to ensure we indeed have 30 entries in the region.
gfsh>query --query="select count(*) from /exampleRegion"; Result : true startCount : 0 endCount : 20 Rows : 1 Result ------ 30 NEXT_STEP_NAME : END
4. Create an XML cache.xml file which will enable us to connect as a cache server to the Distributed System with storage disabled.
<?xml version="1.0"?> <!DOCTYPE cache PUBLIC "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN" "http://www.gemstone.com/dtd/cache7_0.dtd"> <cache> <cache-server port="0" /> <region name="exampleRegion" > <region-attributes data-policy="partition" > <partition-attributes local-max-memory="0" redundant-copies="1"/> <subscription-attributes interest-policy="all"/> </region-attributes> </region> </cache>
5. Write a java client with code as follows
package vmware.au.gemfire.demos.deptemp; import java.util.Map; import java.util.Set; import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.CacheFactory; import com.gemstone.gemfire.cache.Region; import com.gemstone.gemfire.cache.partition.PartitionRegionHelper; import com.gemstone.gemfire.distributed.DistributedMember; public class VerifyDataLocations { private Cache cache = null; public VerifyDataLocations() { CacheFactory cf = new CacheFactory(); cf.set("cache-xml-file", "xml/datalocations-cache-no-storage.xml"); cf.set("locators", "localhost[10334]"); cache = cf.create(); } public void run() throws InterruptedException { Region<String,String> exampleRegion = cache.getRegion("exampleRegion"); System.out.println("exampleRegion size = " + exampleRegion.size()); Set<Map.Entry<String, String>> entries = exampleRegion.entrySet(); for (Map.Entry entry: entries) { DistributedMember member = PartitionRegionHelper.getPrimaryMemberForKey(exampleRegion, (String) entry.getKey()); System.out.println (String.format("\"Primary Member [Host=%s, Id=%s - Key=%s, Value=%s]", member.getHost(), member.getId(), entry.getKey(), (String) entry.getValue())); } System.out.println("Sleeping for 20 seconds.."); cache.close(); } /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub VerifyDataLocations test = new VerifyDataLocations(); test.run(); } }
6. Output as follows.
exampleRegion size = 30
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002)
No comments:
Post a Comment