Search in sources :

Example 11 with CommandResult

use of com.mongodb.CommandResult in project mongo-hadoop by mongodb.

the class BaseHadoopTest method isSharded.

protected boolean isSharded(final MongoClientURI uri) {
    final CommandResult isMasterResult = runIsMaster(uri);
    final Object msg = isMasterResult.get("msg");
    return msg != null && msg.equals("isdbgrid");
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) CommandResult(com.mongodb.CommandResult)

Example 12 with CommandResult

use of com.mongodb.CommandResult 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)

Example 13 with CommandResult

use of com.mongodb.CommandResult in project graylog2-server by Graylog2.

the class MongoProbe method createHostInfo.

private static HostInfo createHostInfo(DB adminDb) {
    final HostInfo hostInfo;
    final CommandResult hostInfoResult = adminDb.command("hostInfo");
    if (hostInfoResult.ok()) {
        final BasicDBObject systemMap = (BasicDBObject) hostInfoResult.get("system");
        final HostInfo.System system = HostInfo.System.create(new DateTime(systemMap.getDate("currentTime")), systemMap.getString("hostname"), systemMap.getInt("cpuAddrSize"), systemMap.getLong("memSizeMB"), systemMap.getInt("numCores"), systemMap.getString("cpuArch"), systemMap.getBoolean("numaEnabled"));
        final BasicDBObject osMap = (BasicDBObject) hostInfoResult.get("os");
        final HostInfo.Os os = HostInfo.Os.create(osMap.getString("type"), osMap.getString("name"), osMap.getString("version"));
        final BasicDBObject extraMap = (BasicDBObject) hostInfoResult.get("extra");
        final HostInfo.Extra extra = HostInfo.Extra.create(extraMap.getString("versionString"), extraMap.getString("libcVersion"), extraMap.getString("kernelVersion"), extraMap.getString("cpuFrequencyMHz"), extraMap.getString("cpuFeatures"), extraMap.getString("scheduler"), extraMap.getLong("pageSize", -1L), extraMap.getLong("numPages", -1L), extraMap.getLong("maxOpenFiles", -1L));
        hostInfo = HostInfo.create(system, os, extra);
    } else {
        LOG.debug("Couldn't retrieve MongoDB hostInfo: {}", hostInfoResult.getErrorMessage());
        hostInfo = null;
    }
    return hostInfo;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DateTime(org.joda.time.DateTime) CommandResult(com.mongodb.CommandResult)

Example 14 with CommandResult

use of com.mongodb.CommandResult in project graylog2-server by Graylog2.

the class MongoProbe method mongoStats.

public MongoStats mongoStats() {
    final List<ServerAddress> serverAddresses = mongoClient.getServerAddressList();
    final List<HostAndPort> servers = Lists.newArrayListWithCapacity(serverAddresses.size());
    for (ServerAddress serverAddress : serverAddresses) {
        servers.add(HostAndPort.fromParts(serverAddress.getHost(), serverAddress.getPort()));
    }
    final DatabaseStats dbStats;
    final CommandResult dbStatsResult = db.command("dbStats");
    if (dbStatsResult.ok()) {
        final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList");
        final DatabaseStats.ExtentFreeList extentFreeList;
        if (extentFreeListMap == null) {
            extentFreeList = null;
        } else {
            extentFreeList = DatabaseStats.ExtentFreeList.create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize"));
        }
        final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion");
        final DatabaseStats.DataFileVersion dataFileVersion;
        if (dataFileVersionMap == null) {
            dataFileVersion = null;
        } else {
            dataFileVersion = DatabaseStats.DataFileVersion.create(dataFileVersionMap.getInt("major"), dataFileVersionMap.getInt("minor"));
        }
        dbStats = DatabaseStats.create(dbStatsResult.getString("db"), dbStatsResult.getLong("collections"), dbStatsResult.getLong("objects"), dbStatsResult.getDouble("avgObjSize"), dbStatsResult.getLong("dataSize"), dbStatsResult.getLong("storageSize"), dbStatsResult.containsField("numExtents") ? dbStatsResult.getLong("numExtents") : null, dbStatsResult.getLong("indexes"), dbStatsResult.getLong("indexSize"), dbStatsResult.containsField("fileSize") ? dbStatsResult.getLong("fileSize") : null, dbStatsResult.containsField("nsSizeMB") ? dbStatsResult.getLong("nsSizeMB") : null, extentFreeList, dataFileVersion);
    } else {
        LOG.debug("Couldn't retrieve MongoDB dbStats: {}", dbStatsResult.getErrorMessage());
        dbStats = null;
    }
    final ServerStatus serverStatus;
    final CommandResult serverStatusResult = adminDb.command("serverStatus");
    if (serverStatusResult.ok()) {
        final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections");
        final ServerStatus.Connections connections = ServerStatus.Connections.create(connectionsMap.getInt("current"), connectionsMap.getInt("available"), connectionsMap.containsField("totalCreated") ? connectionsMap.getLong("totalCreated") : null);
        final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network");
        final ServerStatus.Network network = ServerStatus.Network.create(networkMap.getInt("bytesIn"), networkMap.getInt("bytesOut"), networkMap.getInt("numRequests"));
        final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem");
        final ServerStatus.Memory memory = ServerStatus.Memory.create(memoryMap.getInt("bits"), memoryMap.getInt("resident"), memoryMap.getInt("virtual"), memoryMap.getBoolean("supported"), memoryMap.getInt("mapped", -1), memoryMap.getInt("mappedWithJournal", -1));
        final BasicDBObject storageEngineMap = (BasicDBObject) serverStatusResult.get("storageEngine");
        final ServerStatus.StorageEngine storageEngine;
        if (storageEngineMap == null) {
            storageEngine = ServerStatus.StorageEngine.DEFAULT;
        } else {
            storageEngine = ServerStatus.StorageEngine.create(storageEngineMap.getString("name"));
        }
        final int uptime = serverStatusResult.getInt("uptime", 0);
        serverStatus = ServerStatus.create(serverStatusResult.getString("host"), serverStatusResult.getString("version"), serverStatusResult.getString("process"), serverStatusResult.getLong("pid", 0), uptime, serverStatusResult.getLong("uptimeMillis", uptime * 1000L), serverStatusResult.getInt("uptimeEstimate"), new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory, storageEngine);
    } else {
        LOG.debug("Couldn't retrieve MongoDB serverStatus: {}", serverStatusResult.getErrorMessage());
        serverStatus = null;
    }
    // TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/
    return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats);
}
Also used : ServerAddress(com.mongodb.ServerAddress) DateTime(org.joda.time.DateTime) CommandResult(com.mongodb.CommandResult) HostAndPort(com.google.common.net.HostAndPort) BasicDBObject(com.mongodb.BasicDBObject)

