com.datastax.driver.core.exceptions.QueryValidationException Java Examples

The following examples show how to use com.datastax.driver.core.exceptions.QueryValidationException. 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: CQLExecutionCommand.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private void executeCommand(Session session, Operation operation) throws CommandExecutionException {
   try {
      if(operation == Operation.UPGRADE) {
         session.execute(command.getUpdateCql());
      } else {
         session.execute(command.getRollbackCql());
      }
   } catch(QueryExecutionException | QueryValidationException e) {
      throw new CommandExecutionException(e);
   }
}
 
Example #2
Source File: QueryCommand.java    From FlareBot with MIT License 5 votes vote down vote up
@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    try {
        CassandraController.runUnsafeTask(conn -> {
            ResultSet set = conn.execute(MessageUtils.getMessage(args, 0));
            List<String> header = new ArrayList<>();
            List<List<String>> table = new ArrayList<>();
            int columnsCount = set.getColumnDefinitions().size();
            for (int i = 0; i < columnsCount; i++) {
                header.add(set.getColumnDefinitions().getName(i));
            }
            for (Row setRow : set) {
                List<String> row = new ArrayList<>();
                for (int i = 0; i < columnsCount; i++) {
                    String value = setRow.getObject(i).toString();
                    row.add(value.substring(0, Math.min(30, value.length())));
                }
                table.add(row);
            }
            String output = MessageUtils.makeAsciiTable(header, table, null);
            if (output.length() < 2000) {
                channel.sendMessage(output).queue();
            } else {
                MessageUtils.sendErrorMessage("The query result set was very large, it has been posted to paste [here](" + MessageUtils
                        .paste(output) + ")", channel, sender);
            }
        });
    } catch (QueryExecutionException | QueryValidationException e) {
        EmbedBuilder eb = new EmbedBuilder();
        eb.setTitle("Failed to execute query");
        eb.addField("Error", "```\n" + e.getMessage() + "\n```", false);
        channel.sendMessage(eb.build()).queue();
    }
}
 
Example #3
Source File: CassandraController.java    From FlareBot with MIT License 5 votes vote down vote up
public static void runTask(CassandraTask task) {
    try {
        task.execute(session);
    } catch (QueryExecutionException | QueryValidationException e) {
        FlareBot.LOGGER.error("Failed to execute Cassandra query", e);
    }
}
 
Example #4
Source File: CassandraController.java    From FlareBot with MIT License 5 votes vote down vote up
public static ResultSet execute(String query) {
    try {
        return session.execute(query);
    } catch (QueryExecutionException | QueryValidationException e) {
        FlareBot.LOGGER.error("Failed to execute Cassandra query", e);
        return null;
    }
}
 
Example #5
Source File: CassandraOperationImpl.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
@Override
public Response batchInsert(
    String keyspaceName, String tableName, List<Map<String, Object>> records) {

  long startTime = System.currentTimeMillis();
  ProjectLogger.log(
      "Cassandra Service batchInsert method started at ==" + startTime, LoggerEnum.INFO);

  Session session = connectionManager.getSession(keyspaceName);
  Response response = new Response();
  BatchStatement batchStatement = new BatchStatement();
  ResultSet resultSet = null;

  try {
    for (Map<String, Object> map : records) {
      Insert insert = QueryBuilder.insertInto(keyspaceName, tableName);
      map.entrySet()
          .stream()
          .forEach(
              x -> {
                insert.value(x.getKey(), x.getValue());
              });
      batchStatement.add(insert);
    }
    resultSet = session.execute(batchStatement);
    response.put(Constants.RESPONSE, Constants.SUCCESS);
  } catch (QueryExecutionException
      | QueryValidationException
      | NoHostAvailableException
      | IllegalStateException e) {
    ProjectLogger.log("Cassandra Batch Insert Failed." + e.getMessage(), e);
    throw new ProjectCommonException(
        ResponseCode.SERVER_ERROR.getErrorCode(),
        ResponseCode.SERVER_ERROR.getErrorMessage(),
        ResponseCode.SERVER_ERROR.getResponseCode());
  }
  logQueryElapseTime("batchInsert", startTime);
  return response;
}
 
Example #6
Source File: CassandraOperationImpl.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
/**
 * This method updates all the records in a batch
 *
 * @param keyspaceName
 * @param tableName
 * @param records
 * @return
 */
// @Override
public Response batchUpdateById(
    String keyspaceName, String tableName, List<Map<String, Object>> records) {

  long startTime = System.currentTimeMillis();
  ProjectLogger.log(
      "Cassandra Service batchUpdateById method started at ==" + startTime, LoggerEnum.INFO);
  Session session = connectionManager.getSession(keyspaceName);
  Response response = new Response();
  BatchStatement batchStatement = new BatchStatement();
  ResultSet resultSet = null;

  try {
    for (Map<String, Object> map : records) {
      Update update = createUpdateStatement(keyspaceName, tableName, map);
      batchStatement.add(update);
    }
    resultSet = session.execute(batchStatement);
    response.put(Constants.RESPONSE, Constants.SUCCESS);
  } catch (QueryExecutionException
      | QueryValidationException
      | NoHostAvailableException
      | IllegalStateException e) {
    ProjectLogger.log("Cassandra Batch Update Failed." + e.getMessage(), e);
    throw new ProjectCommonException(
        ResponseCode.SERVER_ERROR.getErrorCode(),
        ResponseCode.SERVER_ERROR.getErrorMessage(),
        ResponseCode.SERVER_ERROR.getResponseCode());
  }
  logQueryElapseTime("batchUpdateById", startTime);
  return response;
}
 
