Pivotal Gemfire 8.1将数据保存到Gemfire中

时间:2015-11-13 12:09:49

标签: java gemfire

我创建了没有Spring Data的示例Gem fire示例。

问题:我的示例工作正常并连接到Gem-fire服务器,并在java代码中返回响应,但是当我在gfsh中运行相同的查询时,则找不到数据

gfsh> query --query ='select * from / regionA'

结果:是的 startCount:0 endCount:20 行:0

NEXT_STEP_NAME:结束

然后我使用单独的类添加数据并使用单独的类使用获取数据,因此在java代码中返回null。

似乎没有将数据存储到Gem-fire中,任何日志中都没有错误

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    delay(5.0) {
        self.sum1TimeLabel?.hidden = false
    }

    delay(30.0) {
        self.sum2TimeLabel?.hidden = false
    }

    delay(60.0) {
        self.sum3TimeLabel?.hidden = false
    }
}

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

XML GemFire5.xml

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();
        }
    }
}

Cache.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 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>  

XML

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 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>

5 个答案:

答案 0 :(得分:0)

从gfsh查询时,您可以尝试在查询中添加投影属性吗?像

这样的东西
gfsh>query --query='select name from /regionA'

答案 1 :(得分:0)

找到问题和解决方案

问题:在xml文件中需要refid =“PROXY”。

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>

答案 2 :(得分:0)

我试过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>

答案 3 :(得分:0)

您可以使用Spring Data。 分享示例enter image description here,如下所示 -

<强>的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>WebserviceSpringDataGemFire</groupId>
  <artifactId>WebserviceSpringDataGemFire</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>WebserviceSpringDataGemFire</name>
 <properties>
        <spring.version>4.1.7.RELEASE</spring.version>
        <jackson.version>1.9.10</jackson.version>
        <jdk.version>1.7</jdk.version>
    </properties>

    <dependencies>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>       
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.5.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons-core</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-gemfire</artifactId>
            <version>1.2.0.RELEASE</version>
        </dependency>

        <dependency>
          <groupId>com.gemstone.gemfire</groupId>
          <artifactId>gemfire</artifactId>
          <version>8.0.0</version>
       </dependency>    

        <dependency>
            <!-- Required for Log4J XML configuration files -->
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>commons-chain</groupId>
            <artifactId>commons-chain</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency> 
    </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

<强>的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>WebserviceSpringDataGemFire</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

<强> MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="spring.gemfire.repository.deptemp"/>

</beans>

<强> gemfirebean1.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:context="http://www.springframework.org/schema/context"
    xmlns:gfe="http://www.springframework.org/schema/gemfire"  
    xmlns:gfe-data="http://www.springframework.org/schema/data/gemfire"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/gemfire 
        http://www.springframework.org/schema/gemfire/spring-gemfire-1.2.xsd  
        http://www.springframework.org/schema/data/gemfire 
        http://www.springframework.org/schema/data/gemfire/spring-data-gemfire-1.2.xsd">

    <context:component-scan base-package="spring.gemfire.repository.deptemp"/>

    <gfe:client-cache id="client-cache" cache-xml-location="classpath:gemfire-client.xml" pool-name="gfPool"/>

    <gfe:pool id="gfPool" max-connections="10">  
        <gfe:locator host="localhost" port="10334"/>        
    </gfe:pool>   



    <gfe:lookup-region id="departments" name="departments" cache-ref="client-cache"/>  
    <gfe:lookup-region id="employees" name="employees" cache-ref="client-cache"/>  
    <gfe-data:repositories base-package="spring.gemfire.repository.deptemp.repos"  />  

</beans> 

<强>的GemFire-client.xml的

<!DOCTYPE client-cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>      
    <!-- No cache storage in the client region because of the PROXY client region shortcut setting. -->  
<!--    <pool name="cacheServerPool">
        <locator host="GCOTDVMR770036.nam.nsroot.net" port="10334" />
    </pool> -->
    <region name="departments" refid="PROXY">  
    <!--   <region-attributes refid="PROXY" pool-name="gfPool" data-policy="replicate"/> -->  
      <region-attributes refid="PROXY" data-policy="replicate"/>
    </region>     

    <region name="employees" refid="PROXY">
      <!--   <region-attributes refid="PROXY" pool-name="gfPool" data-policy="replicate"/>   -->
      <region-attributes refid="PROXY" data-policy="replicate"/>
    </region>  
