use of com.mongodb.MongoException in project mongo-java-driver by mongodb.
the class MongoClientFactory method getObjectInstance.
/**
* This implementation will create instances of {@link MongoClient} based on a connection string conforming to the format specified in
* {@link MongoClientURI}.
* <p>The connection string is specified in one of two ways:</p>
* <ul>
* <li>As the {@code String} value of a property in the {@code environment} parameter with a key of {@code "connectionString"}</li>
* <li>As the {@code String} value of a {@link RefAddr} with type {@code "connectionString"} in an {@code obj} parameter
* of type {@link Reference}</li>
* </ul>
*
* Specification of the connection string in the {@code environment} parameter takes precedence over specification in the {@code obj}
* parameter. The {@code name} and {@code nameCtx} parameters are ignored.
*
* If a non-empty connection string is not specified in either of these two ways, a {@link MongoException} is thrown.
* @return an instance of {@link MongoClient} based on the specified connection string
* @throws MongoException
*
* Note: Not all options that can be specified via {@link com.mongodb.MongoClientOptions} can be specified via the connection string.
*/
@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception {
// Some app servers, e.g. Wildfly, use the environment to pass location information to an ObjectFactory
String connectionString = null;
if (environment.get(CONNECTION_STRING) instanceof String) {
connectionString = (String) environment.get(CONNECTION_STRING);
}
if (connectionString == null || connectionString.isEmpty()) {
LOGGER.debug(format("No '%s' property in environment. Casting 'obj' to java.naming.Reference to look for a " + "javax.naming.RefAddr with type equal to '%s'", CONNECTION_STRING, CONNECTION_STRING));
// javax.naming.RefAddr
if (obj instanceof Reference) {
Enumeration<RefAddr> props = ((Reference) obj).getAll();
while (props.hasMoreElements()) {
RefAddr addr = props.nextElement();
if (addr != null) {
if (CONNECTION_STRING.equals(addr.getType())) {
if (addr.getContent() instanceof String) {
connectionString = (String) addr.getContent();
break;
}
}
}
}
}
}
if (connectionString == null || connectionString.isEmpty()) {
throw new MongoException(format("Could not locate '%s' in either environment or obj", CONNECTION_STRING));
}
MongoClientURI uri = new MongoClientURI(connectionString);
return new MongoClient(uri);
}
use of com.mongodb.MongoException in project mongo-java-driver by mongodb.
the class GridFSFile method validate.
/**
* Verifies that the MD5 matches between the database and the local file. This should be called after transferring a file.
*
* @throws MongoException if there's a failure
*/
public void validate() {
if (fs == null) {
throw new MongoException("no fs");
}
if (md5 == null) {
throw new MongoException("no md5 stored");
}
DBObject cmd = new BasicDBObject("filemd5", id);
cmd.put("root", fs.getBucketName());
DBObject res = fs.getDB().command(cmd);
if (res != null && res.containsField("md5")) {
String m = res.get("md5").toString();
if (m.equals(md5)) {
return;
}
throw new MongoException("md5 differ. mine [" + md5 + "] theirs [" + m + "]");
}
// no md5 from the server
throw new MongoException("no md5 returned from server: " + res);
}
use of com.mongodb.MongoException in project mongo-hadoop by mongodb.
the class MongoRecordReader method nextKeyValue.
public boolean nextKeyValue() throws IOException {
try {
if (!cursor.hasNext()) {
LOG.info("Read " + seen + " documents from:");
LOG.info(split.toString());
return false;
}
DBObject next = cursor.next();
this.currentVal.setDoc(next);
this.currentKey.setDoc(new BasicBSONObject("_id", next.get("_id")));
seen++;
return true;
} catch (MongoException e) {
throw new IOException("Couldn't get next key/value from mongodb: ", e);
}
}
use of com.mongodb.MongoException in project mongo-hadoop by mongodb.
the class MongoOutputCommitter method commitTask.
public void commitTask(final CompatUtils.TaskAttemptContext taskContext) throws IOException {
LOG.info("Committing task.");
collection = MongoConfigUtil.getOutputCollection(taskContext.getConfiguration());
// Get temporary file.
Path tempFilePath = getTaskAttemptPath(taskContext);
LOG.info("Committing from temporary file: " + tempFilePath.toString());
long filePos = 0, fileLen;
FSDataInputStream inputStream = null;
try {
FileSystem fs = FileSystem.get(taskContext.getConfiguration());
inputStream = fs.open(tempFilePath);
fileLen = fs.getFileStatus(tempFilePath).getLen();
} catch (IOException e) {
LOG.error("Could not open temporary file for committing", e);
cleanupAfterCommit(inputStream, taskContext);
throw e;
}
int maxDocs = MongoConfigUtil.getBatchSize(taskContext.getConfiguration());
int curBatchSize = 0;
BulkWriteOperation bulkOp;
if (MongoConfigUtil.isBulkOrdered(taskContext.getConfiguration())) {
bulkOp = collection.initializeOrderedBulkOperation();
} else {
bulkOp = collection.initializeUnorderedBulkOperation();
}
// Read Writables out of the temporary file.
BSONWritable bw = new BSONWritable();
MongoUpdateWritable muw = new MongoUpdateWritable();
while (filePos < fileLen) {
try {
// Determine writable type, and perform corresponding operation
// on MongoDB.
int mwType = inputStream.readInt();
if (MongoWritableTypes.BSON_WRITABLE == mwType) {
bw.readFields(inputStream);
bulkOp.insert(new BasicDBObject(bw.getDoc().toMap()));
} else if (MongoWritableTypes.MONGO_UPDATE_WRITABLE == mwType) {
muw.readFields(inputStream);
DBObject query = new BasicDBObject(muw.getQuery().toMap());
DBObject modifiers = new BasicDBObject(muw.getModifiers().toMap());
BulkWriteRequestBuilder writeBuilder = bulkOp.find(query);
if (muw.isReplace()) {
writeBuilder.replaceOne(modifiers);
} else if (muw.isUpsert()) {
BulkUpdateRequestBuilder updateBuilder = writeBuilder.upsert();
if (muw.isMultiUpdate()) {
updateBuilder.update(modifiers);
} else {
updateBuilder.updateOne(modifiers);
}
} else {
// No-upsert update.
if (muw.isMultiUpdate()) {
writeBuilder.update(modifiers);
} else {
writeBuilder.updateOne(modifiers);
}
}
} else {
throw new IOException("Unrecognized type: " + mwType);
}
filePos = inputStream.getPos();
// operation to be performed for the Task.
if (++curBatchSize >= maxDocs || filePos >= fileLen) {
try {
bulkOp.execute();
} catch (MongoException e) {
LOG.error("Could not write to MongoDB", e);
throw e;
}
bulkOp = collection.initializeOrderedBulkOperation();
curBatchSize = 0;
// Signal progress back to Hadoop framework so that we
// don't time out.
taskContext.progress();
}
} catch (IOException e) {
LOG.error("Error reading from temporary file", e);
throw e;
}
}
cleanupAfterCommit(inputStream, taskContext);
}
use of com.mongodb.MongoException in project mongo-hadoop by mongodb.
the class SampleSplitter method calculateSplits.
@Override
public List<InputSplit> calculateSplits() throws SplitFailedException {
Configuration conf = getConfiguration();
long splitSizeMB = MongoConfigUtil.getSplitSize(conf);
long samplesPerSplit = MongoConfigUtil.getSamplesPerSplit(conf);
DBObject splitKey = MongoConfigUtil.getInputSplitKey(conf);
DBCollection inputCollection = MongoConfigUtil.getInputCollection(conf);
CommandResult result = inputCollection.getDB().command(new BasicDBObject("collstats", inputCollection.getName()));
if (!result.ok()) {
throw new SplitFailedException("Could not execute command 'collstats': " + result.getErrorMessage());
}
int count = result.getInt("count");
int avgObjSize = result.getInt("avgObjSize");
int numDocsPerSplit = (int) Math.floor(splitSizeMB * 1024 * 1024 / avgObjSize);
int numSplits = (int) Math.ceil((double) count / numDocsPerSplit);
int totalSamples = (int) Math.floor(samplesPerSplit * numSplits);
if (count < numDocsPerSplit) {
LOG.warn("Not enough documents for more than one split! Consider " + "setting " + MongoConfigUtil.INPUT_SPLIT_SIZE + " to a " + "lower value.");
InputSplit split = createSplitFromBounds(null, null);
return Collections.singletonList(split);
}
DBObject[] pipeline = { new BasicDBObjectBuilder().push("$sample").add("size", totalSamples).get(), new BasicDBObject("$project", splitKey), new BasicDBObject("$sort", splitKey) };
AggregationOutput aggregationOutput;
try {
aggregationOutput = inputCollection.aggregate(Arrays.asList(pipeline));
} catch (MongoException e) {
throw new SplitFailedException("Failed to aggregate sample documents. Note that this Splitter " + "implementation is incompatible with MongoDB versions " + "prior to 3.2.", e);
}
BasicDBObject previousKey = null;
List<InputSplit> splits = new ArrayList<InputSplit>(numSplits);
int i = 0;
for (DBObject sample : aggregationOutput.results()) {
if (i++ % samplesPerSplit == 0) {
BasicDBObject bdbo = (BasicDBObject) sample;
splits.add(createSplitFromBounds(previousKey, bdbo));
previousKey = bdbo;
}
}
splits.add(createSplitFromBounds(previousKey, null));
if (MongoConfigUtil.isFilterEmptySplitsEnabled(conf)) {
return filterEmptySplits(splits);
}
return splits;
}
Aggregations