- •Credits
- •About the Authors
- •About the Reviewers
- •www.PacktPub.com
- •Table of Contents
- •Preface
- •Introduction
- •Installing Groovy on Windows
- •Installing Groovy on Linux and OS X
- •Executing Groovy code from the command line
- •Using Groovy as a command-line text file editor
- •Running Groovy with invokedynamic support
- •Building Groovy from source
- •Managing multiple Groovy installations on Linux
- •Using groovysh to try out Groovy commands
- •Starting groovyConsole to execute Groovy snippets
- •Configuring Groovy in Eclipse
- •Configuring Groovy in IntelliJ IDEA
- •Introduction
- •Using Java classes from Groovy
- •Embedding Groovy into Java
- •Compiling Groovy code
- •Generating documentation for Groovy code
- •Introduction
- •Searching strings with regular expressions
- •Writing less verbose Java Beans with Groovy Beans
- •Inheriting constructors in Groovy classes
- •Defining code as data in Groovy
- •Defining data structures as code in Groovy
- •Implementing multiple inheritance in Groovy
- •Defining type-checking rules for dynamic code
- •Adding automatic logging to Groovy classes
- •Introduction
- •Reading from a file
- •Reading a text file line by line
- •Processing every word in a text file
- •Writing to a file
- •Replacing tabs with spaces in a text file
- •Deleting a file or directory
- •Walking through a directory recursively
- •Searching for files
- •Changing file attributes on Windows
- •Reading data from a ZIP file
- •Reading an Excel file
- •Extracting data from a PDF
- •Introduction
- •Reading XML using XmlSlurper
- •Reading XML using XmlParser
- •Reading XML content with namespaces
- •Searching in XML with GPath
- •Searching in XML with XPath
- •Constructing XML content
- •Modifying XML content
- •Sorting XML nodes
- •Serializing Groovy Beans to XML
- •Introduction
- •Parsing JSON messages with JsonSlurper
- •Constructing JSON messages with JsonBuilder
- •Modifying JSON messages
- •Validating JSON messages
- •Converting JSON message to XML
- •Converting JSON message to Groovy Bean
- •Using JSON to configure your scripts
- •Introduction
- •Creating a database table
- •Connecting to an SQL database
- •Modifying data in an SQL database
- •Calling a stored procedure
- •Reading BLOB/CLOB from a database
- •Building a simple ORM framework
- •Using Groovy to access Redis
- •Using Groovy to access MongoDB
- •Using Groovy to access Apache Cassandra
- •Introduction
- •Downloading content from the Internet
- •Executing an HTTP GET request
- •Executing an HTTP POST request
- •Constructing and modifying complex URLs
- •Issuing a REST request and parsing a response
- •Issuing a SOAP request and parsing a response
- •Consuming RSS and Atom feeds
- •Using basic authentication for web service security
- •Using OAuth for web service security
- •Introduction
- •Querying methods and properties
- •Dynamically extending classes with new methods
- •Overriding methods dynamically
- •Adding performance logging to methods
- •Adding transparent imports to a script
- •DSL for executing commands over SSH
- •DSL for generating reports from logfiles
- •Introduction
- •Processing collections concurrently
- •Downloading files concurrently
- •Splitting a large task into smaller parallel jobs
- •Running tasks in parallel and asynchronously
- •Using actors to build message-based concurrency
- •Using STM to atomically update fields
- •Using dataflow variables for lazy evaluation
- •Index
Working with Databases in Groovy
The Jedis multi method returns a Transaction object on which the commands have to be executed. This is an example of the actual Java code:
Transaction t = jedis.multi(); t.set("foo", "bar");
Response<String> result1 = t.get("foo");
Thanks to Groovy closures and the Closure's delegate property we can invoke the atomic function as follows:
def res = atomic { incr ('foo') incr ('bar')
}
println jedis.get('foo') assert '1' == jedis.get('bar') assert res == [1, 1]
The two INCR commands are executed atomically, and the res variable contains the result of last command.
See also
ff http://redis.io
ff http://redis.io/topics/quickstart ff https://github.com/xetorthio/jedis ff http://www.grails.org/plugin/redis
Using Groovy to access MongoDB
MongoDB (http://www.mongodb.org) is a document-oriented database written in C++ with RDBMS-like features such as indexing and replication. It is developed and supported by 10gen.
MongoDB is very popular mainly for its simplicity: documents are created as JSON-like records (key/value pairs with a rich data type model) and the interface is simple enough to be used directly from JavaScript. It has been designed for scalability in mind. Its document-oriented data model allows it to automatically split up data across multiple servers, letting developers focus on application logic instead of scaling up the data store.
Other very useful features of MongoDB are built-in support for Map/Reduce-style aggregation and geospatial indexes.
This recipe will show you how to execute CRUD-like operations on a MongoDB instance and how to search for data.
252
www.it-ebooks.info
Chapter 7
Getting ready
MongoDB installation is quite simple, especially if you are on Linux or OS X, where you can use a package manager such as Yum or Homebrew. Detailed installation instructions are available for every operating system on the MongoDB website (http://docs.mongodb.
org/manual/installation).
Before we get into the details of this recipe, we will give a quick crash course on the main MongoDB's entities.
MongoDB has databases, collections, and documents. MongoDB stores a database in its own file. A database has specific permissions and can be conceptually mapped to a relational database.
A collection is a group of documents stored in a database. A collection has no schema, meaning that each document in a collection is free to have any kind of structure (it is recommended to keep similar documents in the same collection).
Finally, the document is an ordered set of key/value pairs. Each programming language has a data structure to represent a key/pair values, such as maps or dictionaries.
This is a Javascript representation of a MongoDB document.
{ "name": "Oscar", "lastName": "Wilde" }
The Java driver (https://github.com/mongodb/mongo-java-driver/downloads) is the oldest MongoDB driver. It is very stable and a popular choice for enterprise developers. The driver is enough to get started, but as it often happens with Java libraries, it is very verbose. The verbosity is quite evident when building documents to be sent to the server.
Fortunately, there is a Groovy wrapper for the MongoDB Java driver, named GMongo, developed and actively maintained by Paulo Poiati (http://blog.paulopoiati.com/). The wrapper is maintained at the same pace as the main Java driver, making it an excellent option for Groovy developers.
How to do it...
Ensure that your MongoDB instance is running; the server usually listens for connections on port 27017.
1.As usual, it is necessary to import the third-party library that will be used throughout this recipe, GMongo.
@Grab('com.gmongo:gmongo:1.2') import com.gmongo.GMongo
253
www.it-ebooks.info
Working with Databases in Groovy
2.Then, the GMongo class instance is created to access the database groovy-book. The getDB method does actually create a database, if that does not exist.
def mongo = new GMongo()
def db = mongo.getDB('groovy-book')
3.Now, let's insert some documents into a collection named todos.
db.todos.insert([name: 'buy milk']) db.todos.insert(name: 'pay rent') db.todos << [name: 'read document']
The previous snippet inserts three to-do documents into MongoDB. You can notice that the syntax for inserting data is slightly different in each statement. In the first two cases we are actually calling the same method; Groovy allows omitting square brackets ([]) when the method's only parameter is a Map. The last statement is making use of the leftShift method, which under the hood results in the same call to the insert method.
4.So, you can now select a document from the collection using for example the findOne method:
println db.todos.findOne()
The output will be as follows:
> [_id:50b299e81a88da0e521db954, name:buy milk]
How it works...
The findOne method with no arguments returns the first document in the collection.
MongoDB automatically adds an id, named ObjectID, to a new document. The ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter.
findOne can be also used with an argument, very much like an SQL SELECT statement.
println db.todos.findOne(name: 'pay rent')
The output will be as follows:
> [_id:50b29b091a88d361473e6b46, name:pay rent]
The find method allows searching for documents using regular expressions. Groovy supports regular expressions natively with the help of the ~ operator, which creates a compiled Pattern object from the given string:
db.todos.find([name: ~/^pay/]).each { println it
}
254
www.it-ebooks.info
Chapter 7
The output will be as follows:
> [_id:50b29b091a88d361473e6b46, name:pay rent]
Updating documents is also as easy as inserting them. A MongoDB collection is schemaless, so each document can have its own shape.
db.todos.update( name: 'pay rent',
[$set: [name: 'pay December rent']]
)
assert db.todos.count(
name: 'pay December rent') == 1
The update method changes the value of the name field using the $set operator. This operator appends the new field or fields to the document or replaces the existing value of the field(s) in case they are already present in the document.
db.todos.update(name: 'pay December rent', [$set:[priority:1]])
The second update example adds a new attribute to the document, the priority field. Please note that the value of priority is an Integer and not a String. MongoDB supports several data types internally through the BSON format, which includes dates and binary.
Another way to update a document is the following:
def todo = db.todos.findOne(name: 'buy milk') todo.priority = 2
db.todos.save todo println todo
The output will be as follows:
> [_id:50b2a3aa1a8848c02c604025, name:buy milk, priority:2]
The syntax to remove documents is very similar to the one for updating.
db.todos.insert([name: 'call John', priority: 2]) assert db.todos.count(priority: 2) == 2
db.todos.remove(priority: 2, name: 'call John') assert db.todos.count(priority: 2) == 1
255
www.it-ebooks.info