org.apache.geode.cache.query.SelectResults Java Examples

The following examples show how to use org.apache.geode.cache.query.SelectResults. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Example.java    From geode-examples with Apache License 2.0 6 votes vote down vote up
public static void doQueries(ClientCache cache) throws NameResolutionException,
    TypeMismatchException, QueryInvocationTargetException, FunctionDomainException {
  QueryService queryService = cache.getQueryService();

  // Query for every entry in the region, and print query results.
  System.out.println("\nExecuting query: " + QUERY1);
  SelectResults<EmployeeData> results =
      (SelectResults<EmployeeData>) queryService.newQuery(QUERY1).execute();
  printSetOfEmployees(results);

  // Query for all part time employees, and print query results.
  System.out.println("\nExecuting query: " + QUERY2);
  results = (SelectResults<EmployeeData>) queryService.newQuery(QUERY2).execute();
  printSetOfEmployees(results);

  // Query for last name of Jive, and print the full name and employee number.
  System.out.println("\nExecuting query: " + QUERY3);
  results =
      (SelectResults<EmployeeData>) queryService.newQuery(QUERY3).execute(new String[] {"Jive"});
  for (EmployeeData eachEmployee : results) {
    System.out.println(String.format("Employee %s %s has employee number %d",
        eachEmployee.getFirstName(), eachEmployee.getLastName(), eachEmployee.getEmplNumber()));
  }
}
 
Example #2
Source File: Example.java    From geode-examples with Apache License 2.0 6 votes vote down vote up
public static void doQueries(ClientCache cache) throws NameResolutionException,
    TypeMismatchException, QueryInvocationTargetException, FunctionDomainException {
  QueryService queryService = cache.getQueryService();

  // Query for every entry in the region, and print query results.
  System.out.println("\nExecuting query: " + QUERY1);
  SelectResults<EmployeeData> results =
      (SelectResults<EmployeeData>) queryService.newQuery(QUERY1).execute();
  printSetOfEmployees(results);

  // Query for all part time employees, and print query results.
  System.out.println("\nExecuting query: " + QUERY2);
  results = (SelectResults<EmployeeData>) queryService.newQuery(QUERY2).execute();
  printSetOfEmployees(results);

  // Query for last name of Jive, and print the full name and employee number.
  System.out.println("\nExecuting query: " + QUERY3);
  results =
      (SelectResults<EmployeeData>) queryService.newQuery(QUERY3).execute(new String[] {"Jive"});
  for (EmployeeData eachEmployee : results) {
    System.out.println(String.format("Employee %s %s has employee number %d",
        eachEmployee.getFirstName(), eachEmployee.getLastName(), eachEmployee.getEmplNumber()));
  }
}
 
Example #3
Source File: BookMasterRegionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

    ClientCache clientCache = new ClientCacheFactory()
        .addPoolLocator("localhost", 10334)
        .setPdxSerializer(new ReflectionBasedAutoSerializer("org.apache.calcite.adapter.geode.*"))
        .create();

    // Using Key/Value
    Region bookMaster = clientCache
        .createClientRegionFactory(ClientRegionShortcut.PROXY)
        .create("BookMaster");

    System.out.println("BookMaster = " + bookMaster.get(789));

    // Using OQL
    QueryService queryService = clientCache.getQueryService();
    String oql = "select itemNumber, description, retailCost from /BookMaster";
    SelectResults result = (SelectResults) queryService.newQuery(oql).execute();
    System.out.println(result.asList());
  }
 
Example #4
Source File: Example.java    From geode-examples with Apache License 2.0 5 votes vote down vote up
int countResults(QueryService queryService, String queryString, Object[] params) {
  try {
    int count = 0;
    SelectResults<Passenger> results =
        (SelectResults<Passenger>) queryService.newQuery(queryString).execute(params);
    for (Passenger passenger : results) {
      ++count;
    }
    return count;
  } catch (FunctionDomainException | TypeMismatchException | NameResolutionException
      | QueryInvocationTargetException e) {
    e.printStackTrace();
    return -1;
  }
}
 
Example #5
Source File: Example.java    From geode-examples with Apache License 2.0 5 votes vote down vote up
int countResults(QueryService queryService, String queryString, Object[] params) {
  try {
    int count = 0;
    SelectResults<Passenger> results =
        (SelectResults<Passenger>) queryService.newQuery(queryString).execute(params);
    for (Passenger passenger : results) {
      ++count;
    }
    return count;
  } catch (FunctionDomainException | TypeMismatchException | NameResolutionException
      | QueryInvocationTargetException e) {
    e.printStackTrace();
    return -1;
  }
}
 
Example #6
Source File: GeodeEnumerator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a GeodeEnumerator.
 *
 * @param results      Geode result set ({@link SelectResults})
 * @param protoRowType The type of resulting rows
 */
GeodeEnumerator(SelectResults results, RelProtoDataType protoRowType) {
  if (results == null) {
    LOGGER.warn("Null OQL results!");
  }
  this.iterator = (results == null) ? Collections.emptyIterator() : results.iterator();
  this.current = null;

  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  this.fieldTypes = protoRowType.apply(typeFactory).getFieldList();
}
 
