|
Summary: How to continuously query the space with advanced matching, including boolean operators and inequalities.
OverviewContinuous Query allows you to write a template to the space, and continuously receive Entries that match the template. Each time an Entry appears in the space that matches the template, the Continuous Query mechanism notifies the application. Continuous Query provides advanced matching functionality, including boolean operators and inequalities. For example, you can request all Entries for which a certain field value is larger than 15 and smaller than 18.
Data Sets vs Data StreamsTraditional database management systems expect all data to be managed within some form of persistent data sets. For many recent applications, the concept of a continuous data stream is more appropriate than a data set. By nature, a stored data set is appropriate when significant portions of the data are queried again and again, and updates are small and/or relatively infrequent. In contrast, a data stream is appropriate when the data is changing constantly (often exclusively through insertions of new elements), and it is either unnecessary or impractical to operate on large portions of the data multiple times. Several applications naturally generate data streams as opposed to data sets: financial tickers, performance measurements in network monitoring and traffic management, log records or click-streams in web tracking and personalization, manufacturing processes, data feeds from sensor applications, call detail records in telecommunications, and others. Because today's database systems are ill-equipped to perform any kind of special storage management or query processing for data streams, heavily stream oriented applications tend to use a DBMS largely as an offline storage system, or not at all. Like other relatively recent new demands on data management (e.g., triggers, objects), it would be beneficial to provide stream-oriented processing as an integral part of the Space. GigaSpaces continuous Query support addressing the new demands imposed by data streams on data management and processing techniques. The continuous query is issued once and then logically run continuously over the space (in contrast to traditional one-time queries which are run once to completion over the current data sets - i.e. readMultiple using SQLQuery and regular JavaSpaces matching). In network traffic management, for example, continuous queries can be used to monitor network behavior online in order to detect anomalies (e.g., link congestion) and their causes (e.g., hardware failure, denial-of-service attack). Continuous queries can also be used to support load balancing or other network performance adjustments. In financial applications, continuous queries can be used to monitor trends and detect fleeting opportunities. Both of these applications are characterized by a need for continuous queries that go well beyond simple element-at-a-time processing, by rapid data streams, and by a need for timely online answers. To construct a continuous query, register for notifications using the SQLQuery. You can construct continuous queries using multiple Entry fields. You can construct continuous queries for any space operation (write, update, take) with any field type and values.
ExampleIn the example below we create 2 continues queries:
package com.j_spaces.examples.continuousquery; import java.io.IOException; import java.rmi.RemoteException; import net.jini.core.entry.*; import net.jini.core.event.RemoteEvent; import net.jini.core.event.RemoteEventListener; import net.jini.core.event.UnknownEventException; import net.jini.core.lease.Lease; import net.jini.core.transaction.TransactionException; import com.j_spaces.core.IJSpace; import com.j_spaces.core.client.*; public class Main implements RemoteEventListener { static IJSpace space = null; public static void main(String[] args) { try { space = (IJSpace) SpaceFinder.find("jini://localhost/./myCache"); if (space == null) { System.out.println("Space not found: " + args[0]); System.exit(-1); } System.out.println("try to Clean Space"); space.clean(); System.out.println("try to Clean Space - OK!"); Main cq = new Main(); cq.go(); System.out.println("try to Write 100 entries to space"); for (int i = 0; i < 100; i++) { MyEntry msg = new MyEntry(); space.write(msg, null, Lease.FOREVER); MyEntry ret = (MyEntry )space.read(msg, null, Lease.FOREVER); ret.m_long = new Long(i); ret.m_integer = new Integer(i); space.update(ret, null,0, 0); space.take(ret , null,0); } System.out.println("Writing 100 entries to space...Done!"); System.out.println("Click Enter to complete demo!"); System.in.read(); System.out.println("Done demo!"); } catch (Exception e) { e.printStackTrace(); } } public void go() throws RemoteException, IOException, TransactionException { space.snapshot(new MyEntry()); // lets define a continuous query that will provide notifications for entries //with fields m_long>10 and m_integer<20 SQLQuery template1 = new SQLQuery(MyEntry.class.getName() , "m_long>10 and m_integer<20"); System.out.println ("Register for Notifications for Update operations..."); NotifyDelegator notifyDelegator1 = new NotifyDelegator(space, template1, null, this, Lease.FOREVER, null, false, NotifyModifiers.NOTIFY_UPDATE); System.out.println ("Register for Notifications Update operations - Done!"); System.out.println ("Register for Notifications for Take operations..."); // lets define a continues query that will provide notifications for entries than //got fields m_long>15 and m_integer<18 SQLQuery template2 = new SQLQuery(MyEntry.class.getName() , "m_long>15 and m_integer<18"); NotifyDelegator notifyDelegator2 = new NotifyDelegator(space, template2, null, this, Lease.FOREVER, null, false, NotifyModifiers.NOTIFY_TAKE); System.out.println ("Register for Notifications Take operations - Done!"); } public void notify(RemoteEvent event) throws UnknownEventException, RemoteException { try { EntryArrivedRemoteEvent eare= (EntryArrivedRemoteEvent) event; MyEntry msg= (MyEntry) eare.getEntry(); System.out.println("Got event! Notify Type:" + getNotifyType(eare.getNotifyType())+ " object:" +msg); } catch (UnusableEntryException e) { e.printStackTrace(); } } private String getNotifyType(int notifyType) { String desc = ""; if (NotifyModifiers.isWrite(notifyType)) desc = "Write"; if (NotifyModifiers.isTake(notifyType)) desc = "Take"; if (NotifyModifiers.isLeaseExpiration(notifyType)) desc = "LeaseExpiration"; if (NotifyModifiers.isUpdate(notifyType)) desc = "Update"; if (NotifyModifiers.isALL(notifyType)) desc = "ALL"; return desc ; } } The Entry class: package com.j_spaces.examples.continuousquery; import com.j_spaces.core.client.MetaDataEntry; public class MyEntry extends MetaDataEntry{ public MyEntry(){} public Long m_long = null; public Integer m_integer = null; public String toString() { return "m_long:"+m_long + " m_integer:"+m_integer; } public static String[] __getSpaceIndexedFields() { String[] indexedFields = { "m_long" , "m_integer"}; return indexedFields; } } To start the space run the following command from the GigaSpaces Root\bin folder: gsInstance The output: CONFIG: Sets the system property ${com.gs.home} with value: E:\GigaSpacesEE5.2\
try to Clean Space
try to Clean Space - OK!
Register for Notifications for Update operations...
Register for Notifications Update operations - Done!
Register for Notifications for Take operations...
Register for Notifications Take operations - Done!
try to Write 100 entries to space
Got event! Notify Type:Update object:m_long:11 m_integer:11
Got event! Notify Type:Update object:m_long:12 m_integer:12
Got event! Notify Type:Update object:m_long:13 m_integer:13
Got event! Notify Type:Update object:m_long:14 m_integer:14
Got event! Notify Type:Update object:m_long:15 m_integer:15
Got event! Notify Type:Update object:m_long:16 m_integer:16
Got event! Notify Type:Take object:m_long:16 m_integer:16
Got event! Notify Type:Update object:m_long:17 m_integer:17
Got event! Notify Type:Take object:m_long:17 m_integer:17
Got event! Notify Type:Update object:m_long:18 m_integer:18
Got event! Notify Type:Update object:m_long:19 m_integer:19
Writing 100 entries to space...Done!
Click Enter to complete demo!
Done demo!
|
Wiki Content Tree
Your Feedback Needed!
We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.
Add Comment