Java Code Examples for com.datastax.driver.core.querybuilder.Select.Where#and()

The following examples show how to use com.datastax.driver.core.querybuilder.Select.Where#and() . 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: CassandraOperationImpl.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
private Response executeSelectQuery(
    String keyspaceName,
    String tableName,
    List<String> ids,
    Builder selectBuilder,
    String primaryKeyColumnName) {
  Response response;
  Select selectQuery = selectBuilder.from(keyspaceName, tableName);
  Where selectWhere = selectQuery.where();
  Clause clause = null;
  if (StringUtils.isBlank(primaryKeyColumnName)) {
    clause = QueryBuilder.in(JsonKey.ID, ids.toArray(new Object[ids.size()]));
  } else {
    clause = QueryBuilder.in(primaryKeyColumnName, ids.toArray(new Object[ids.size()]));
  }

  selectWhere.and(clause);
  ResultSet results = connectionManager.getSession(keyspaceName).execute(selectQuery);
  response = CassandraUtil.createResponse(results);
  return response;
}
 
Example 2
Source File: CassandraUtil.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * Method to create the where clause.
 *
 * @param key represents the column name.
 * @param value represents the column value.
 * @param where where clause.
 */
public static void createWhereQuery(String key, Object value, Where where) {
  if (value instanceof Map) {
    Map<String, Object> map = (Map<String, Object>) value;
    map.entrySet()
        .stream()
        .forEach(
            x -> {
              if (Constants.LTE.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.lte(key, x.getValue()));
              } else if (Constants.LT.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.lt(key, x.getValue()));
              } else if (Constants.GTE.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.gte(key, x.getValue()));
              } else if (Constants.GT.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.gt(key, x.getValue()));
              }
            });
  } else if (value instanceof List) {
    where.and(QueryBuilder.in(key, (List) value));
  } else {
    where.and(QueryBuilder.eq(key, value));
  }
}
 
Example 3
Source File: CassandraUtil.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
public static void createQuery(String key, Object value, Where where) {
  if (value instanceof Map) {
    Map<String, Object> map = (Map<String, Object>) value;
    map.entrySet()
        .stream()
        .forEach(
            x -> {
              if (Constants.LTE.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.lte(key, x.getValue()));
              } else if (Constants.LT.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.lt(key, x.getValue()));
              } else if (Constants.GTE.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.gte(key, x.getValue()));
              } else if (Constants.GT.equalsIgnoreCase(x.getKey())) {
                where.and(QueryBuilder.gt(key, x.getValue()));
              }
            });
  } else if (value instanceof List) {
    where.and(QueryBuilder.in(key, (List) value));
  } else {
    where.and(QueryBuilder.eq(key, value));
  }
}
 
Example 4
Source File: CassandraOperationImpl.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
private Response getRecordByIdentifier(
    String keyspaceName, String tableName, Object key, List<String> fields) {
  long startTime = System.currentTimeMillis();
  ProjectLogger.log(
      "Cassandra Service getRecordBy key method started at ==" + startTime, LoggerEnum.INFO);
  Response response = new Response();
  try {
    Session session = connectionManager.getSession(keyspaceName);
    Builder selectBuilder;
    if (CollectionUtils.isNotEmpty(fields)) {
      selectBuilder = QueryBuilder.select(fields.toArray(new String[fields.size()]));
    } else {
      selectBuilder = QueryBuilder.select().all();
    }
    Select selectQuery = selectBuilder.from(keyspaceName, tableName);
    Where selectWhere = selectQuery.where();
    if (key instanceof String) {
      selectWhere.and(eq(Constants.IDENTIFIER, key));
    } else if (key instanceof Map) {
      Map<String, Object> compositeKey = (Map<String, Object>) key;
      compositeKey
          .entrySet()
          .stream()
          .forEach(
              x -> {
                CassandraUtil.createQuery(x.getKey(), x.getValue(), selectWhere);
              });
    }
    ResultSet results = session.execute(selectWhere);
    response = CassandraUtil.createResponse(results);
  } catch (Exception e) {
    ProjectLogger.log(Constants.EXCEPTION_MSG_FETCH + tableName + " : " + e.getMessage(), e);
    throw new ProjectCommonException(
        ResponseCode.SERVER_ERROR.getErrorCode(),
        ResponseCode.SERVER_ERROR.getErrorMessage(),
        ResponseCode.SERVER_ERROR.getResponseCode());
  }
  logQueryElapseTime("getRecordByIdentifier", startTime);
  return response;
}
 
Example 5
Source File: CassandraOperationImpl.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
@Override
public Response getRecordsByCompositeKey(
    String keyspaceName, String tableName, Map<String, Object> compositeKeyMap) {
  long startTime = System.currentTimeMillis();
  ProjectLogger.log(
      "CassandraOperationImpl: getRecordsByCompositeKey called at " + startTime, LoggerEnum.INFO);
  Response response = new Response();
  try {
    Builder selectBuilder = QueryBuilder.select().all();
    Select selectQuery = selectBuilder.from(keyspaceName, tableName);
    Where selectWhere = selectQuery.where();
    for (Entry<String, Object> entry : compositeKeyMap.entrySet()) {
      Clause clause = eq(entry.getKey(), entry.getValue());
      selectWhere.and(clause);
    }
    ResultSet results = connectionManager.getSession(keyspaceName).execute(selectQuery);
    response = CassandraUtil.createResponse(results);
  } catch (Exception e) {
    ProjectLogger.log(
        "CassandraOperationImpl:getRecordsByCompositeKey: "
            + Constants.EXCEPTION_MSG_FETCH
            + tableName
            + " : "
            + e.getMessage());
    throw new ProjectCommonException(
        ResponseCode.SERVER_ERROR.getErrorCode(),
        ResponseCode.SERVER_ERROR.getErrorMessage(),
        ResponseCode.SERVER_ERROR.getResponseCode());
  }
  logQueryElapseTime("getRecordsByCompositeKey", startTime);
  return response;
}