Mongodb的java driver本身的接口

void createCollection(String collectionName, CreateCollectionOptions createCollectionOptions)

是不支持创建一个sharding collection的,通过此方式创建的collection不支持分片,即此表存放在一个节点上。传统的方式是通过mongodb的shell命令(见之前的文章),如

sh.shardCollection("dbname.tablename", {"_id": "hashed"}) to create a shard table hashed by _id

但是这种方式也有一定的制约,不能通过程序动态的添加表,如果我默认的表不足以支撑业务,就必须通过shell方式创建一个sharding表,之后再插入数据才能满足要求。

通过javadoc的查询,MongoDatabase存在如下接口:

Document runCommand(Bson command)

也就是说,通过此接口可以执行runCommand命令。而runCommand命令是什么呢?通过官方reference(https://docs.mongodb.com/manual/reference/command/shardCollection/)的查询,我们看到如下说明:

Shards a collection to distribute its documents across shards. You must run enableSharding on a database before running the shardCollection command. The shardCollection command must be run against the admin database.

To run shardCollection, use the db.runCommand( { <command> } ) method.

shardCollection has the following form:

{
shardCollection: "<database>.<collection>",
key: <shardkey>,
unique: <boolean>,
numInitialChunks: <integer>,
collation: { locale: "simple" }
}

也就是说,我们可以通过shell命令,执行db.runCommand({shardCollection:"db.collection", key: {"_id": "hashed"}})来执行此命令。

对于java driver而言,我们只要把命令中的json当参数调用runCommand应当就可以实现创建sharding表了。

接下来看java driver的接口,传递参数是一个Bson,之前我们进行查询以及过滤时生成过Bson,不过那些model显然不全,只是为了查询方便Mongodb帮我们建好的,里面并不包含shardCollection。于是去javadoc找到对应的Bson:

http://mongodb.github.io/mongo-java-driver/3.4/javadoc/

里面有

org.bson.conversions

Interface Bson

找到此Interface后看下面有哪些类implement了此接口。

Class BasicDBObject

接下来去此BasicDBObject页面看看怎么用就可以了,具体实现方式为:

lazy val mongoURI = new MongoClientURI(new MongoUserUtils().origin2URI)
lazy val mongo = new MongoClient(mongoURI)
//这里必须是admin,只有admin库才有创建shardingcollection的权限。
lazy val db = mongo.getDatabase("admin")
val bs = new BasicDBObject()
bs.append("shardCollection", "test.testsharding2")
bs.append("key", mapAsJavaMap(Map(("_id", "hashed"))))
println(bs.toJson())
db.runCommand(bs)

  

MongoDB通过JavaDriver执行shell命令,例如创建sharding collection的更多相关文章

  1. python(6)-执行shell命令

    可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.* ...

  2. Android执行shell命令

    一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/bin/cat", &q ...

  3. loadrunner调用plink,远程linux执行shell命令

    loadrunner调用plink,远程linux执行shell命令   脚本: Action() {   char* cmd; cmd = lr_eval_string("C:\\\&qu ...

  4. python 执行shell命令

    1.os模块中的os.system()这个函数来执行shell命令 1 2 3 >>> os.system('ls') anaconda-ks.cfg  install.log  i ...

  5. python2.7执行shell命令

    python学习——python中执行shell命令 2013-10-21 17:44:33 标签:python shell命令 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...

  6. 在 Ruby 中执行 Shell 命令的 6 种方法

    我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...

  7. Android执行shell命令 top ps

    Android执行shell命令 一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/ ...

  8. 第3节 sqoop:7、通过java代码远程连接linux执行shell命令

    数据库的数据同步软件sqoop 数据同步 关系型数据库到大数据平台 任务:sqoop 是批量导入数据太慢,如何做到实时的数据同步 实时的数据同步工具: canal 阿里开源的一个数据库数据实时同步的软 ...

  9. 【Hadoop离线基础总结】通过Java代码执行Shell命令

    通过Java代码执行Shell命令 需求 在实际工作中,总会有些时候需要我们通过java代码通过远程连接去linux服务器上面执行一些shell命令,包括一些集群的状态管理,执行任务,集群的可视化界面 ...

随机推荐

  1. mysql 表相关操作(1)

    查询语句 select * from t_deptselect empno,ename,sal from t_emp select    empno,   sal * 12 as "inco ...

  2. PostgreSQL-UNION与Order by 冲突

    问题描述 union 连接的两个sql 都包含 order SELECT <property1>, <property2> FROM <table1> ORDER ...

  3. 使用svn未响应卡死的几个原因,commit时checkout时

    1.commit 时 很可能是:检索文件内容过多导致,解决:不要在最外层文件夹目录下commit 2.checkout时 很可能是:地址错误

  4. 【vsCode】识别.vue/一键生成.vue模板文件

    1.安装插件Vetur 结果 ---> .vue代码识别彩标 2.配置.vue文件模板,以便快速一键生成格式化 2.1新建代码片段 File->Preferences->User S ...

  5. docker 开启特权模式

    可以在创建容器时通过 --privileged=true 开启特权模式. 创建容器: # docker run -d --name centos7 --privileged=true centos7: ...

  6. 微软宣布全新命令行+脚本工具:PowerShell 7

    DOS 逐渐退出历史舞台后,Windows 一直内置着 CMD 命令行工具,并在 Windows 7 时代升级为更强悍的 PowerShell,不仅可以执行命令行,更可以执行各种高级脚本,还能跨平台. ...

  7. 在centOS7.2上编译gcc4.1.2

    1.下载安装gcc4.1.2安装包  wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.1.2/gcc-4.1.2.tar.bz2 注:其他版本的安装包可以在上级目录寻找到. ...

  8. openstack基础架构

    申明:本文主要观点引用自cloudman:http://blog.51cto.com/cloudman,感谢cloudman的分享. OpenStack主要是通过Nova,Neutron,Glance ...

  9. Min-max theorem

    wiki链接

  10. Codeforces 919 行+列前缀和 树上记忆化搜索(树形DP)

    A B C #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) ...