</client-cache>  

<强> Department.java

package spring.gemfire.repository.deptemp.beans;

import java.io.Serializable;
import java.util.Properties;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import com.gemstone.gemfire.cache.Declarable;

@Region("departments")
public class Department implements Declarable, Serializable {
    private static final long   serialVersionUID    = -9097335119586059309L;
    @Id
    private int                 deptno;
    private String              name;

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) { return true; }
        if (obj == null) { return false; }
        if (getClass() != obj.getClass()) { return false; }
        final Department other = (Department) obj;
        if (deptno != other.deptno) { return false; }
        if (name == null) {
            if (other.name != null) { return false; }
        } else if (!name.equals(other.name)) { return false; }
        return true;
    }

    public int getDeptno() {
        return deptno;
    }

    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    public void init(final Properties props) {
        // TODO Auto-generated method stub
        deptno = Integer.parseInt(props.getProperty("deptno"));
        name = props.getProperty("name");
    }

    public void setDeptno(final int deptno) {
        this.deptno = deptno;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Department [deptno=" + deptno + ", name=" + name + "]";
    }
}

<强> Employee.java

package spring.gemfire.repository.deptemp.beans;

import java.io.Serializable;
import java.util.Properties;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.Region;
import com.gemstone.gemfire.cache.Declarable;

@Region("employees")
public class Employee implements Declarable, Serializable {
    private static final long   serialVersionUID    = -8229531542107983344L;
    @Id
    private int                 empno;
    private String              name;
    private String              job;
    private int                 deptno;

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) { return true; }
        if (obj == null) { return false; }
        if (getClass() != obj.getClass()) { return false; }
        final Employee other = (Employee) obj;
        if (deptno != other.deptno) { return false; }
        if (empno != other.empno) { return false; }
        if (job == null) {
            if (other.job != null) { return false; }
        } else if (!job.equals(other.job)) { return false; }
        if (name == null) {
            if (other.name != null) { return false; }
        } else if (!name.equals(other.name)) { return false; }
        return true;
    }

    public int getDeptno() {
        return deptno;
    }

    public int getEmpno() {
        return empno;
    }

    public String getJob() {
        return job;
    }

    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + empno;
        result = prime * result + ((job == null) ? 0 : job.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    public void init(final Properties props) {
        // TODO Auto-generated method stub
        empno = Integer.parseInt(props.getProperty("empno"));
        name = props.getProperty("name");
        job = props.getProperty("job");
        deptno = Integer.parseInt(props.getProperty("deptno"));
    }

    public void setDeptno(final int deptno) {
        this.deptno = deptno;
    }

    public void setEmpno(final int empno) {
        this.empno = empno;
    }

    public void setJob(final String job) {
        this.job = job;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Employee [empno=" + empno + ", name=" + name + ", job=" + job + ", deptno=" + deptno + "]";
    }
}

<强> DeptRepository.java

package spring.gemfire.repository.deptemp.repos;

import java.util.Collection;

import org.springframework.data.gemfire.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import spring.gemfire.repository.deptemp.beans.Department;

//@Repository(DeptRepository.BEAN_ID)
public interface DeptRepository extends CrudRepository<Department, Integer> {
      String BEAN_ID="deptRepository";

    Department findByName(String name);

    @Query("SELECT * FROM /departments")
    Collection<Department> myFindAll();
}

<强> EmpRepository.java

package spring.gemfire.repository.deptemp.repos;

import java.util.Collection;

import org.springframework.data.gemfire.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import spring.gemfire.repository.deptemp.beans.Employee;

//@Repository(DeptRepository.BEAN_ID)
public interface EmpRepository extends CrudRepository<Employee, Integer> {
    String BEAN_ID="empRepository";

    @Query("SELECT * FROM /employees where deptno = $1")
    Collection<Employee> empsInDeptno(int deptno);

    @Query("SELECT * FROM /employees")
    Collection<Employee> myFindAll();
}

<强> SpringFactoryConfig.java

