本文介绍了Mongodb:用Java(和JavaScript)调用db.printShardingStatus()/ sh.status()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java代码内部进行分片后得到一个块列表。我的代码很简单,看起来像这样:

I need to get a list of chunks after sharding inside my Java code. My code is simple and looks like this:

Mongo m = new Mongo( "localhost" , 27017 );

DB db = m.getDB( "admin" );

Object cr = db.eval("db.printShardingStatus()", 1);

调用eval()会返回错误:

A call of eval() returns an error:

Exception in thread "main" com.mongodb.CommandResult$CommandFailure: command failed [$eval]: { "serverUsed" : "localhost/127.0.0.1:27017" , "errno" : -3.0 , "errmsg" : "invoke failed: JS Error: ReferenceError: printShardingStatus is not defined src/mongo/shell/db.js:891" , "ok" : 0.0}
    at com.mongodb.CommandResult.getException(CommandResult.java:88)
    at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
    at com.mongodb.DB.eval(DB.java:340)
    at org.sm.mongodb.MongoTest.main(MongoTest.java:35)

实际上,如果我们查看db.js的代码,在第891行中调用了一个未在文件中定义的方法printShardingStatus()。在utils_sh.js文件中的sh.status()方法内部,甚至还有一条注释:

And, really, if we look into the code of db.js, in line 891 there is a call to a method printShardingStatus() that is not defined inside a file. Inside of sh.status() method in utils_sh.js file, there is even a comment:

重要的是,当我在mongo命令行中运行这些命令时,一切正常!

Important to mention, when I run these commands in mongo command line, everything works properly!

我的问题是:


  • 是否还有其他可能在Java代码中获得完整的分片状态? (例如,使用DB.command()方法)

  • 如果没有,还有其他建议如何避免我的问题吗?

推荐答案

许多shell的辅助函数不能用于服务器端代码执行。在 printShardingStatus()的情况下,它是有意义的,因为没有用于打印输出的控制台,你宁愿返回一个字符串。值得庆幸的是,您应该能够提取shell函数的源代码并在应用程序中重新实现它(例如,连接返回的字符串而不是直接打印)。

Many of the shell's helper functions are not available for server-side code execution. In the case of printShardingStatus(), it makes sense because there isn't a console to use for printing output and you'd rather have a string returned. Thankfully, you should be able to pull up the source of the shell function and reimplement it in your application (e.g. concatenating a returned string instead of printing directly).

$ mongo
MongoDB shell version: 2.2.0
connecting to: test
> db.printShardingStatus
function (verbose) {
    printShardingStatus(this.getSiblingDB("config"), verbose);
}

那么,让我们看一下 printShardingStatus() function ...

So, let's look at the printShardingStatus() function...

> printShardingStatus
function (configDB, verbose) {
    if (configDB === undefined) {
        configDB = db.getSisterDB("config");
    }
    var version = configDB.getCollection("version").findOne();

    // ...
}

在全部转将输出语句转换为字符串连接,您需要确保其他DB方法都可供您使用。在性能方面,我认为最好的选择是将此函数的内部移植到Java并完全避免服务器端的JS评估。如果你深入研究 printShardingStatus()函数,你会发现它只是在配置上发出 find()数据库以及一些 group()查询。

Before turning all of the output statements into string concatenation, you'd want to make sure the other DB methods are all available to you. Performance-wise, I think the best option is to port the innards of this function to Java and avoid server-side JS evaluation altogether. If you dive deeper into the printShardingStatus() function, you'll see it's just issuing find() on the config database along with some group() queries.

如果你确实想坚持评估JS而不愿意将此代码保存在Java应用程序中,您还可以查看。

If you do want to stick with evaluating JS and would rather not keep this code within your Java application, you can also look into storing JS functions server-side.

这篇关于Mongodb:用Java(和JavaScript)调用db.printShardingStatus()/ sh.status()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:00