Search in sources :

Example 1 with Query

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));
}
Also used : RunningQuery(datawave.webservice.query.runner.RunningQuery) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) Query(datawave.webservice.query.Query) RunningQuery(datawave.webservice.query.runner.RunningQuery) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) IOException(java.io.IOException) AsyncResult(javax.ejb.AsyncResult) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) EJBException(javax.ejb.EJBException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) NoResultsException(datawave.webservice.common.exception.NoResultsException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) MalformedURLException(java.net.MalformedURLException) Interceptors(javax.interceptor.Interceptors)

Example 2 with Query

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;
}
Also used : Blob(java.sql.Blob) Query(datawave.webservice.query.Query) AbstractRunningQuery(datawave.webservice.query.cache.AbstractRunningQuery) SQLException(java.sql.SQLException) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) Date(java.util.Date) ResultSet(java.sql.ResultSet) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with Query

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));
}
Also used : Query(datawave.webservice.query.Query) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with 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));
}
Also used : Query(datawave.webservice.query.Query) Configuration(org.apache.hadoop.conf.Configuration) JAXBException(javax.xml.bind.JAXBException) QueryLogic(datawave.webservice.query.logic.QueryLogic) Weld(org.jboss.weld.environment.se.Weld)

Example 5 with Query

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));
}
Also used : Query(datawave.webservice.query.Query) JAXBException(javax.xml.bind.JAXBException) Text(org.apache.hadoop.io.Text) QueryLogic(datawave.webservice.query.logic.QueryLogic)

Aggregations

Query (datawave.webservice.query.Query)81 QueryImpl (datawave.webservice.query.QueryImpl)39 Test (org.junit.Test)36 HashSet (java.util.HashSet)24 MultivaluedMapImpl (org.jboss.resteasy.specimpl.MultivaluedMapImpl)23 QueryException (datawave.webservice.query.exception.QueryException)21 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)20 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)19 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)18 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)18 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)18 IOException (java.io.IOException)18 NoResultsException (datawave.webservice.common.exception.NoResultsException)17 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)16 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)16 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)15 Interceptors (javax.interceptor.Interceptors)15 Authorizations (org.apache.accumulo.core.security.Authorizations)15 Timed (com.codahale.metrics.annotation.Timed)14 Principal (java.security.Principal)14