use of datawave.webservice.query.Query in project datawave by NationalSecurityAgency.
the class CachedResultsBean method loadAndCreateAsync.
// Do not use the @Asynchronous annotation here. This method runs, setting status and
// then executes loadAndCreate asynchronously. It does not itself get run asynchronously.
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
public Future<CachedResultsResponse> loadAndCreateAsync(MultivaluedMap<String, String> queryParameters) {
String newQueryId = queryParameters.getFirst("newQueryId");
Preconditions.checkNotNull(newQueryId, "newQueryId cannot be null");
String queryId = queryParameters.getFirst(CachedResultsParameters.QUERY_ID);
Preconditions.checkNotNull(queryId, "queryId cannot be null");
String alias = queryParameters.getFirst("alias");
if (alias == null) {
alias = newQueryId;
}
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String owner = getOwnerFromPrincipal(p);
CachedRunningQuery crq = null;
try {
persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.LOADING, "", true);
crq = retrieve(newQueryId, owner);
} catch (IOException e1) {
log.error("Error trying to persist/retrieve cached results", e1);
try {
persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.ERROR, e1.getMessage(), false);
} catch (IOException e2) {
log.error(e2.getMessage(), e2);
}
PreConditionFailedQueryException e = new PreConditionFailedQueryException(DatawaveErrorCode.CACHED_RESULTS_PERSIST_ERROR, e1);
throw new PreConditionFailedException(e, null);
}
RunningQuery rq = null;
try {
rq = getQueryById(queryId);
if (rq != null) {
String nameBase = UUID.randomUUID().toString().replaceAll("-", "");
Query q = rq.getSettings();
crq.setOriginalQueryId(q.getId().toString());
crq.setView(nameBase);
crq.setAlias(alias);
crq.setQuery(rq.getSettings());
persist(crq, owner);
}
} catch (Exception e) {
String statusMessage = e.getMessage();
if (null == statusMessage) {
statusMessage = e.getClass().getName();
}
try {
persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.ERROR, statusMessage, true);
} catch (IOException e1) {
log.error("Error persisting status to CachedResult store", e1);
}
log.error(e.getMessage(), e);
}
// pagesize validated in loadAndCreate
return new AsyncResult<>(loadAndCreate(queryId, queryParameters));
}
use of datawave.webservice.query.Query in project datawave by NationalSecurityAgency.
the class CachedRunningQuery method retrieveFromDatabase.
public static CachedRunningQuery retrieveFromDatabase(String id, Principal principal, QueryMetricFactory metricFactory) {
verifyCrqTableExists();
CachedRunningQuery crq = null;
String sql = "SELECT * FROM cachedResultsQuery WHERE alias=? OR queryId=? OR view=?";
try (Connection localConnection = datasource.getConnection();
PreparedStatement statement = localConnection.prepareStatement(sql)) {
statement.setString(1, id);
statement.setString(2, id);
statement.setString(3, id);
try (ResultSet resultSet = statement.executeQuery()) {
resultSet.last();
int numRows = resultSet.getRow();
if (numRows > 0) {
resultSet.first();
crq = new CachedRunningQuery(metricFactory);
int x = 1;
crq.queryId = resultSet.getString(x++);
crq.alias = resultSet.getString(x++);
// last updated
resultSet.getTimestamp(x++);
crq.pagesize = resultSet.getInt(x++);
crq.user = resultSet.getString(x++);
crq.view = CachedResultsParameters.validate(resultSet.getString(x++), true);
crq.tableName = resultSet.getString(x++);
crq.getMetric().setQueryType(CachedRunningQuery.class);
crq.getMetric().setQueryId(crq.queryId);
crq.getMetric().setUser(crq.user);
String s = resultSet.getString(x++);
if (s != null) {
crq.status = Status.valueOf(s);
}
crq.statusMessage = resultSet.getString(x++);
crq.fields = resultSet.getString(x++);
crq.conditions = resultSet.getString(x++);
crq.grouping = resultSet.getString(x++);
crq.order = resultSet.getString(x++);
String varFields = resultSet.getString(x++);
if (varFields != null) {
crq.variableFields.addAll(Arrays.asList(varFields.split(" ")));
}
Query query = crq.responseObjectFactory.getQueryImpl();
query.setQuery(resultSet.getString(x++));
Timestamp bDate = resultSet.getTimestamp(x++);
if (bDate == null) {
query.setBeginDate(null);
} else {
query.setBeginDate(new Date(bDate.getTime()));
}
Timestamp eDate = resultSet.getTimestamp(x++);
if (bDate == null) {
query.setBeginDate(null);
} else {
query.setEndDate(new Date(eDate.getTime()));
}
query.setQueryAuthorizations(resultSet.getString(x++));
query.setQueryLogicName(resultSet.getString(x++));
query.setQueryName(resultSet.getString(x++));
query.setUserDN(resultSet.getString(x++));
String uuid = resultSet.getString(x++);
if (uuid != null) {
query.setId(UUID.fromString(uuid));
}
query.setPagesize(resultSet.getInt(x++));
String fixedFields = resultSet.getString(x++);
if (!StringUtils.isEmpty(fixedFields)) {
for (String field : fixedFields.split(",")) crq.fixedFieldsInEvent.add(field.trim());
}
Blob optionalQueryParametersBlob = resultSet.getBlob(x++);
if (optionalQueryParametersBlob != null) {
try {
InputStream istream = optionalQueryParametersBlob.getBinaryStream();
ObjectInputStream oistream = new ObjectInputStream(istream);
Object optionalQueryParametersObject = oistream.readObject();
if (optionalQueryParametersObject != null && optionalQueryParametersObject instanceof MultiValueMap) {
MultiValueMap<String, String> optionalQueryParameters = (MultiValueMap<String, String>) optionalQueryParametersObject;
query.setOptionalQueryParameters(optionalQueryParameters);
}
} catch (IOException e) {
log.error(e.getMessage(), e);
} catch (ClassNotFoundException e) {
log.error(e.getMessage(), e);
}
}
crq.query = query;
crq.queryLogicName = query.getQueryLogicName();
crq.originalQueryId = uuid;
if (crq.view != null && crq.fields != null && crq.user != null) {
crq.sqlQuery = crq.generateSql(crq.view, crq.fields, crq.conditions, crq.grouping, crq.order, crq.user, localConnection);
}
if (crq.queryLogicName != null) {
try {
crq.queryLogic = queryFactory.getQueryLogic(crq.queryLogicName, principal);
} catch (IllegalArgumentException | CloneNotSupportedException e) {
log.error(e.getMessage(), e);
}
}
}
}
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
return crq;
}
use of datawave.webservice.query.Query in project datawave by NationalSecurityAgency.
the class BulkResultsFileOutputMapper method deserializeQuery.
public static Query deserializeQuery(String base64EncodedQuery, Class<? extends Query> queryImplClass) throws JAXBException {
String query = new String(Base64.decodeBase64(base64EncodedQuery), Charset.forName("UTF-8"));
JAXBContext ctx = JAXBContext.newInstance(queryImplClass);
Unmarshaller u = ctx.createUnmarshaller();
return (Query) u.unmarshal(new StringReader(query));
}
use of datawave.webservice.query.Query in project datawave by NationalSecurityAgency.
the class BulkResultsFileOutputMapper method setup.
@Override
protected void setup(org.apache.hadoop.mapreduce.Mapper<Key, Value, Key, Value>.Context context) throws IOException, InterruptedException {
if (System.getProperty("ignore.weld.startMain") == null) {
// Disable CDI extensions in Jersey libs
System.setProperty("com.sun.jersey.server.impl.cdi.lookupExtensionInBeanManager", "true");
weld = new Weld("STATIC_INSTANCE");
weld.initialize();
}
super.setup(context);
Query query;
try {
Class<? extends Query> queryImplClass = Class.forName(context.getConfiguration().get(QUERY_IMPL_CLASS)).asSubclass(Query.class);
query = deserializeQuery(context.getConfiguration().get(QUERY_LOGIC_SETTINGS), queryImplClass);
} catch (JAXBException e) {
throw new RuntimeException("Error deserializing Query", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Error instantiating query impl class " + context.getConfiguration().get(QUERY_IMPL_CLASS), e);
}
final Configuration configuration = context.getConfiguration();
this.setApplicationContext(configuration.get(SPRING_CONFIG_LOCATIONS));
String logicName = context.getConfiguration().get(QUERY_LOGIC_NAME);
QueryLogic<?> logic = (QueryLogic<?>) super.applicationContext.getBean(logicName);
t = logic.getTransformer(query);
Assert.notNull(logic.getMarkingFunctions());
Assert.notNull(logic.getResponseObjectFactory());
this.format = SerializationFormat.valueOf(context.getConfiguration().get(RESULT_SERIALIZATION_FORMAT));
}
use of datawave.webservice.query.Query in project datawave by NationalSecurityAgency.
the class BulkResultsTableOutputMapper method setup.
@Override
protected void setup(org.apache.hadoop.mapreduce.Mapper<Key, Value, Text, Mutation>.Context context) throws IOException, InterruptedException {
super.setup(context);
Query query;
try {
String base64EncodedQuery = context.getConfiguration().get(BulkResultsFileOutputMapper.QUERY_LOGIC_SETTINGS);
Class<? extends Query> queryImplClass = Class.forName(context.getConfiguration().get(BulkResultsFileOutputMapper.QUERY_IMPL_CLASS)).asSubclass(Query.class);
query = BulkResultsFileOutputMapper.deserializeQuery(base64EncodedQuery, queryImplClass);
} catch (JAXBException e) {
throw new RuntimeException("Error deserializing Query", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Error instantiating query impl class " + context.getConfiguration().get(BulkResultsFileOutputMapper.QUERY_IMPL_CLASS), e);
}
QueryLogic<?> logic = (QueryLogic<?>) super.applicationContext.getBean(QUERY_LOGIC_NAME);
t = logic.getTransformer(query);
this.tableName = new Text(context.getConfiguration().get(TABLE_NAME));
this.format = SerializationFormat.valueOf(context.getConfiguration().get(BulkResultsFileOutputMapper.RESULT_SERIALIZATION_FORMAT));
}
Aggregations