Monday, 14 November 2011

Accessing GemFire Regions from Jython

My previous blog entry showed how to access GemFire Regions from JRuby. This demo below accesses the same regions , this time using jython. The code has identical java calls with the obvious jython syntax rather then JRuby. Here I just show the jython script and output.

Before we run the script we need to set the classpath correctly to pick up the GemFire JAR's as well as the JAR file required to access the Region.

export CUR_DIR=`pwd`
export CLASSPATH=$CUR_DIR/lib/antlr.jar:$CUR_DIR/lib/gemfire.jar:$CUR_DIR/lib/gemfire-quickstart.jar

jython script - gemfire-caching-proxy-client.jy

from java.util import Date
from java.util import Collection
from java.util import Iterator

from vmware.au.se.demo.domain import AllDBObject 
  
from com.gemstone.gemfire.cache import Region 
from com.gemstone.gemfire.cache.client import ClientCache
from com.gemstone.gemfire.cache.client import ClientCacheFactory

from com.gemstone.gemfire.cache.query import Query  
from com.gemstone.gemfire.cache.query import QueryService
from com.gemstone.gemfire.cache.query import SelectResults

print "*********************************"  
print "GemFire 6.6 Example from JYTHON"  
print "*********************************"  
  
print "Started at " + Date().toString()  
  
try:  
  
 ccf = ClientCacheFactory()
 ccf.set("cache-xml-file", "client.xml")  
 cache = ccf.create() 

 allObjectRegion = cache.getRegion("AllObjectRegion")  
 queryService = cache.getQueryService()  
 
 query = queryService.newQuery("SELECT * FROM /AllObjectRegion where owner = 'SCOTT'")  
 print "\n** All OBJECTS with owner = 'SCOTT'\n"  

 result = query.execute() 
 collection = result.asList()
 iter = collection.iterator()
 i = 0  
 
 while iter.hasNext():
  i = i + 1  
  entry = iter.next()
  print "Entry " , i , " : " , entry
  
except:  
    print "Unexpected error:", sys.exc_info()[0]  
    raise  
      
finally:  
    cache.close()
     
print "Ended at " + Date().toString()   

Output as follows
Pas-Apicellas-MacBook-Pro:quickstart-client papicella$ jython gemfire-caching-proxy-client.jy 
*********************************
GemFire 6.6 Example from JYTHON
*********************************
Started at Mon Nov 14 08:02:06 EST 2011

[info 2011/11/14 08:02:07.033 EST <main> tid=0x1] 

...

  udp-recv-buffer-size="1048576"
  udp-send-buffer-size="65535"
  writable-working-dir=""
  

[info 2011/11/14 08:02:07.045 EST <main> tid=0x1] Running in local mode since mcast-port was 0 and locators was empty.

[info 2011/11/14 08:02:07.113 EST <Thread-3 StatSampler> tid=0x15] Disabling statistic archival.

[info 2011/11/14 08:02:07.380 EST <poolTimer-client-2> tid=0x1a] AutoConnectionSource discovered new locators [/10.117.85.62:41111]

[config 2011/11/14 08:02:07.381 EST <main> tid=0x1] Updating membership port.  Port changed from 0 to 49,335.

[config 2011/11/14 08:02:07.443 EST <main> tid=0x1] Pool client started with multiuser-authentication=false

[info 2011/11/14 08:02:07.444 EST <main> tid=0x1] Overridding MemoryPoolMXBean heap threshold bytes 0 on pool CMS Old Gen with 352,321,536

[info 2011/11/14 08:02:07.447 EST <main> tid=0x1] Overridding MemoryPoolMXBean heap threshold bytes 352,321,536 on pool CMS Old Gen with 352,321,536

[info 2011/11/14 08:02:07.447 EST <Cache Client Updater Thread  on Pas-Apicellas-MacBook-Pro(434)<v2>:38304/49301> tid=0x1c] Cache Client Updater Thread  on Pas-Apicellas-MacBook-Pro(434)<v2>:38304/49301 (10.117.85.62:49311) : ready to process messages.

[config 2011/11/14 08:02:07.527 EST <main> tid=0x1] Cache initialized using "jar:file:/Users/papicella/vmware/scripting/demos/jython/gemfire/quickstart-client/lib/gemfire-quickstart.jar!/client.xml".

** All OBJECTS with owner = 'SCOTT'

Entry  1  :  AllDBObject [owner=SCOTT, objectName=BONUS, objectId=74213, objectType=TABLE, status=VALID]
Entry  2  :  AllDBObject [owner=SCOTT, objectName=SALGRADE, objectId=74214, objectType=TABLE, status=VALID]
Entry  3  :  AllDBObject [owner=SCOTT, objectName=PK_EMP, objectId=74212, objectType=INDEX, status=VALID]
Entry  4  :  AllDBObject [owner=SCOTT, objectName=PK_DEPT, objectId=74210, objectType=INDEX, status=VALID]
Entry  5  :  AllDBObject [owner=SCOTT, objectName=DEPT, objectId=74209, objectType=TABLE, status=VALID]
Entry  6  :  AllDBObject [owner=SCOTT, objectName=EMP, objectId=74211, objectType=TABLE, status=VALID]
Entry  7  :  AllDBObject [owner=SCOTT, objectName=JDBC_BATCH_TABLE, objectId=75753, objectType=TABLE, status=VALID]

[info 2011/11/14 08:02:07.742 EST <main> tid=0x1] GemFireCache[id = 1069736291; isClosing = true; created = Mon Nov 14 08:02:07 EST 2011; server = false; copyOnRead = false; lockLease = 120; lockTimeout = 60]: Now closing.

[info 2011/11/14 08:02:07.771 EST <main> tid=0x1] Resetting original MemoryPoolMXBean heap threshold bytes 0 on pool CMS Old Gen

[config 2011/11/14 08:02:07.803 EST <main> tid=0x1] Destroying connection pool client
Ended at Mon Nov 14 08:02:07 EST 2011  

No comments: