Is it possible to execute raw commands as javascript through the Java driver for MongoDB?
I'm tired of wrapping everything in Java objects using Rhino, and would happily sacrifice performance for the convenience of passing javascript directly through to the DB.
If not, I can always use sleepymongoose or something, but I don't really want to add yet another language (python) to the stack at this point.
Any insights are appreciated.
actually no. This command(String) can run any kind of mongo database commands, not arbitrary javascript. For the latter you would need DB.eval() which would block your whole DB unless you are using 1.7.2 mongo or later and the noblock option set.
references:
http://api.mongodb.org/java/2.4-rc0/index.html
http://mongodb.onconfluence.com/display/DOCS/List+of+Database+Commands
There are 2 DB.command() methods in the mongo java driver. One of the 2 takes a String. I think this is what you are looking for.
See here
Related
I need to implement a store procedure in an Oracle database that will do the following:
Read an external file that needs to be processed (extract data from file and validate)
Call another store procedure in the database in charge to validate/insert the data.
Manage exceptions.
Write to another file with the results of the operations executed.
I know I can do all these things with PL/SQL or Java (store procedure), but which will be more efficient/faster or better? most of the operations are reading/writing a file, and the database operations are done in a store procedure already.
I have read other posts about PL/SQL vs Java (like this and this) but none talks about this.
I would never want to use a SQL dialect, no matter how versatile it may be, to do anything outside the DB. I would do what you're trying to do preferably in a shell or perl script, to use the lowest common denominator, although Java is OK, just perhaps a bit too sophisticated for a job this simple. But if Java is all you have or know how to use, then go for it.
So I have a lot of .js files that I have used to query MongoDB from the Command Line Interface before, but now I want to be able to run those same queries through Java (I am using Java to back a web interface that relies on the information from the query). How can I use those JavaScript queries from the Java driver and return some data that I can work with (the end game is to format the result into HTML, if that helps).
If you need to executye your js files during buildtime, you can use maven-mongodb-plugin. This plugin uses db.eval()...
using Java IO to read your js files, filter the queries , execute the queries.
This not possible in an efficient way in current JAVA driver.
Have a look here :
mongodb java driver - raw command?
I have a Java utility for database imports. I'd like to be able to use sqlldr for performance on oracle. I could create the control and data files, but that doesn't seem like The Right Thing™ to do. I should be able to stream the data by providing INFILE "-" in the control file (q1 - how? from command line, I can pipe "echo <data...>" to the sqlldr, but there must be a way to just stream the string into the input stream for the process? never used Java for this before). I can't see how to stream the control file itself (q2 - or am I missing something obvious?). I could use named pipes, but I have no idea how to instantiate and use them from Java in windows (q3 - would that work and how?).
<moan>why must oracle be so complicated? it was trivial in mysql...<moan>
"why must oracle be so complicated? it
was trivial in mysql"
What you must remember is, Oracle is a venerable product. SQL Loader as a utility must be twenty years old, maybe more. So naturally it is harder to work with than some newer tools.
And that is why you should stop trying to fit SQL Loader into your new-fangled Java app :-) Look at external tables instead. Because these are database objects we can use SQL SELECTs against them, so it's a whole easier to automate load processes with them. I wrote a bit more about external tables in my answer to another question. Check it out.
Fundamentally SQLLDR is about getting data from one or more files into a database table. It is powerful in that role, especially when dealing with multiple files or parallel loads from a single file (it can have multiple threads/processes reading from the same file at the same time).
Not all of these fit well with reading from something that isn't a real file. If your data stream is coming from a web service, then I'd pull it using UTL_HTTP. If it is coming from FTP, then I'd FTP straight into the database as a CLOB/BLOB and process it from there.
Depending on your version, also look at the preprocessor capabilities of external tables
my current java code is deployed to the DB and the plsql code calls it and uses it.
I need to get the Java code out of the database and still be able to use it.
The options I had in mind are:
Web services
Java Stored Procedures
Calling OS command using pl/sql
RMI
What is your recommendation?
please add cons and pros.
thanks,
Leeran
my current java code is deployed to
the DB and the plsql code calls it and
uses it. I need to get the Java code
out of the database and still be able
to use it.
Can you clarify what "get the Java code out of the database" means to you? Is your intent to package it in a JAR and remove it from the database entirely? It's not clear to me whether or not you wish to move this operation to the middle tier and not have PL/SQL call it anymore.
Here are my thoughts on your proposed choices:
Web services - moves the operation out of the database and onto the middle tier.
Java Stored Procedures - I don't understand this. If it's in the database now, how does this change anything? You can certainly have any client call that PL/SQL, which in turn will invoke your Java code. It's a question of whether it's more efficient to perform that operation on the middle tier or on the database server.
Calling OS command using pl/sql - I don't understand this at all.
RMI - just another remoting choice, just like web services. If you encapsulate the operation as a Java POJO service you can remote the code any way you wish. This means only Java clients. A web service can accept a request from any client that can speak HTTP, including non-Java clients.
It isn't completely clear what you want to do or why. If you just want to re-use some code, as part of your build procedure, fetch the java from the database so it can be compiled and included in your jar/war file.
during a lecture my professor gave examples of several actions involving databases and the java.sql package. These examples were supposed to be uploaded online in a pdf file, but for some reason the names of all functions and class names aren't displaying with my pdf reader.
I would like to know the equilavents of the following PHP functions in Java:
mysql_connect
mysql_query
mysql_fetch_row
mysql_fetch_assoc
mysql_close
Thanks!
If you consult the Java API docs appropriate for the version you're using (I'm using JDK 1.5, so it's http://java.sun.com/j2se/1.5.0/docs/api/) and click on java.sql, you can see all the classes for Java JDBC access.
Basically, you create a new Connection to a database with DriverManager, and do a query with Connection.prepareStatement, PreparedStatement.execute() and PreparedStatement.executeQuery() and loop through the resultant ResultSet with ResultSet.next() and pull the results out with ResultSet.getXXXXX.
If you're just getting started with JDBC, consider working your way through Sun's tutorial at: http://java.sun.com/docs/books/tutorial/jdbc/basics/
Working directly with JDBC (java.sql) is verbose and error-prone, especially for beginners, because you need to manually do very repetitive steps, and "finally" close so many database objects (Connections, Statements, ResultSets).
If you do not mind pulling in an extra dependency, Apache Commons have a nice little wrapper package called DbUtils that makes it easy to run queries and updates (while still staying at the SQL level, as opposed to object-relational mappers that go to a higher level of abstraction).