Search in sources :

Example 21 with MongoException

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);
}
Also used : RefAddr(javax.naming.RefAddr) MongoClient(com.mongodb.MongoClient) MongoException(com.mongodb.MongoException) Reference(javax.naming.Reference) MongoClientURI(com.mongodb.MongoClientURI)

Example 22 with MongoException

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);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 23 with MongoException

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);
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) MongoException(com.mongodb.MongoException) IOException(java.io.IOException) DBObject(com.mongodb.DBObject)

Example 24 with MongoException

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);
}
Also used : Path(org.apache.hadoop.fs.Path) BulkWriteOperation(com.mongodb.BulkWriteOperation) MongoException(com.mongodb.MongoException) IOException(java.io.IOException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BSONWritable(com.mongodb.hadoop.io.BSONWritable) BasicDBObject(com.mongodb.BasicDBObject) BulkWriteRequestBuilder(com.mongodb.BulkWriteRequestBuilder) FileSystem(org.apache.hadoop.fs.FileSystem) MongoUpdateWritable(com.mongodb.hadoop.io.MongoUpdateWritable) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) BulkUpdateRequestBuilder(com.mongodb.BulkUpdateRequestBuilder)

Example 25 with MongoException

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;
}
Also used : BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) MongoException(com.mongodb.MongoException) Configuration(org.apache.hadoop.conf.Configuration) ArrayList(java.util.ArrayList) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) AggregationOutput(com.mongodb.AggregationOutput) CommandResult(com.mongodb.CommandResult) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) InputSplit(org.apache.hadoop.mapreduce.InputSplit)

Aggregations

MongoException (com.mongodb.MongoException)42 BasicDBObject (com.mongodb.BasicDBObject)22 DBObject (com.mongodb.DBObject)21 DBCollection (com.mongodb.DBCollection)16 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 DBCursor (com.mongodb.DBCursor)8 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)4 IOException (java.io.IOException)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 JSONException (org.json.JSONException)4 Stopwatch (com.google.common.base.Stopwatch)3 BasicDBList (com.mongodb.BasicDBList)3 DuplicateKeyException (com.mongodb.DuplicateKeyException)3 MongoClient (com.mongodb.MongoClient)3 BulkWriteOperation (com.mongodb.BulkWriteOperation)2 CommandResult (com.mongodb.CommandResult)2 MongoClientURI (com.mongodb.MongoClientURI)2