use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class MapReduceBeanTest method testNoResults.
@Test(expected = NoResultsException.class)
public void testNoResults() throws Exception {
Job mockJob = createMock(Job.class);
bean.setJob(mockJob);
MapReduceJobConfiguration cfg = new MapReduceJobConfiguration() {
@Override
public final void initializeConfiguration(String jobId, Job job, Map<String, String> runtimeParameters, DatawavePrincipal serverPrincipal) throws Exception {
throw new NoResultsException(new QueryException(DatawaveErrorCode.NO_RANGES));
}
};
MapReduceConfiguration mrConfig = applicationContext.getBean(MapReduceConfiguration.class);
mrConfig.getJobConfiguration().clear();
mrConfig.getJobConfiguration().put("TestJob", cfg);
// BulkResultsJob uses AccumuloInputFormat, MapReduceJobs.xml in
// src/test/resources specifies something else
expect(ctx.getCallerPrincipal()).andReturn(principal);
replayAll();
bean.submit("TestJob", "queryId:1243;format:XML");
verifyAll();
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class ModelBean method listModelNames.
/**
* Get the names of the models
*
* @param modelTableName
* name of the table that contains the model
* @return datawave.webservice.model.ModelList
* @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
*
* @HTTP 200 success
* @HTTP 500 internal server error
*/
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/list")
@GZIP
@Interceptors(ResponseInterceptor.class)
public ModelList listModelNames(@QueryParam("modelTableName") String modelTableName) {
if (modelTableName == null) {
modelTableName = defaultModelTableName;
}
ModelList response = new ModelList(jqueryUri, dataTablesUri, modelTableName);
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String user = p.getName();
Set<Authorizations> cbAuths = new HashSet<>();
if (p instanceof DatawavePrincipal) {
DatawavePrincipal cp = (DatawavePrincipal) p;
user = cp.getShortName();
for (Collection<String> auths : cp.getAuthorizations()) {
cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
}
}
log.trace(user + " has authorizations " + cbAuths);
Connector connector = null;
HashSet<String> modelNames = new HashSet<>();
try {
Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
for (Entry<Key, Value> entry : scanner) {
String colf = entry.getKey().getColumnFamily().toString();
if (!RESERVED_COLF_VALUES.contains(colf) && !modelNames.contains(colf)) {
String[] parts = colf.split(ModelKeyParser.NULL_BYTE);
if (parts.length == 1)
modelNames.add(colf);
else if (parts.length == 2)
modelNames.add(parts[0]);
}
}
}
} catch (Exception e) {
QueryException qe = new QueryException(DatawaveErrorCode.MODEL_NAME_LIST_ERROR, e);
log.error(qe);
response.addException(qe.getBottomQueryException());
throw new DatawaveWebApplicationException(qe, response);
} finally {
if (null != connector) {
try {
connectionFactory.returnConnection(connector);
} catch (Exception e) {
log.error("Error returning connection to factory", e);
}
}
}
response.setNames(modelNames);
return response;
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class ModelBean method getModel.
/**
* Retrieve the model and all of its mappings
*
* @param name
* model name
* @param modelTableName
* name of the table that contains the model
* @return datawave.webservice.model.Model
* @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
*
* @HTTP 200 success
* @HTTP 404 model not found
* @HTTP 500 internal server error
*/
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/{name}")
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
public datawave.webservice.model.Model getModel(@Required("name") @PathParam("name") String name, @QueryParam("modelTableName") String modelTableName) {
if (modelTableName == null) {
modelTableName = defaultModelTableName;
}
datawave.webservice.model.Model response = new datawave.webservice.model.Model(jqueryUri, dataTablesUri);
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String user = p.getName();
Set<Authorizations> cbAuths = new HashSet<>();
if (p instanceof DatawavePrincipal) {
DatawavePrincipal cp = (DatawavePrincipal) p;
user = cp.getShortName();
for (Collection<String> auths : cp.getAuthorizations()) {
cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
}
}
log.trace(user + " has authorizations " + cbAuths);
Connector connector = null;
try {
Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
IteratorSetting cfg = new IteratorSetting(21, "colfRegex", RegExFilter.class.getName());
cfg.addOption(RegExFilter.COLF_REGEX, "^" + name + "(\\x00.*)?");
scanner.addScanIterator(cfg);
for (Entry<Key, Value> entry : scanner) {
FieldMapping mapping = ModelKeyParser.parseKey(entry.getKey(), cbAuths);
response.getFields().add(mapping);
}
}
} catch (Exception e) {
QueryException qe = new QueryException(DatawaveErrorCode.MODEL_FETCH_ERROR, e);
log.error(qe);
response.addException(qe.getBottomQueryException());
throw new DatawaveWebApplicationException(qe, response);
} finally {
if (null != connector) {
try {
connectionFactory.returnConnection(connector);
} catch (Exception e) {
log.error("Error returning connection to factory", e);
}
}
}
// return 404 if model not found
if (response.getFields().isEmpty()) {
throw new NotFoundException(null, response);
}
response.setName(name);
return response;
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class BulkResultsJobConfiguration method _initializeConfiguration.
@Override
public void _initializeConfiguration(Job job, Path jobDir, String jobId, Map<String, String> runtimeParameters, DatawavePrincipal serverPrincipal) throws IOException, QueryException {
String queryId = runtimeParameters.get("queryId");
SerializationFormat format = SerializationFormat.valueOf(runtimeParameters.get("format"));
String outputFormatParameter = runtimeParameters.get("outputFormat");
if (outputFormatParameter != null && outputFormatParameter.equalsIgnoreCase("TEXT")) {
this.outputFormatClass = TextOutputFormat.class;
}
if (runtimeParameters.containsKey("outputTableName"))
this.tableName = runtimeParameters.get("outputTableName");
// Initialize the Query
QueryLogic<?> logic;
GenericQueryConfiguration queryConfig;
String base64EncodedQuery;
Class<? extends Query> queryImplClass;
Set<Authorizations> runtimeQueryAuthorizations;
try {
QuerySettings settings = setupQuery(sid, queryId, principal);
logic = settings.getLogic();
queryConfig = settings.getQueryConfig();
base64EncodedQuery = settings.getBase64EncodedQuery();
queryImplClass = settings.getQueryImplClass();
runtimeQueryAuthorizations = settings.getRuntimeQueryAuthorizations();
} catch (QueryException qe) {
log.error("Error getting Query for id: " + queryId, qe);
throw qe;
} catch (Exception e) {
log.error("Error setting up Query for id: " + queryId, e);
throw new QueryException(e);
}
// Setup and run the MapReduce job
try {
setupJob(job, jobDir, queryConfig, logic, base64EncodedQuery, queryImplClass, runtimeQueryAuthorizations, serverPrincipal);
if (null == this.tableName) {
// Setup job for output to HDFS
// set the mapper
job.setMapperClass(BulkResultsFileOutputMapper.class);
job.getConfiguration().set(BulkResultsFileOutputMapper.RESULT_SERIALIZATION_FORMAT, format.name());
// Setup the output
job.setOutputFormatClass(outputFormatClass);
job.setOutputKeyClass(Key.class);
job.setOutputValueClass(Value.class);
if (this.outputFormatClass.equals(SequenceFileOutputFormat.class)) {
SequenceFileOutputFormat.setOutputCompressionType(job, CompressionType.BLOCK);
} else if (this.outputFormatClass.equals(TextOutputFormat.class)) {
// if we are writing Text output to hdfs, we don't want to write key-tab-value, we want just the value
// this property gets fetched in the Mapper to skip writing the key
job.setOutputKeyClass(NullWritable.class);
}
job.setNumReduceTasks(0);
SequenceFileOutputFormat.setOutputPath(job, new Path(this.getResultsDir()));
} else {
// Setup job for output to table.
// set the mapper
job.setMapperClass(BulkResultsTableOutputMapper.class);
job.getConfiguration().set(BulkResultsTableOutputMapper.TABLE_NAME, tableName);
job.getConfiguration().set(BulkResultsFileOutputMapper.RESULT_SERIALIZATION_FORMAT, format.name());
// Setup the output
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Mutation.class);
job.setNumReduceTasks(0);
job.setOutputFormatClass(AccumuloOutputFormat.class);
AccumuloOutputFormat.setZooKeeperInstance(job, ClientConfiguration.loadDefault().withInstance(instanceName).withZkHosts(zookeepers));
AccumuloOutputFormat.setConnectorInfo(job, user, new PasswordToken(password));
AccumuloOutputFormat.setCreateTables(job, true);
AccumuloOutputFormat.setDefaultTableName(job, tableName);
// AccumuloOutputFormat.loglevel
AccumuloOutputFormat.setLogLevel(job, Level.INFO);
// AccumuloOutputFormat.maxlatency
// AccumuloOutputFormat.maxmemory
// AccumuloOutputFormat.writethreads
AccumuloOutputFormat.setBatchWriterOptions(job, new BatchWriterConfig().setMaxLatency(30, TimeUnit.SECONDS).setMaxMemory(10485760).setMaxWriteThreads(2));
}
} catch (WebApplicationException wex) {
throw wex;
} catch (Exception e) {
log.error("Error starting job", e);
throw new QueryException(DatawaveErrorCode.JOB_STARTING_ERROR, e);
}
}
use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.
the class BulkResultsJobConfiguration method setupQuery.
private QuerySettings setupQuery(String sid, String queryId, Principal principal) throws Exception {
Connector connector = null;
QueryLogic<?> logic = null;
try {
// Get the query by the query id
Query q = getQueryById(queryId);
if (!sid.equals(q.getOwner()))
throw new QueryException("This query does not belong to you. expected: " + q.getOwner() + ", value: " + sid, Response.Status.UNAUTHORIZED.getStatusCode());
// will throw IllegalArgumentException if not defined
logic = queryFactory.getQueryLogic(q.getQueryLogicName(), principal);
// Get an accumulo connection
Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
connector = connectionFactory.getConnection(logic.getConnectionPriority(), trackingMap);
// Merge user auths with the auths that they use in the Query
Set<Authorizations> runtimeQueryAuthorizations = AuthorizationsUtil.getDowngradedAuthorizations(q.getQueryAuthorizations(), principal);
// Initialize the logic so that the configuration contains all of the iterator options
GenericQueryConfiguration queryConfig = logic.initialize(connector, q, runtimeQueryAuthorizations);
String base64EncodedQuery = BulkResultsFileOutputMapper.serializeQuery(q);
return new QuerySettings(logic, queryConfig, base64EncodedQuery, q.getClass(), runtimeQueryAuthorizations);
} finally {
if (null != logic && null != connector)
connectionFactory.returnConnection(connector);
}
}
Aggregations