package spring.gemfire.repository.deptemp.util;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class SpringFactoryConfig {
    private static String               DEFAULT_CONFIG_FILE = "gemfirebean1.xml";
    private static SpringFactoryConfig  INSTANCE;

    public static final BeanFactory getDefaultSpringBeanFactory() {
        if (INSTANCE == null) {
            INSTANCE = new SpringFactoryConfig(DEFAULT_CONFIG_FILE);
        }
        return INSTANCE.getBeanFactory();
    }

    public static SpringFactoryConfig getSpringFactoryConfig(final String configFile) {
        return new SpringFactoryConfig(configFile);
    }

    private final ApplicationContext    springAppContext;

    private SpringFactoryConfig(final String configFile) {
        springAppContext = new ClassPathXmlApplicationContext("classpath:" + configFile);
        if (springAppContext == null) { throw new IllegalStateException("Cannot get an instance of the Spring bean factory !"); }
    }

    public final <T> T getBean(final String name, final Class<T> beanType) {
        if (springAppContext.isTypeMatch(name, beanType)) {
            return beanType.cast(springAppContext.getBean(name));
        } else {
            throw new IllegalArgumentException("Bean " + name + " is not of appropriate type " + beanType);
        }
    }

    public final BeanFactory getBeanFactory() {
        return springAppContext;
    }
}

<强> Test.java

package spring.gemfire.repository.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring.gemfire.repository.deptemp.beans.Department;
import spring.gemfire.repository.deptemp.beans.Employee;
import spring.gemfire.repository.deptemp.repos.DeptRepository;
import spring.gemfire.repository.deptemp.repos.EmpRepository;

public class Test {
    public static void main(final String[] args) {
        // TODO Auto-generated method stub
        final Test t = new Test();
        // t.deleteData();
        // t.saveData();
        t.getData();
        System.out.println("all done..");
    }

    private ConfigurableApplicationContext  ctx = null;

    public Test() {
        ctx = new ClassPathXmlApplicationContext("mvc-dispatcher-servlet.xml");
    }

    public void deleteData() {
        final DeptRepository deptRepos = ctx.getBean(DeptRepository.class);
        // get quick size
        System.out.println("** Size of dept repository **");
        System.out.println("Size = " + deptRepos.count());
        // deptRepos.deleteAll();
        final Collection<Department> deps = deptRepos.myFindAll();
        deptRepos.delete(deps);
        for (final Department d : deps) {
            System.out.println(d.toString());
            // deptRepos.delete(d);
        }
        System.out.println("** dept repository deleted All**");
        final EmpRepository empRepos = ctx.getBean(EmpRepository.class);
        // get quick size
        System.out.println("** Size of emp repository **");
        System.out.println("Size = " + empRepos.count());
        // empRepos.deleteAll();
        final Collection<Employee> emps = empRepos.myFindAll();
        empRepos.delete(emps);
        for (final Employee e : emps) {
            System.out.println(e.toString());
            // empRepos.delete(e);
        }
        System.out.println("** empRepos repository deleted All**");
    }

    public void getData() {
        final DeptRepository deptRepos = ctx.getBean(DeptRepository.class);
        // get quick size
        System.out.println("** Size of dept repository **");
        System.out.println("Size = " + deptRepos.count());
        // call findOne crud method by key
        System.out.println("** calling  deptRepos.findOne(\"2\") **");
        final Department dept = deptRepos.findOne(5);
        System.out.println(dept);
        // call findOne crud method for an attribute
        System.out.println("** calling  deptRepos.findByName(\"B\") **");
        final Department dept2 = deptRepos.findByName("B");
        System.out.println(dept2);
        // call my own findAll
        final Collection<Department> deps = deptRepos.myFindAll();
        System.out.println("\n** All Departments using -> deptRepos.myFindAll()");
        System.out.println("Defined as : @Query(\"SELECT * FROM /departments\") ");
        System.out.println("Collection<Department> myFindAll(); ** ");
        for (final Department d : deps) {
            System.out.println(d.toString());
        }
        final EmpRepository empRepos = ctx.getBean(EmpRepository.class);
        // get quick size
        System.out.println("** Size of emp repository **");
        System.out.println("Size = " + empRepos.count());
        final Employee empt = empRepos.findOne(2);
        System.out.println(empt);
        final Collection<Employee> emps = empRepos.empsInDeptno(2);
        System.out.println("\n ** All Employees in dept 2 using -> Collection<Employee> empsInDeptno(int deptno) **");
        for (final Employee e : emps) {
            System.out.println(e.toString());
        }
    }

