昨天研究了半天apache geode,通過gfsh命令查詢和操作比較簡單,但是如何通過程序查詢因為資歷太少,沒有搞懂,試驗了半天才明白
創(chuàng)新互聯(lián)是專業(yè)的輝南網(wǎng)站建設(shè)公司,輝南接單;提供網(wǎng)站設(shè)計制作、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行輝南網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
1.創(chuàng)建測試region
create region --name=user --type=REPLICATE_PERSISTENT
2.用eclipse創(chuàng)建maven工程
修改pom.xml,需要引入gemfire-8.2.0.jar,這個在
4.0.0 io.proinsight hellogeode 0.0.1-SNAPSHOT jar hellogeode http://maven.apache.org UTF-8 org.apache.geode geode-core 1.2.0 org.apache.geode geode-json 1.2.0 org.apache.geode geode-common 1.2.0 log4j log4j 1.2.17 org.apache.logging.log4j log4j-core 2.8.2 org.apache.logging.log4j log4j-api 2.8.2 log4j log4j 1.2.17 commons-codec commons-codec 1.10 org.apache.commons commons-lang3 3.4 junit junit 4.12 com.alibaba fastjson 1.2.13 org.apache.maven.plugins maven-compiler-plugin 3.3 1.8 org.apache.maven.plugins maven-jar-plugin 2.6 true lib/ io.proinsight.hellogeode.App org.apache.maven.plugins maven-dependency-plugin 2.10 copy-dependencies package copy-dependencies ${project.build.directory}/lib
新建UserBean.java
package io.proinsight.hellogeode; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.util.Date; import org.apache.geode.DataSerializable; import org.apache.geode.DataSerializer; public class UserBean implements DataSerializable{ /** * 必須有這個方法,否則查詢會報沒有init()這個方法 * */ public UserBean() { } public UserBean(int id2, int i, String string, Date date) { // TODO Auto-generated constructor stub this.id=id2; this.age=i; this.name=string; this.createTime=date; } /** * id * */ private int id; /** * 年齡 * */ private int age; /** * 姓名 * */ private String name; /** * 創(chuàng)建時間 * */ private Date createTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public void fromData(DataInput in) throws IOException, ClassNotFoundException { this.id = in.readInt(); this.age=in.readInt(); this.name = in.readUTF(); this.createTime = DataSerializer.readDate(in); } public void toData(DataOutput out) throws IOException { out.writeInt(this.id); out.writeInt(this.age); out.writeUTF(this.name); DataSerializer.writeDate(this.createTime, out); } }
修改App.java
package io.proinsight.hellogeode; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.apache.geode.DataSerializer; import org.apache.geode.cache.Region; import org.apache.geode.cache.RegionFactory; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientCacheFactory; import org.apache.geode.cache.client.ClientRegionFactory; import org.apache.geode.cache.client.ClientRegionShortcut; import org.apache.geode.cache.query.internal.ResultsBag; import com.alibaba.fastjson.JSON; /** * Hello world! * */ public class App { String regionName="user"; int userNum=10; public static void main( String[] args ) { new App().test(); } public void test(){ Region region = null; ClientCache cache = new ClientCacheFactory().addPoolLocator("192.168.60.50", 10334).create(); ClientRegionFactory rf = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY); //打開user表 region = rf.create(regionName); System.out.println("開始創(chuàng)建用戶"); create(region); select(region); System.out.println("完成創(chuàng)建用戶"); System.out.println("開始修改用戶"); update(region); select(region); System.out.println("完成修改用戶"); System.out.println("開始刪除用戶"); delete(region,108); select(region); System.out.println("完成刪除用戶"); System.out.println("開始清空表"); truncate(region); select(region); System.out.println("完成清空表"); //關(guān)閉表 region.close(); } /** * 查詢user表 * */ public void select(Region region){ try { Object objList = region.query("select * from /"+regionName+" u where u.age > 15"); if(objList != null && objList instanceof ResultsBag){ Iterator iter = ((ResultsBag) objList).iterator(); while(iter.hasNext()){ UserBean userBean = (UserBean) iter.next(); System.out.println("User信息:"+JSON.toJSONString(userBean)); } } Object obj = region.get(108); if(obj != null && obj instanceof UserBean){ System.out.println("User108信息"+JSON.toJSONString(obj)); } }catch (Exception e){ e.printStackTrace(); // logger.error("error occured.", e); } } /** * 增加10個 * */ public void create(Region region){ for (int i = 0; i < userNum; i++) { int id = i+100; region.put(id, new UserBean(id,10+i,"username:"+id,new Date())); } } /** * 修改 * */ public void update(Region region){ UserBean user108= (UserBean) region.get(108); if(user108 != null && user108 instanceof UserBean){ System.out.println("User108信息"+JSON.toJSONString(user108)); //修改年齡為 user108.setAge(12); region.put(user108.getId(), user108); } } /** * 刪除某個用戶 * */ public void delete(Region region,Integer id){ region.remove(id); } /** * 清空表 * */ public void truncate(Region region){ region.clear(); } }
3.打包
maven install一下
在target目錄下有對應(yīng)的jar包
然后把hellogeode-0.0.1-SNAPSHOT.jar拷貝到geode服務(wù)器上某個目錄例如/tmp/t/下
4.發(fā)布Jar到geode里
為啥要發(fā)布到geode里呢?因為我們用UserBean的序列化和反序列化作為表結(jié)構(gòu),因此deode查詢時需要UserBean.class,因此我們發(fā)布hellogeode-0.0.1-SNAPSHOT.jar到geode,主要是為了把UserBean.class導(dǎo)入到geode里;
gfsh>deploy --jar=/tmp/t/hellogeode-0.0.1-SNAPSHOT.jar Member | Deployed JAR | Deployed JAR Location -------- | ----------------------------- | ----------------------------------------------------------------- server50 | hellogeode-0.0.1-SNAPSHOT.jar | /opt/apache-geode-1.2.1/server50/hellogeode-0.0.1-SNAPSHOT.v1.jar
若是我們對UserBean有修改的話,需要先卸載jar,然后再重新發(fā)布
gfsh>undeploy --jar=hellogeode-0.0.1-SNAPSHOT.jar Member | Un-Deployed JAR | Un-Deployed From JAR Location -------- | ----------------------------- | ----------------------------------------------------------------- server50 | hellogeode-0.0.1-SNAPSHOT.jar | /opt/apache-geode-1.2.1/server50/hellogeode-0.0.1-SNAPSHOT.v1.jar gfsh>deploy --jar=/tmp/t/hellogeode-0.0.1-SNAPSHOT.jar Member | Deployed JAR | Deployed JAR Location -------- | ----------------------------- | ----------------------------------------------------------------- server50 | hellogeode-0.0.1-SNAPSHOT.jar | /opt/apache-geode-1.2.1/server50/hellogeode-0.0.1-SNAPSHOT.v1.jar
5.測試
測試程序在eclipse里運行即可,不需要放到服務(wù)器上運行
在eclipse運行app.java
測試結(jié)果
開始創(chuàng)建用戶 [info 2017/09/27 13:09:13.422 CSTtid=0x17] AutoConnectionSource discovered new locators [slave1/211.98.71.195:10334] [info 2017/09/27 13:09:13.511 CST tid=0x1] Updating membership port. Port changed from 0 to 51,084. ID is now bogon(92040:loner):0:0c29bdc1 User信息:{"age":12,"createTime":1506488953578,"id":102,"name":"username:102"} User信息:{"age":15,"createTime":1506488953592,"id":105,"name":"username:105"} User信息:{"age":14,"createTime":1506488953588,"id":104,"name":"username:104"} User信息:{"age":10,"createTime":1506488953405,"id":100,"name":"username:100"} User信息:{"age":19,"createTime":1506488953611,"id":109,"name":"username:109"} User信息:{"age":13,"createTime":1506488953583,"id":103,"name":"username:103"} User信息:{"age":18,"createTime":1506488953606,"id":108,"name":"username:108"} User信息:{"age":11,"createTime":1506488953573,"id":101,"name":"username:101"} User信息:{"age":16,"createTime":1506488953597,"id":106,"name":"username:106"} User信息:{"age":17,"createTime":1506488953602,"id":107,"name":"username:107"} User108信息{"age":18,"createTime":1506488953606,"id":108,"name":"username:108"} 完成創(chuàng)建用戶 開始修改用戶 User108信息{"age":18,"createTime":1506488953606,"id":108,"name":"username:108"} User信息:{"age":17,"createTime":1506488953602,"id":107,"name":"username:107"} User信息:{"age":12,"createTime":1506488953578,"id":102,"name":"username:102"} User信息:{"age":15,"createTime":1506488953592,"id":105,"name":"username:105"} User信息:{"age":12,"createTime":1506488953606,"id":108,"name":"username:108"} User信息:{"age":13,"createTime":1506488953583,"id":103,"name":"username:103"} User信息:{"age":19,"createTime":1506488953611,"id":109,"name":"username:109"} User信息:{"age":16,"createTime":1506488953597,"id":106,"name":"username:106"} User信息:{"age":11,"createTime":1506488953573,"id":101,"name":"username:101"} User信息:{"age":14,"createTime":1506488953588,"id":104,"name":"username:104"} User信息:{"age":10,"createTime":1506488953405,"id":100,"name":"username:100"} User108信息{"age":12,"createTime":1506488953606,"id":108,"name":"username:108"} 完成修改用戶 開始刪除用戶 User信息:{"age":15,"createTime":1506488953592,"id":105,"name":"username:105"} User信息:{"age":12,"createTime":1506488953578,"id":102,"name":"username:102"} User信息:{"age":16,"createTime":1506488953597,"id":106,"name":"username:106"} User信息:{"age":10,"createTime":1506488953405,"id":100,"name":"username:100"} User信息:{"age":13,"createTime":1506488953583,"id":103,"name":"username:103"} User信息:{"age":19,"createTime":1506488953611,"id":109,"name":"username:109"} User信息:{"age":11,"createTime":1506488953573,"id":101,"name":"username:101"} User信息:{"age":17,"createTime":1506488953602,"id":107,"name":"username:107"} User信息:{"age":14,"createTime":1506488953588,"id":104,"name":"username:104"} 完成刪除用戶 開始清空表 完成清空表