我在没有Spring数据的情况下创建了示例Gem示例。
问题:示例运行良好,并连接到Gem-fire服务器,并以java代码返回响应,但当我在gfsh中运行相同的查询时,没有发现任何数据。
gfsh>query --查询=‘select* from /regionA’
结果:真startCount :0 endCount : 20行:0
NEXT_STEP_NAME :结束
然后,我使用单独的类添加了数据,并使用单独的类使用fetch数据,因此在java代码中返回null。
它似乎没有将数据存储到Gem中,在任何日志中都没有错误
package com.vaquar.example1;
import java.util.List;
import org.json.simple.JSONObject;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.pdx.JSONFormatter;
import com.gemstone.gemfire.pdx.PdxInstance;
public final class JSONGemFireClient {
public static final String REGION_NAME = "myPOregion";//"myPOregion";//"regionA";
public ClientCache cache = null;
public JSONGemFireClient() {
//cache = new ClientCacheFactory().set("name", "JSONClient").set("cache-xml-file", "GemFire5.xml").create();
cache = new ClientCacheFactory().set("name", "JSONClient").set("cache-xml-file", "GemFire5.xml").create();
//
/*Cache c1 = new CacheFactory().create();
Region r = c1.createRegionFactory("REPLICATE").create("customers");
*/
System.out.println("cache------------------------->"+cache);
System.out.println("cache------------------------->"+cache.getRegion(REGION_NAME).toString());
}
public void run() throws Exception {
JSONObject obj = null;
System.out.println("Connecting to the distributed system and creating the cache.");
// Get the exampleRegion
Region<String, PdxInstance> jsonregion = cache.getRegion(REGION_NAME);
//
System.out.println("jsonregion----------jsonregion.getFullPath()--------------->"+cache.getRegionAttributes(REGION_NAME)+jsonregion.getFullPath());
//
System.out.println("Example region \"" + jsonregion.getFullPath() + "\" created in cache.");
//
//
// add 5 entries with age = 30
for (int i = 1; i <= 5; i++) {
obj = new JSONObject();
obj.put("name", String.format("Person%s", i));
obj.put("age", 30);
System.out.println("------------------"+obj.toJSONString());
String json=obj.toJSONString();
System.out.println("************************************"+JSONFormatter.fromJSON(json));
jsonregion.put(String.valueOf(i), JSONFormatter.fromJSON(json));
}
//
//
// add 5 entries with age = 20
for (int i = 6; i <= 10; i++) {
obj = new JSONObject();
obj.put("name", String.format("Person%s", i));
obj.put("age", 20);
jsonregion.put(String.valueOf(i), JSONFormatter.fromJSON(obj.toJSONString()));
}
//
//
// Query region
SelectResults<PdxInstance> sr = jsonregion.query("age = 30");
System.out.println("Number of entries where age = 30 is -> " + sr.size());
//
List<PdxInstance> entries = sr.asList();
//
for (PdxInstance val : entries) {
System.out.println("\n** JSON data ** ");
System.out.println("Name = " + val.getField("name"));
System.out.println("Full JSON data -> \n" + JSONFormatter.toJSON(val));
}
cache.close();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JSONGemFireClient test = new JSONGemFireClient();
try {
test.run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}XML GemFire5.xml
<!DOCTYPE client-cache PUBLIC
"-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
"http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
<pool name="client" subscription-enabled="true">
<server host="VKDTMVS760001.vk.vkroot.net" port="40411"/>
<!-- <locator host="localhost" port="10334" /> -->
</pool>
<region name="myPOregion" >
<region-attributes data-policy="persistent-replicate"/><
<!-- <region name="myPOregion">
<region-attributes refid="clientAttributes"/>
</region> -->
</region>
</client-cache> Cache.xml
<?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="40411"/>
<region name="myPOregion">
<region-attributes data-policy="persistent-replicate" />
</region>
</cache> 我创造了一个更简单的例子
package com.vaquar.example1;
import static java.lang.System.out;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
public final class GemFireSample3 {
public static void main(final String[] args) {
//final ClientCache cache = new ClientCacheFactory().create();
Cache cache = new CacheFactory()
.set("cache-xml-file", "HelloWorld.xml")
.create();
System.out.println("cache-------------------->"+cache);
final Region<String, String> sampleRegion = cache.getRegion("regionA");
System.out.println("getMembers"+cache.getMembers());
System.out.println("listRegionAttributes"+cache.listRegionAttributes());
// System.out.println("989898989898"+cache.getResourceManager().toString());
sampleRegion.put("samplekey", "Sample Value test");
//
final String sampleValue = sampleRegion.get("samplekey");
out.println("------------------------------------------------------"+sampleValue);
// sampleRegion.clear();
cache.close();
}
}XML
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
"-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
"http://www.gemstone.com/dtd/cache6_5.dtd">
<cache>
<pool name="cacheServerPool">
<!-- <locator host="localhost" port="40411" />
<locator host="localhost" port="40412" /> -->
<server host="VKDTMVS760001.vk.vkroot.net" port="40411"/>
</pool>
<region name="regionA" >
<region-attributes data-policy="persistent-replicate"/>
</region>
</cache>Jar:
antlr-2.7.7.jar
gemfire-8.0.0.jar
jackson-annotations-2.6.0.jar
jackson-core-2.6.0.jar
jackson-databind-2.6.0.jar
json-simple-1.1.1.jar发布于 2015-11-13 18:42:33
当从gfsh查询时,可以尝试在查询中添加一个投影属性吗?有点像
gfsh>query --query='select name from /regionA'发布于 2015-11-16 08:42:45
发现问题及解决办法
问题: xml文件需要refid=“代理”。
package com.vaquar.example2;
import java.util.List;
import org.json.simple.JSONObject;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.query.SelectResults;
import com.gemstone.gemfire.pdx.JSONFormatter;
import com.gemstone.gemfire.pdx.PdxInstance;
public final class JSONGemFireClient {
public static final String REGION_NAME = "regionB";
/**
* @param args
*/
public static void main(final String[] args) {
//
final JSONGemFireClient test = new JSONGemFireClient();
try {
test.run();
} catch (final Exception e) {
//
e.printStackTrace();
}
}
public ClientCache cache = null;
public JSONGemFireClient() {
//
cache = new ClientCacheFactory().set("name", "JSONClient").set("cache-xml-file", "GemFire6.xml").create();
//
}
public void run() throws Exception {
JSONObject obj = null;
System.out.println("Connecting to the distributed system and creating the cache.");
// Get the exampleRegion
final Region<String, PdxInstance> jsonregion = cache.getRegion(REGION_NAME);
//
System.out.println("jsonregion----------jsonregion.getFullPath()--------------->" + cache.getRegionAttributes(REGION_NAME)
+ jsonregion.getFullPath());
//
System.out.println("Example region \"" + jsonregion.getFullPath() + "\" created in cache.");
//
//
// add 5 entries with age = 30
for (int i = 1; i <= 5; i++) {
obj = new JSONObject();
obj.put("name", String.format("Person%s", i));
obj.put("age", 30);
System.out.println("------------------" + obj.toJSONString());
final String json = obj.toJSONString();
System.out.println("************************************" + JSONFormatter.fromJSON(json));
jsonregion.put(String.valueOf(i), JSONFormatter.fromJSON(json));
}
//
//
// add 5 entries with age = 20
for (int i = 6; i <= 10; i++) {
obj = new JSONObject();
obj.put("name", String.format("Person%s", i));
obj.put("age", 20);
jsonregion.put(String.valueOf(i), JSONFormatter.fromJSON(obj.toJSONString()));
}
//
//
// Query region
final SelectResults<PdxInstance> sr = jsonregion.query("age = 30");
System.out.println("Number of entries where age = 30 is -> " + sr.size());
//
final List<PdxInstance> entries = sr.asList();
//
for (final PdxInstance val : entries) {
System.out.println("\n** JSON data ** ");
System.out.println("Name = " + val.getField("name"));
System.out.println("Full JSON data -> \n" + JSONFormatter.toJSON(val));
}
cache.close();
}
}XML
<!DOCTYPE client-cache PUBLIC
"-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
"http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
<pool name="cacheServerPool">
<locator host="localhost" port="10334" />
</pool>
<region name="regionB" refid="PROXY">
<region-attributes refid="PROXY" data-policy="replicate">
</region-attributes>
</region>
</client-cache>不需要Cache.xml。
结果和命令:
start locator --name=locator1 --port=10334 --J=-Xms256m --J=-Xmx256m
start server --name=server1 --server-port=40411
start server --name=server2 --server-port=40412
gfsh>create region --name=regionB --type=LOCAL
Member | Status
------- | --------------------------------------
server1 | Region "/regionB" created on "server1"
gfsh>list regions
List of regions
---------------
myPOregion
regionA
regionB
regionC
gfsh>query --query='select * from /regionB'
Result : true
startCount : 0
endCount : 20
Rows : 10
age | name
--- | --------
30 | Person1
30 | Person3
30 | Person2
20 | Person8
20 | Person9
30 | Person4
30 | Person5
20 | Person6
20 | Person7
20 | Person10
NEXT_STEP_NAME : END第二个示例修复:
package com.vaquar.example2;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
public final class GemFireSample3 {
public static void main(final String[] args) {
final ClientCache cache = new ClientCacheFactory().set("cache-xml-file", "HelloWorld1.xml").create();
final Region<String, String> sampleRegion = cache.getRegion("pingTestRegion");
sampleRegion.put("samplekey", "Sample Value Test");
//
final String sampleValue = sampleRegion.get("samplekey");
System.out.println("sampleValue"+sampleValue);
// sampleRegion.clear();
cache.close();
}
}XML
<!DOCTYPE client-cache PUBLIC
"-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
"http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
<pool name="cacheServerPool">
<locator host="localhost" port="10334" />
</pool>
<region name="pingTestRegion" refid="PROXY">
<region-attributes refid="PROXY" data-policy="replicate">
<key-constraint>java.lang.String</key-constraint>
<value-constraint>java.lang.String</value-constraint>
</region-attributes>
</region>
<!-- </region> -->
</client-cache>结果
gfsh>create region --name=pingTestRegion --type=LOCAL
Member | Status
------- | ---------------------------------------------
server1 | Region "/pingTestRegion" created on "server1"
gfsh>query --query='select * from /pingTestRegion'
Result : true
startCount : 0
endCount : 20
Rows : 1
Result
-----------------
Sample Value Test
NEXT_STEP_NAME : END
gfsh>发布于 2015-11-17 09:41:42
我试过DTO的例子
package com.viquar.gem.fire.example;
import static java.lang.System.out;
import java.util.Calendar;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
public class GemFireDTOExample {
public static void main(final String[] args) {
// final ClientCache cache = new ClientCacheFactory().create();
final ClientCache cache = new ClientCacheFactory().set("cache-xml-file", "gemfiredtoexample.xml").create();
System.out.println("cache-------------------->" + cache);
final Region<Employee, Address> sampleRegion = cache.getRegion("dtoTestRegion");
final Employee emp1 = getEmployee(1L, "shahbaz", 16, 1990);
final Address add1 = getAddress("B-12", "M.G.ROAD", "PUNE");
sampleRegion.put(emp1, add1);
//
final Address sampleValue = sampleRegion.get(emp1);
out.println("------------------------------------------------------" + sampleValue);
// sampleRegion.clear();
cache.close();
}
private static Address getAddress(final String blockName, final String street, final String city) {
final Address add = new Address();
add.setBlockName(blockName);
add.setCity(city);
add.setStreet(street);
return add;
}
private static Employee getEmployee(final Long empId, final String name, final int date, final int year) {
final Employee emp = new Employee();
emp.setEmpId(empId);
emp.setName(name);
final Calendar dateOfBirth = Calendar.getInstance();
dateOfBirth.set(year, Calendar.DECEMBER, date);
emp.setDateOfBirth(dateOfBirth.getTime());
return emp;
}
}XML:
<!DOCTYPE client-cache PUBLIC
"-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
"http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
<pool name="cacheServerPool">
<locator host="localhost" port="10334" />
</pool>
<region name="dtoTestRegion" refid="PROXY">
<region-attributes refid="PROXY" data-policy="replicate">
<key-constraint>com.viquar.gem.fire.example.Employee</key-constraint>
<value-constraint>com.viquar.gem.fire.example.Address</value-constraint>
</region-attributes>
</region>
</client-cache>然后用DTO创建Jar文件
Address.class Employee.class
现在在以下路径中复制jar文件:
C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster现在运行以下命令
gfsh>deploy --jar=C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar
Could not process command due to GemFire error. 'C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar' not found.
gfsh>deploy --jar=C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar
Member | Deployed JAR | Deployed JAR Location
------- | ---------------- | -----------------------------------------------------------------------------------
server1 | employee_dto.jar | C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\server1\vf.gf#employee_dto.jar#2
server2 | employee_dto.jar | C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\server2\vf.gf#employee_dto.jar#2
gfsh>create region --name=dtoTestRegion --type=REPLICATE --key-constraint=com.viquar.gem.fire.example.Employee --value-constraint=com.viquar.gem.fire.example.Address
Member | Status
------- | --------------------------------------------
server1 | Region "/dtoTestRegion" created on "server1"
server2 | Region "/dtoTestRegion" created on "server2"现在在gfsh中运行下面的查询之后运行您的程序
gfsh>query --query='select * from /dtoTestRegion'
Result : true
startCount : 0
endCount : 20
Rows : 1
blockName | city | street
--------- | ---- | --------
B-12 | PUNE | M.G.ROAD
NEXT_STEP_NAME : END
gfsh>https://stackoverflow.com/questions/33692461
复制相似问题