Example #7
Source File: GeodeSimpleEnumerator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public GeodeSimpleEnumerator(ClientCache clientCache, String regionName) {
  this.clientCache = clientCache;
  QueryService queryService = clientCache.getQueryService();
  String oql = "select * from /" + regionName.trim();
  try {
    results = ((SelectResults) queryService.newQuery(oql).execute()).iterator();
  } catch (Exception e) {
    e.printStackTrace();
    results = null;
  }
}
 
Example #8
Source File: GeodeZipsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testWhereWithOrForLargeValueList() throws Exception {
  Cache cache = POLICY.cache();
  QueryService queryService = cache.getQueryService();
  Query query = queryService.newQuery("select state as state from /zips");
  SelectResults results = (SelectResults) query.execute();

  Set<String> stateList = (Set<String>) results.stream().map(s -> {
    StructImpl struct = (StructImpl) s;
    return struct.get("state");
  })
      .collect(Collectors.toCollection(LinkedHashSet::new));

  String stateListPredicate = stateList.stream()
      .map(s -> String.format(Locale.ROOT, "state = '%s'", s))
      .collect(Collectors.joining(" OR "));

  String stateListStr = "'" + String.join("', '", stateList) + "'";

  String queryToBeExecuted = "SELECT state as state FROM view WHERE " + stateListPredicate;

  String expectedQuery = "SELECT state AS state FROM /zips WHERE state "
      + "IN SET(" + stateListStr + ")";

  calciteAssert()
      .query(queryToBeExecuted)
      .returnsCount(149)
      .queryContains(
          GeodeAssertions.query(expectedQuery));
}
 
Example #9
Source File: Example.java    From geode-examples with Apache License 2.0 4 votes vote down vote up
private static void printSetOfEmployees(SelectResults<EmployeeData> results) {
  System.out.println("Query returned " + results.size() + " results.");
  for (EmployeeData eachEmployee : results) {
    System.out.println(String.format("Employee: %s", eachEmployee.toString()));
  }
}
 
Example #10
Source File: Example.java    From geode-examples with Apache License 2.0 4 votes vote down vote up
private static void printSetOfEmployees(SelectResults<EmployeeData> results) {
  System.out.println("Query returned " + results.size() + " results.");
  for (EmployeeData eachEmployee : results) {
    System.out.println(String.format("Employee: %s", eachEmployee.toString()));
  }
}
 
Example #11
Source File: GeodeOqlInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
private InterpreterResult executeOql(String oql) {
  try {

    if (getExceptionOnConnect() != null) {
      return new InterpreterResult(Code.ERROR, getExceptionOnConnect().getMessage());
    }

    @SuppressWarnings("unchecked")
    SelectResults<Object> results =
        (SelectResults<Object>) getQueryService().newQuery(oql).execute();

    StringBuilder msg = new StringBuilder(TABLE_MAGIC_TAG);
    boolean isTableHeaderSet = false;

    Iterator<Object> iterator = results.iterator();
    int rowDisplayCount = 0;

    while (iterator.hasNext() && (rowDisplayCount < getMaxResult())) {

      Object entry = iterator.next();
      rowDisplayCount++;

      if (entry instanceof Number) {
        handleNumberEntry(isTableHeaderSet, entry, msg);
      } else if (entry instanceof Struct) {
        handleStructEntry(isTableHeaderSet, entry, msg);
      } else if (entry instanceof PdxInstance) {
        handlePdxInstanceEntry(isTableHeaderSet, entry, msg);
      } else {
        handleUnsupportedTypeEntry(isTableHeaderSet, entry, msg);
      }

      isTableHeaderSet = true;
      msg.append(NEWLINE);
    }

    return new InterpreterResult(Code.SUCCESS, msg.toString());

  } catch (Exception ex) {
    logger.error("Cannot run " + oql, ex);
    return new InterpreterResult(Code.ERROR, ex.getMessage());
  }
}
 
Example #12
Source File: GeodeOqlInterpreterTest.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
private void testOql(Iterator<Object> queryResponseIterator, String expectedOutput, int maxResult)
    throws Exception {

  GeodeOqlInterpreter spyGeodeOqlInterpreter = spy(new GeodeOqlInterpreter(new Properties()));

  QueryService mockQueryService = mock(QueryService.class, RETURNS_DEEP_STUBS);

  when(spyGeodeOqlInterpreter.getQueryService()).thenReturn(mockQueryService);
  when(spyGeodeOqlInterpreter.getMaxResult()).thenReturn(maxResult);

  @SuppressWarnings("unchecked")
  SelectResults<Object> mockResults = mock(SelectResults.class);

  when(mockQueryService.newQuery(eq(OQL_QUERY)).execute()).thenReturn(mockResults);

  when(mockResults.iterator()).thenReturn(queryResponseIterator);

  InterpreterResult interpreterResult = spyGeodeOqlInterpreter.interpret(OQL_QUERY, null);

  assertEquals(Code.SUCCESS, interpreterResult.code());
  assertEquals(expectedOutput, interpreterResult.message().get(0).getData());
}
 
Example #13
Source File: GeodePojoTest.java    From immutables with Apache License 2.0 4 votes vote down vote up
private int executeCountQuery(String query, Object... bindVariables) throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException {
  return ((SelectResults<Integer>) execute(query, bindVariables)).stream()
          .findAny().orElseThrow(IllegalArgumentException::new);
}