Mongo get db.serverstatus from casbah - mongodb

Is there a way to get the results of
db.serverStatus()
from casbah to handle them?
I specifically need the connections, network, metrics fields.
Thanks

import com.mongodb.casbah.Imports._
//connect to MongoDB, testDB is the name of database
val test = MongoClient()("testDB")
// run serverStatus command
val status = test.command("serverStatus")
// status is an instance of BasicDBObject
//retrieving number of available connections
val availableConnections = status.get("connections").asInstanceOf[BasicDBObject].get("available")

Related

Is it possible writing down to RDS raw sql (PostgreSQL) using AWS/Glue/Spark shell?

I have a Glue/Connection for an RDS/PostgreSQL DB pre-built via CloudFormation, which works fine in a Glue/Scala/Sparkshell via getJDBCSink API to write down a DataFrame to that DB.
But also I need to write down to the same db, plain sql like create index ... or create table ... etc.
How can I forward that sort of statements in the same Glue/Spark shell?
In python, you can provide pg8000 dependency to the spark glue jobs and then run the sql commands by establishing the connection to the RDS using pg8000.
In scala you can directly establish a JDBC connection without the need of any external library as far as driver is concerned, postgres driver is available in aws glue.
You can create connection as
import java.sql.{Connection, DriverManager, ResultSet}
object pgconn extends App {
println("Postgres connector")
classOf[org.postgresql.Driver]
val con_st = "jdbc:postgresql://localhost:5432/DB_NAME?user=DB_USER"
val conn = DriverManager.getConnection(con_str)
try {
val stm = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
val rs = stm.executeQuery("SELECT * from Users")
while(rs.next) {
println(rs.getString("quote"))
}
} finally {
conn.close()
}
}
or follow this blog

Combine MongoDb clients: pyMongo and MongoEngine

In my web application, I use Flask as framework and MongoDB as persistent layer. There are multiple libraries to connect to MongoDB. I am currently using the low-level lib pyMongo. However, I would like to combine it with MongoEngine for some models.
The only approach I see is to create an instance of both clients. This looks a big doggy. Is there a simpler way to combine these libraries (pyMongo, MongoEngine) such that they use the same database (with different collections).
Its currently not possible to use an existing Pymongo client to connect MongoEngine but you can do the opposite; if you connect MongoEngine, you can retrieve its underlying pymongo client or database instances.
from mongoengine import connect, get_db, Document, StringField
conn = connect() # connects to the default "test" database on localhost:27017
print(conn) # pymongo.MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())
db = get_db() # pymongo.Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary()), u'test')
print(db)
class Person(Document):
name = StringField()
coll = Person._get_collection()
print(coll) # pymongo.Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary()), u'test'), u'person')

How to change Pymongo database connection

I do have a database named users but pymongo is looking for a db named as app is there any way to change to Pymongo connection method ?
this one is working if i crate a db named as app;
app = Flask(__name__)
mongo = PyMongo(app)
i want to connect like this but i do get an error for that ;
mongo = PyMongo('users')
I have found my answer;
client = MongoClient('localhost')
db = client['dbname']
collection = db.collection_name
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'dbname'
mongo = PyMongo(app)
This allows you to use flask_pymongo, more info here

MongoLab with ReactiveMongo Authorization Problems

I have a small connection routine which is as below:
val dbName = "myDb"
val user = "myUser"
val pass = "myPass"
val mongoDriver = new reactivemongo.api.MongoDriver()
val db = MyDBObject(
mongoDriver.connection(
Seq("XXXXX.mongolab.com:XXXXX"),
options = MongoConnectionOptions(authMode = ScramSha1Authentication),
authentications = List(Authenticate(dbName, user, pass))
),
dbName
)
I'm using this to create a new connection and then using this connection, I work with my documents. But the problem is that for some very strange reason, I could not get this to work!
Here is the error that I get:
CommandError[code=13, errmsg=not authorized on myDb to execute command {....,
code: BSONInteger(13)
}]
I've been trying to dig into this for almost 3 hours without any vail! The MongoLab uses 3.0 version of MongoDB and I use 0.11.7 version for the ReactiveMongo library.
Using the mongo shell, I'm able to log in to the MongoLab and create new collections with the same set of credentials!

Programmatically enable sharding + choosing shard key on a collection using casbah with Mongo 2.4

I am attempting to programmatically "enable sharding" and set the "shard key" using java/scala API particularly casbah
Our config
scala 2.10
casbah 2.6 - "org.mongodb" % "casbah_2.10" % "2.6.0",
MongoDB 2.4.4
Also what is the casbah driver version for mongo 2.4.4 (with scala 2.10)
Our use case is such that, collections + indexes are created programmatically using the casbah scala API dbConnection.getCollection(....) and collection.ensureIndex(DBObject("orgId" -> 1), DBObject("background" -> true, "name" -> "org_idx", "unique" -> false))
Is there an equivalent casbah API to programmatically, enableSharding and choose shardKey as well as we are currently sharding our mongo cluster to scale out.
Our database + collection names are not known ahead time and a dynamically created using API, so a enabling sharding using mongo shell is simply not an option.
Is there a better way to do this ? ANy recommendations?
Here's what worked for me, building on #Ross's answer, which didn't quite work for me:
import com.mongodb.casbah.Imports._
// Connect to MongoDB
val conn = MongoClient()
val adminDB = conn("admin")
// Enable sharding on the DB
adminDB.command(MongoDBObject("enableSharding" -> <database>))
// Create the index for our shard key
val shardKey = MongoDBObject("_id" -> "hashed")
conn(<database>)(<collection>).ensureIndex(shardKey)
// Enable sharding on the collection
adminDB.command(MongoDBObject("shardCollection" -> "<database>.<collection>",
"key" -> shardkey))
For completeness and to help others - enableSharding is a command (see the enableSharding docs) and you can run any command from casbah by using db.command.
import com.mongodb.casbah.Imports._
// Connect to MongoDB
val conn = MongoClient()
val adminDB = conn("admin")
// Enable sharding
adminDB.command(MongoDBObject("shardCollection" -> "<database>.<collection>", "key" -> <shardkey>))
The part should be a MongoDBObject defining the shardkey.
As Asya mentions this might not be the right solution for your use case but its certainly possible to do pragmatically using casbah.