Example 15 with CommandResult

use of com.mongodb.CommandResult in project commons by craftercms.

the class MongoScriptRunner method runScript.

private void runScript(DB db, Resource scriptPath) throws MongoDataException {
    String script;
    try {
        if (scriptPath.getFile().isDirectory()) {
            final File[] files = scriptPath.getFile().listFiles(new FilenameFilter() {

                @Override
                public boolean accept(final File dir, final String name) {
                    return name.toLowerCase().endsWith(".js");
                }
            });
            List<File> orderFiles = Arrays.asList(files);
            Collections.sort(orderFiles, new Comparator<File>() {

                @Override
                public int compare(final File o1, final File o2) {
                    return o1.getName().compareTo(o2.getName());
                }
            });
            logger.debug("Directory {} files to exec {}", scriptPath.getFile(), orderFiles);
            for (File file : orderFiles) {
                runScript(db, new FileSystemResource(file.getPath()));
            }
        } else {
            logger.debug("Running Script {}", scriptPath.getURI());
            try {
                script = IOUtils.toString(scriptPath.getInputStream(), "UTF-8");
            } catch (IOException e) {
                throw new MongoDataException("Unable to read script at " + scriptPath.getURI().toString());
            }
            CommandResult result = db.doEval(script);
            if (!result.ok()) {
                Exception ex = result.getException();
                throw new MongoDataException("An error occurred while running script at " + scriptPath.getURI().toString(), ex);
            }
            logger.info("Mongo script at {} executed successfully", scriptPath.getDescription());
        }
    } catch (IOException ex) {
        logger.error("Unable to read files from {}", ex);
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) FileSystemResource(org.springframework.core.io.FileSystemResource) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) CommandResult(com.mongodb.CommandResult)

Aggregations

CommandResult (com.mongodb.CommandResult)16 BasicDBObject (com.mongodb.BasicDBObject)7 DB (com.mongodb.DB)5 DBCollection (com.mongodb.DBCollection)4 DBObject (com.mongodb.DBObject)3 MongoException (com.mongodb.MongoException)3 AggregationOutput (com.mongodb.AggregationOutput)2 BasicDBList (com.mongodb.BasicDBList)2 MongoClientURI (com.mongodb.MongoClientURI)2 File (java.io.File)2 List (java.util.List)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 DateTime (org.joda.time.DateTime)2 HostAndPort (com.google.common.net.HostAndPort)1 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 DBCursor (com.mongodb.DBCursor)1 EmbedMongoDB (com.mongodb.EmbedMongoDB)1 Mongo (com.mongodb.Mongo)1 MongoClient (com.mongodb.MongoClient)1 ServerAddress (com.mongodb.ServerAddress)1