Databases Reference
In-Depth Information
This gets things ready. Next, run the Thrift server and then use a Thrift client to connect to the
server.
You may not need to generate Thrift clients for a NoSQL store like Apache Cassandra that supports
Thrift bindings but may be able to use a language-specifi c client instead. The language-specifi c
client in turn leverages Thrift. The next few sections explore language bindings for a few specifi c
data stores.
Language Bindings for Java
Java is a ubiquitous programming language. It may have lost some of its glory but it certainly hasn't
lost its popularity or pervasiveness. In this section I explain a bit about the Java drivers and libraries
for MongoDB and HBase.
The makers of MongoDB offi cially support a Java driver. You can download and learn more about
the MongoDB Java driver at www.mongodb.org/display/DOCS/Java+Language+Center . It is
distributed as a single JAR fi le and its most recent version is 2.5.2. After you have downloaded the
JAR, just add it to your application classpath and you should be good to go.
A logdata collection was created in a MongoDB instance earlier in this chapter. Use the Java driver
to connect to that database and list out all the elements of that collection. Walk through the code in
Listing 3-2 to see how it's done.
LISTING 3-2: Java program to list all elements of the logdata MongoDB collection
Available for
download on
Wrox.com
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
public class JavaMongoDBClient {
Mongo m;
DB db;
DBCollection coll;
public void init() throws Exception {
m = new Mongo( “localhost” , 27017 );
db = m.getDB( “mydb” );
coll = db.getCollection(“logdata”);
}
public void getLogData() {
DBCursor cur = coll.find();
while(cur.hasNext()) {
System.out.println(cur.next());
}
}
public static void main(String[] args) {
Search WWH ::




Custom Search