Connect apache geode with java
I studied apache geode for a long time yesterday. It was relatively simple to query and operate through the gfsh command, but how to query through the program because I had too little seniority to understand it. It took me a long time to understand.
1. Create a test region
Create region-name=user-type=REPLICATE_PERSISTENT
two。 Using eclipse to create maven Project
To modify pom.xml, you need to introduce gemfire-8.2.0.jar, which is found in the
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 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
Create a new 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 {/ * must have this method, otherwise the query will report no init () method * * / 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; / * Age * * / private int age; / * * name * * / private String name / * creation time * * / 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);}}
Modify 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); / / Open user table region = rf.create (regionName); System.out.println ("start creating user"); create (region); select (region); System.out.println ("finish creating user") System.out.println ("start modifying user"); update (region); select (region); System.out.println ("finished modifying user"); System.out.println ("start deleting user"); delete (region,108); select (region) System.out.println ("finish deleting user"); System.out.println ("start emptying table"); truncate (region); select (region); System.out.println ("finish emptying table"); / / close table region.close () } / * query user table * * / 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 Information:" + JSON.toJSONString (userBean));} Object obj = region.get If (obj! = null & & obj instanceof UserBean) {System.out.println ("User108 Information" + JSON.toJSONString (obj));}} catch (Exception e) {e.printStackTrace (); / / logger.error ("error occured.", e) }} / * * add 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目录下有对应的jar包 然后把hellogeode-0.0.1-SNAPSHOT.jar拷贝到geode服务器上某个目录例如/tmp/t/下 4.发布Jar到geode里 为啥要发布到geode里呢?因为我们用UserBean的序列化和反序列化作为表结构,因此deode查询时需要UserBean.class,因此我们发布hellogeode-0.0.1-SNAPSHOT.jar到geode,主要是为了把UserBean.class导入到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
If we have any changes to UserBean, we need to uninstall jar before republishing it.
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.jargfsh > 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. test
The test program can be run in eclipse, without the need to run on the server
Run app.java on eclipse
Test result
Start creating the user [info 2017-09-27 13 AutoConnectionSource discovered new locators 09 AutoConnectionSource discovered new locators 13 422 CST tid=0x17] [slave1/211.98.71.195:10334] [info 2017-09-27 13 9 AutoConnectionSource discovered new locators 13 511 CST tid=0x1] Updating membership port. Port changed from 0 to 51084. ID is now bogon (92040:loner): 0:0c29bdc1User Information: {"age": 12, "createTime": 1506488953578, "id": 102," name ":" username:102 "} User Information: {" age ": 15," createTime ": 1506488953592," id ": 105," name": "username:105"} User Information: {"age": 14, "createTime": 1506488953588, "id": 104," name ":" username:104 "} User Information: {" age ": 10," createTime ": 15064889405 "id": 100,100, "name": "username:100"} User Information: {"age": 19, "createTime": 1506488953611, "id": 109,109, "name": "username:109"} User Information: {"age": 13, "createTime": 1506488953583, "id": 103,103, "name": "username:103"} User Information: {"age": 18, "createTime": 1506488953606, "id": 1088," name ":" username:108 "} User Information: {" age ": 11," createTime ": 1506488953573 User information: {"age": 16, "createTime": 1506488953597, "id": 106, "name": "username:106"} User information: {"age": 17, "createTime": 1506488953602, "id": 107, "name": "username:107"} User108 information {"age": 18, "createTime": 1506488953606, "id": 108 "name": "username:108"} complete the creation of user User108 information {"age": 18, "createTime": 1506488953606, "id": 108," name ":" username:108 "} User information: {" age ": 17," createTime ": 1506488953602," id ": 107," name ":" username:107 "} User information: {" age ": 12," createTime ": 1506488953578," id ": 102,102," name ":" username:102 "} User information: {" age ": 15 "createTime": 1506488953592, "name": "username:105"} User Information: {"age": 12, "createTime": 1506488953606, "id": 108," name ":" username:108 "} User Information: {" age ": 13," createTime ": 1506488953583," id ": 103," name ":" username:103 "} User Information: {" age ": 19," createTime ": 1506488953611," id ": 109,109," name ":" username:109 "} User Information: {" age ": 16 "createTime": 1506488953597, "name": "username:106"} User Information: {"age": 11, "createTime": 1506488953573, "id": 101," name ":" username:101 "} User Information: {" age ": 14," createTime ": 1506488953588," id ": 104,104," name ":" username:104 "} User Information: {" age ": 10," createTime ": 1506488953405," id ": 100," name": "username:100"} User108 Information {"age": 12 "createTime": 1506488953606, "id": 108,108, "name": "username:108"} complete modification. Users begin to delete user User information: {"age": 15, "createTime": 1506488953592, "id": 105,105, "name": "username:105"} User information: {"age": 12, "createTime": 1506488953578, "id": 102,102, "name": "username:102"} User information: {"age": 16, "createTime": 1506488953597, "id": 106 "name": "username:106"} User Information: {"age": 10, "createTime": 1506488953405, "id": 100," name ":" username:100 "} User Information: {" age ": 13," createTime ": 1506488953583," id ": 103," name": "username:103"} User Information: {"age": 19, "createTime": 1506488953611, "id": 109,109, "name": "username:109"} User Information: {"age": 11, "createTime": 1506488953573, "id": 101 "name": "username:101"} User Information: {"age": 17, "createTime": 1506488953602, "id": 107," name ":" username:107 "} User Information: {" age ": 14," createTime ": 1506488953588," id ": 104," name ":" username:104 "} finish deleting the user start emptying table