    public void saveData() {
        final DeptRepository deptRepos = ctx.getBean(DeptRepository.class);
        // get quick size
        System.out.println("** Size of dept repository **");
        System.out.println("Size = " + deptRepos.count());
        final Department d1 = new Department();
        d1.setDeptno(1);
        d1.setName("A");
        final Department d2 = new Department();
        d2.setDeptno(2);
        d2.setName("B");
        final Department d3 = new Department();
        d3.setDeptno(5);
        d3.setName("C");
        final List<Department> dList = new ArrayList<Department>();
        dList.add(d1);
        dList.add(d2);
        dList.add(d3);
        System.out.println("** calling  deptRepos.save() **");
        final Iterable<Department> deptCollection = deptRepos.save(dList);
        for (final Department d : deptCollection) {
            System.out.println("** saved department **" + d);
        }
        final EmpRepository empRepos = ctx.getBean(EmpRepository.class);
        // get quick size
        System.out.println("** Size of emp repository **");
        System.out.println("Size = " + empRepos.count());
        final Employee e1 = new Employee();
        e1.setEmpno(1);
        e1.setName("X");
        e1.setDeptno(1);
        e1.setJob("J1");
        final Employee e2 = new Employee();
        e2.setEmpno(2);
        e2.setName("Y");
        e2.setDeptno(2);
        e2.setJob("J2");
        final Employee e3 = new Employee();
        e3.setEmpno(3);
        e3.setName("Y");
        e3.setDeptno(5);
        e3.setJob("J5");
        final List<Employee> eList = new ArrayList<Employee>();
        eList.add(e1);
        eList.add(e2);
        eList.add(e3);
        System.out.println("** calling  empRepos.save() **");
        final Iterable<Employee> empCollection = empRepos.save(eList);
        for (final Employee e : empCollection) {
            System.out.println("** saved employee **" + e);
        }
    }
}

<强> GemFireJSONController.java

package spring.gemfire.repository.deptemp.controller;


import java.util.Collection;





import javax.annotation.PostConstruct;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import spring.gemfire.repository.deptemp.beans.Department;
import spring.gemfire.repository.deptemp.beans.Employee;
import spring.gemfire.repository.deptemp.repos.DeptRepository;
import spring.gemfire.repository.deptemp.repos.EmpRepository;
import spring.gemfire.repository.deptemp.util.SpringFactoryConfig;


@Controller
@RequestMapping("/gemfire")
public class GemFireJSONController {


     @RequestMapping(value ="/create/employee", method = RequestMethod.POST,headers="Accept=application/json")
        public   ResponseEntity<?>  createEmployee(@RequestBody Employee emp) {
            System.out.println("Start createEmployee.");

            getEmpRepository().save(emp);

              return new ResponseEntity<>(emp, HttpStatus.CREATED);
        }

     @RequestMapping(value ="/create/department", method = RequestMethod.POST,headers="Accept=application/json")
        public   ResponseEntity<?>  createDepartment(@RequestBody Department dept) {
            System.out.println("Start createEmployee.");
            getDeptRepository().save(dept);
              return new ResponseEntity<>(dept, HttpStatus.CREATED);
        }

      @RequestMapping(value = "/employee/all", method = RequestMethod.GET,headers="Accept=application/json")
        public @ResponseBody Collection<Employee> getAllEmployees() {
          System.out.println("Start getAllEmployeess.");      


            return getEmpRepository().myFindAll();
        }

      @RequestMapping(value = "/department/all", method = RequestMethod.GET,headers="Accept=application/json")
        public @ResponseBody Collection<Department> getAllDepartments() {
          System.out.println("Start getAllDepartments.");          

            return getDeptRepository().myFindAll();
        }

      private DeptRepository getDeptRepository(){
             return SpringFactoryConfig.getDefaultSpringBeanFactory().getBean(DeptRepository.BEAN_ID,
                     DeptRepository.class);
      }

      private EmpRepository getEmpRepository(){
             return SpringFactoryConfig.getDefaultSpringBeanFactory().getBean(EmpRepository.BEAN_ID,
                     EmpRepository.class);
      }


}

答案 4 :(得分:0)

有用的命令&amp;链接 -

start locator --name=locator1 --port=10334 --initial-heap=256m --max-heap=256m