Example #7
Source File: CassandraCqlMapState.java    From storm-cassandra-cql with Apache License 2.0 5 votes vote down vote up
protected void checkCassandraException(Exception e) {
    _mexceptions.incr();
    if (e instanceof AlreadyExistsException ||
            e instanceof AuthenticationException ||
            e instanceof DriverException ||
            e instanceof DriverInternalError ||
            e instanceof InvalidConfigurationInQueryException ||
            e instanceof InvalidQueryException ||
            e instanceof InvalidTypeException ||
            e instanceof QueryExecutionException ||
            e instanceof QueryValidationException ||
            e instanceof ReadTimeoutException ||
            e instanceof SyntaxError ||
            e instanceof TraceRetrievalException ||
            e instanceof TruncateException ||
            e instanceof UnauthorizedException ||
            e instanceof UnavailableException ||
            e instanceof ReadTimeoutException ||
            e instanceof WriteTimeoutException ||
            e instanceof ReadFailureException ||
            e instanceof WriteFailureException ||
            e instanceof FunctionExecutionException) {
        throw new ReportedFailedException(e);
    } else {
        throw new RuntimeException(e);
    }
}
 
Example #8
Source File: CassandraController.java    From FlareBot with MIT License 4 votes vote down vote up
public static void runUnsafeTask(CassandraTask task) throws QueryExecutionException, QueryValidationException {
    task.execute(session);
}
 
Example #9
Source File: CassandraOperationImpl.java    From sunbird-lms-service with MIT License 4 votes vote down vote up
/**
 * This method performs batch operations of insert and update on a same table, further other
 * operations can be added to if it is necessary.
 *
 * @param keySpaceName
 * @param tableName
 * @param inputData
 * @return
 */
@Override
public Response performBatchAction(
    String keySpaceName, String tableName, Map<String, Object> inputData) {

  long startTime = System.currentTimeMillis();
  ProjectLogger.log(
      "Cassandra Service performBatchAction method started at ==" + startTime,
      LoggerEnum.INFO.name());

  Session session = connectionManager.getSession(keySpaceName);
  Response response = new Response();
  BatchStatement batchStatement = new BatchStatement();
  ResultSet resultSet = null;
  try {
    inputData.forEach(
        (key, inputMap) -> {
          Map<String, Object> record = (Map<String, Object>) inputMap;
          if (key.equals(JsonKey.INSERT)) {
            Insert insert = createInsertStatement(keySpaceName, tableName, record);
            batchStatement.add(insert);
          } else if (key.equals(JsonKey.UPDATE)) {
            Update update = createUpdateStatement(keySpaceName, tableName, record);
            batchStatement.add(update);
          }
        });
    resultSet = session.execute(batchStatement);
    response.put(Constants.RESPONSE, Constants.SUCCESS);
  } catch (QueryExecutionException
      | QueryValidationException
      | NoHostAvailableException
      | IllegalStateException e) {
    ProjectLogger.log(
        "Cassandra performBatchAction Failed." + e.getMessage(), LoggerEnum.ERROR.name());
    throw new ProjectCommonException(
        ResponseCode.SERVER_ERROR.getErrorCode(),
        ResponseCode.SERVER_ERROR.getErrorMessage(),
        ResponseCode.SERVER_ERROR.getResponseCode());
  }
  logQueryElapseTime("performBatchAction", startTime);
  return response;
}
 
Example #10
Source File: CassandraOperationImpl.java    From sunbird-lms-service with MIT License 4 votes vote down vote up
@Override
public Response batchInsertWithTTL(
    String keyspaceName,
    String tableName,
    List<Map<String, Object>> records,
    List<Integer> ttls) {
  long startTime = System.currentTimeMillis();
  ProjectLogger.log(
      "CassandraOperationImpl:batchInsertWithTTL: call started at " + startTime, LoggerEnum.INFO);
  if (CollectionUtils.isEmpty(records) || CollectionUtils.isEmpty(ttls)) {
    ProjectLogger.log(
        "CassandraOperationImpl:batchInsertWithTTL: records or ttls is empty", LoggerEnum.ERROR);
    ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR);
  }
  if (ttls.size() != records.size()) {
    ProjectLogger.log(
        "CassandraOperationImpl:batchInsertWithTTL: Mismatch of records and ttls list size",
        LoggerEnum.ERROR);
    ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR);
  }
  Session session = connectionManager.getSession(keyspaceName);
  Response response = new Response();
  BatchStatement batchStatement = new BatchStatement();
  ResultSet resultSet = null;
  Iterator<Integer> ttlIterator = ttls.iterator();
  try {
    for (Map<String, Object> map : records) {
      Insert insert = QueryBuilder.insertInto(keyspaceName, tableName);
      map.entrySet()
          .stream()
          .forEach(
              x -> {
                insert.value(x.getKey(), x.getValue());
              });
      if (ttlIterator.hasNext()) {
        Integer ttlVal = ttlIterator.next();
        if (ttlVal != null & ttlVal > 0) {
          insert.using(QueryBuilder.ttl(ttlVal));
        }
      }
      batchStatement.add(insert);
    }
    resultSet = session.execute(batchStatement);
    response.put(Constants.RESPONSE, Constants.SUCCESS);
  } catch (QueryExecutionException
      | QueryValidationException
      | NoHostAvailableException
      | IllegalStateException e) {
    ProjectLogger.log(
        "CassandraOperationImpl:batchInsertWithTTL: Exception occurred with error message = "
            + e.getMessage(),
        e);
    throw new ProjectCommonException(
        ResponseCode.SERVER_ERROR.getErrorCode(),
        ResponseCode.SERVER_ERROR.getErrorMessage(),
        ResponseCode.SERVER_ERROR.getResponseCode());
  }
  logQueryElapseTime("batchInsertWithTTL", startTime);
  return response;
}