1 package org.apache.helix.josql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.UUID;
27
28 import org.apache.helix.Criteria;
29 import org.apache.helix.InstanceType;
30 import org.apache.helix.ZNRecord;
31 import org.apache.helix.josql.ZNRecordJosqlFunctionHandler;
32 import org.apache.helix.josql.ZNRecordRow;
33 import org.apache.helix.model.LiveInstance.LiveInstanceProperty;
34 import org.apache.helix.tools.DefaultIdealStateCalculator;
35 import org.josql.Query;
36 import org.josql.QueryExecutionException;
37 import org.josql.QueryParseException;
38 import org.josql.QueryResults;
39 import org.testng.annotations.Test;
40
41
42 public class TestClusterJosqlQueryProcessor
43 {
44 @Test (groups = {"unitTest"})
45 public void queryClusterDataSample()
46 {
47 List<ZNRecord> liveInstances = new ArrayList<ZNRecord>();
48 Map<String, ZNRecord> liveInstanceMap = new HashMap<String, ZNRecord>();
49 List<String> instances = new ArrayList<String>();
50 for(int i = 0;i<5; i++)
51 {
52 String instance = "localhost_"+(12918+i);
53 instances.add(instance);
54 ZNRecord metaData = new ZNRecord(instance);
55 metaData.setSimpleField(LiveInstanceProperty.SESSION_ID.toString(),
56 UUID.randomUUID().toString());
57 metaData.setSimpleField("SCN", "" + (10-i));
58 liveInstances.add(metaData);
59 liveInstanceMap.put(instance, metaData);
60 }
61
62
63 ZNRecord externalView = DefaultIdealStateCalculator.calculateIdealState(
64 instances, 21, 3, "TestDB", "MASTER", "SLAVE");
65
66
67 Criteria criteria = new Criteria();
68 criteria.setInstanceName("%");
69 criteria.setResource("TestDB");
70 criteria.setRecipientInstanceType(InstanceType.PARTICIPANT);
71 criteria.setPartition("TestDB_2%");
72 criteria.setPartitionState("SLAVE");
73
74 String josql =
75 " SELECT DISTINCT mapSubKey AS 'subkey', mapValue AS 'mapValue' , getSimpleFieldValue(getZNRecordFromMap(:LIVEINSTANCESMAP, mapSubKey), 'SCN') AS 'SCN'" +
76 " FROM org.apache.helix.josql.ZNRecordRow " +
77 " WHERE mapKey LIKE 'TestDB_2%' " +
78 " AND mapSubKey LIKE '%' " +
79 " AND mapValue LIKE 'SLAVE' " +
80 " AND mapSubKey IN ((SELECT [*]id FROM :LIVEINSTANCES)) " +
81 " ORDER BY parseInt(getSimpleFieldValue(getZNRecordFromMap(:LIVEINSTANCESMAP, mapSubKey), 'SCN'))";
82
83 Query josqlQuery = new Query();
84 josqlQuery.setVariable("LIVEINSTANCES", liveInstances);
85 josqlQuery.setVariable("LIVEINSTANCESMAP", liveInstanceMap);
86 josqlQuery.addFunctionHandler(new ZNRecordRow());
87 josqlQuery.addFunctionHandler(new ZNRecordJosqlFunctionHandler());
88 josqlQuery.addFunctionHandler(new Integer(0));
89 try
90 {
91 josqlQuery.parse(josql);
92 QueryResults qr = josqlQuery.execute(ZNRecordRow.convertMapFields(externalView));
93 @SuppressWarnings({ "unchecked", "unused" })
94 List<Object> result = qr.getResults();
95
96 }
97 catch (QueryParseException e)
98 {
99 e.printStackTrace();
100 } catch (QueryExecutionException e)
101 {
102 e.printStackTrace();
103 }
104
105 }
106 }