Java调用Geode

Goede序列化

Geode提供了Java序列化之外的序列化选项,为数据存储,传输和语言类型提供更高的性能和更大的灵活性。 
Geode DataSerialization 比PDXSerialization快 25%,但使用PDX序列化可以减少反序列化的成本。 
Geode 提供的序列化方式(DataSerialization和PDXSerialization),不支持对象的循环引用,如果多次引用相同的对象,则会将每个引用进行序列化,反序列化会生成对象的多个副本。在这种情况下,Java序列化对象一次,当对象进行反序列化时,它将生成具有多个引用的对象的实例。 

Java连接Geode示例

依赖JAR:

    
      org.apache.geode
      geode-core
      1.1.0
    

    
      com.gemstone.gemfire
      gemfire
      8.2.2
    

    
      org.apache.geode
      geode-json
      1.1.0
    
    
      org.apache.geode
      geode-common
      1.1.0
    


代码示例:

public class UserEntity implements Serializable{

    private int id;
    private int age;
    private String name;
    private Date registerdDate;
    
    //getter and setter...
}


public class DataClient {
    private final static Logger logger  = Logger.getLogger(DataClient.class);

    Region region = null;
    @Before
    public void init(){
        ClientCache cache = new ClientCacheFactory().addPoolLocator("db", 10334).create();
        ClientRegionFactory rf = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY);

        region = rf.create( "user");
    }

    @Test
    public void saveUserEntity(){
        region.putAll(newUserEntity(10));
    }

    @Test
    public void clear(){
        region.clear();
    }

    @Test
    public void delele(){
        region.remove("");
    }

    @Test
    public void update(){
        Object obj = region.get("108");
        Map map = new HashMap();
        UserEntity data= ((UserEntity)obj);
        data.setAge(12);
        map.put("108",data);
        region.putAll(map);
    }

    @Test
    public void getUserEntity(){
        try {
            Object objList = region.query("select * from /user u where u.age > 10");
            Object obj = region.get("108");
            if(objList != null && objList instanceof ResultsBag){
                Iterator iter = ((ResultsBag) objList).iterator();
                while(iter.hasNext()){
                    UserEntity UserEntity = (UserEntity) iter.next();
                    System.out.println("UserEntity"+UserEntity);
                }
            }
            if(obj != null && obj instanceof UserEntity){
                System.out.println("UserEntity"+(UserEntity)obj);
            }
        }catch (Exception e){
            logger.error("error occured.", e);
        }
    }

    @After
    public void done(){
        region.close();
    }

    private Map newUserEntity(int size){
        Map map = new HashMap();
        for (int i = 0; i < size; i++) {
            int id = i+100;
            map.put(id,new UserEntity(id,10+i,"username:"+id,new Date()));
        }
        return map;
    }
}

Geode Query

查询示例

 // Identify your query string.
 String queryString = "SELECT * FROM /exampleRegion";

 // Get QueryService from Cache.
 QueryService queryService = cache.getQueryService();

 // Create the Query Object.
 Query query = queryService.newQuery(queryString);

 // Execute Query locally. Returns results set.
 SelectResults results = (SelectResults)query.execute();

 // Find the Size of the ResultSet.
 int size = results.size();

 // Iterate through your ResultSet.
 Portfolio p = (Portfolio)results.iterator().next(); /* Region containing Portfolio object. */

查询语法

WHERE 语法 
SELECT * FROM /user WHERE name=‘username’; 

LIKE 语法 
SELECT * FROM /user WHERE name like ‘use%’; 
SELECT * FROM /user WHERE name like ‘_use_’; 

添加表别名 
SELECT u.id,u.name FROM /user u WHERE u.name=‘username’; 

DISTINCT 
SELECT DISTINCT u.name FROM /user u 

逻辑操作符 AND,OR,NOT 
SELECT * FROM /user WHERE id>4 and id<9 and not id = 6 

其它比较操作 
=,<>,!=,<,⇐,>,>= 
IN,NOT IN 
SELECT * FROM /user WHERE NOT (ID IN SET(1,2)) 

LIMIT 
SELECT * FROM /user WHERE id>0 LIMIT 10 

COUNT 
SELECT COUNT(*) FROM /user WHERE ID > 0 LIMIT 50 

你可能感兴趣的:(Geode)