我在没有 Spring Data 的情况下创建了示例 Gem fire 示例。
问题:我的示例工作正常并连接到 Gem-fire 服务器,并在 java 代码中返回响应,但是当我在 gfsh 中运行相同的查询时,没有找到数据
gfsh>query --query='从/regionA 选择 *'
结果:正确 开始计数:0 结束计数:20 行数:0
NEXT_STEP_NAME:END
然后我使用单独的类添加了数据,并使用单独的类获取数据,因此在 java 代码中返回 null。
看起来它没有将数据存储到 Gem-fire 中,任何日志中都没有错误
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>
缓存.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
最佳答案
当从 gfsh 查询时,您可以尝试在查询中添加投影属性吗?类似的东西
gfsh>query --query='select name from /regionA'
关于java - Pivotal Gemfire 8.1 将数据不保存数据放入Gemfire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692461/