start server --name=server1 --server-port=40411
start server --name=server2 --server-port=40412
create region --name=regionA --type=REPLICATE_PERSISTENT

connect --locator=locator1
connect --locator=localhost[10334]

--Start locator 1 using  gfsh on node1
--to get mcast ip addres command for windows (netsh interface ip show joins)take scope 0 and for unix (netstat -g) take bond0

start locator --name=locator1 --bind-address=node1 --port=62001 --locators=node1[62001],node2[62001] --log-level=info --mcast-address=239.192.81.1 --mcast-port=10334 --dir=/export/vol01/enterprise-docs/gemfireWorkspace/locator1 --enable-cluster-configuration=true --load-cluster-configuration-from-dir=true --cluster-config-dir=/export/vol01/enterprise-docs/gemfireWorkspace/clusterWorkspace

--Start locator 2 using  gfsh on node2

start locator --name=locator2 --bind-address=node2 --port=62001 --locators=node1[62001],node2[62001] --log-level=info --mcast-address=239.192.81.1 --mcast-port=10334 --dir=/export/vol01/enterprise-docs/gemfireWorkspace/locator2 --enable-cluster-configuration=true --load-cluster-configuration-from-dir=true --cluster-config-dir=/export/vol01/enterprise-docs/gemfireWorkspace/clusterWorkspace


list members
list regions

query --query='select * from /regionA'

deploy --jar=C:\devprogs\Pivotal_GemFire_800_b48398_Windows\bin\locator1\cluster_config\cluster\employee_dto.jar

list deployed

undeploy --jar=employee_dto.jar

create region --name=dtoTestRegion --type=REPLICATE --key-constraint=com.gem.fire.example.Employee --value-constraint=com.gem.fire.example.Address

describe region --name=regionA

remove --region=exampleRegion --all

stop server --name=server1

stop locator --name=locator1

disconnect

shutdown

start pulse
admin/admin

--command to modify cluster.xml - it works only on unix
gfsh>export cluster-configuration --zip-file-name=/export/vol01/enterprise-docs/gemfireWorkspace/clusterWorkspace/cluster_config/cluster/cluster.xml.zip
for import need to follow below steps as first shutdown servers, only locators need to up after import start the servers, 
zip file contains cluster folder with cluster.xml with cluster.properties that modified to import

gfsh> shutdown
gfsh>import cluster-configuration --zip-file-name=/tmp/cluster.xml_1.zip
gfsh> start server --name=server1 --server-port=40411
gfsh> start server --name=server2 --server-port=40412
---------


http://gemfire.docs.pivotal.io/latest/getting_started/15_minute_quickstart_gfsh.html
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/developing/region_options/region_types.html
http://pubs.vmware.com/vfabric53/index.jsp?topic=/com.vmware.vfabric.gemfire.7.0/getting_started/gemfire_tutorial/running_tutorial.html

https://github.com/Pivotal-Field-Engineering/BigDataRoadshow-labs/tree/master/labs/gemfire
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/deploying/gfsh/commands/put.html
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/getting_started/querying_quick_reference.html
http://gemfire.docs.pivotal.io/latest/tools_modules/gfsh/command-pages/put.html

http://gemfire702.docs.pivotal.io/7.0.2/userguide/reference/topics/region_shortcuts_reference.html
https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/developing/querying_basics/query_basics.html
http://gemfire.docs.pivotal.io/latest/geode_rest/chapter_overview.html

http://stackoverflow.com/questions/33692461/pivotal-gemfire-8-1-put-data-not-saving-data-into-gemfire/33753579#33753579

http://theblasfrompas.blogspot.in/2012/11/spring-data-repository-for-gemfire-7.html
http://theblasfrompas.blogspot.in/2014/09/rest-with-pivotal-gemfire-80.html

http://docs.spring.io/spring-data-gemfire/docs/current/reference/html/

https://support.pivotal.io/hc/communities/public/questions/204438137-How-to-create-subregion-from-gfsh-in-the-cluster-configuration

https://github.com/olivergierke/spring-gemfire/blob/master/src/asciidoc/reference/region.adoc

https://maven-repository.com/artifact/com.gemstone.gemfire/gemfire/8.2.0

http://community.gemstone.com/display/gemfire/Querying

-----spring url

http://howtodoinjava.com/2015/01/23/how-to-use-spring-component-repository-service-and-controller-annotations/
http://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan