Browsing the archives for the Examples category.

MongoDB Wildcard Query

Examples

icon

MongoDB uses regex in certain parts of their query syntax. The following is an example of how to perform a wildcard query:

db.collectionName.find( { fieldName : /.*cnn.*/ } );

This example searches the collection for all documents with a field that contains, “%cnn%”. Of course in your query you need to replace “collectionName” with the name of your collection and “fieldName” with the field you are interested in.

1 Comment

Create Amazon EC2 AMI

Examples

aws

Creating an Amazon EC2 AMI from an existing image seems like a challenging process however, it is actually fairly simple. Before looking at the steps to create the image, first have the following AWS information/files handy:

  • Access key
  • Secret Key
  • Account Number
  • Private key (e.g., pk-37BKCPZ2AVHVSYBK2WZXUQ9D4GQZEB4Q.pem)
  • Certificate (e.g., cert-38BKCPZ1BOHVSYBK4WZXUQ4D4GQZEB2Q.pem)

Next, login to your instance and make sure you have the Amazon AMI and EC2 API tools installed and available in your PATH. Also, upload your private key and certificate to the server.

Now you are ready to create your image file. First create a directory to store this image in.

E.g.

mkdir /mnt/ami

The actual AMI bundle is created by running:

ec2-bundle-vol -d /mnt/ami -k YOUR_PRIVATE_KEY -c YOUR_CERTIFICATE -u YOUR_ACCOUNT_NUMBER

note: The account number should not contain any dashes (‘-’).

Creating the bundle takes a few minutes. After the bundle has been created, it is time to upload it to S3. This is done by running:

ec2-upload-bundle -b YOUR_BUNDLE_NAME -m /mnt/ami/image.manifest.xml -a YOUR_ACCESS_KEY -s YOUR_SECRET_KEY

This walks through the files generated by the bundle command and inserts them into S3.

After your files have been uploaded, it is time to register the AMI using the following command:

ec2-register YOUR_BUNDLE_NAME/image.manifest.xml

The result of running this command is your AMI id.

E.g.

ami-323FEA

Finally, your AMI has been created, uploaded and registered and you are able to launch a new instance of your AMI.

If you need more help, there is a good video available from an Amazon employee. Additionally, take a look at the Amazon docs on the subject.

No Comments

MongoDB – Find Documents Missing Fields

Examples

icon

In MongoDB it is possible that some documents will not contain fields other documents have. The following code shows how to find these documents using the mongo shell.

db.collectionName.find("function() { return this.fieldName == null }")
db.collectionName.find("function() { return this.fieldName == null }").count()

The second example shows how to only get the count.

In this example. you would replace “collectionName” with the name of the collection you are interested in querying. Additionally, you would change “fieldName” to the name of the field that interests you.

No Comments

Bash – Write A Sequential For Loop

Examples

bashterm

A sequential for loop in bash script is accomplished in the following example:

for i in {1..100}
do
    # Do something here
    echo "${i}"
done

This example iterates from 1 to 100 (inclusive) and prints the index number.

Note: This only works in modern versions of bash (3.x+).

No Comments

OS X – Empty Trash Command Line

Examples

apple-logo

It is possible to empty the trash in OS X from the command line by running the following command in the terminal:

rm -Rf ~/.Trash/*

This command permanently removes all the files you have placed in your OS X trash bin.

2 Comments

Bash Find On A Large Dataset – Write The Results To A File

Examples

A fast bash based solution for executing a find on a directory with a lot of files that you want to log to a file:

find /my/data/  -name '*.xml' -type f -fprint my_xml_files.txt

This example looks in the /my/data/ directory for all files with the xml extension and then writes the results to the my_xml_files.txt file.

No Comments

Hibernate Wildcard Query

Examples

hibernate_icon

Creating a wildcard query in Hibernate (HQL) is similar to how SQL handles the same funciton. The query is accomplished by using the, like keyword.

The example below searches for people with a name that contains Mik – an obvious match would be Mike.

Query query
= getEntityManager().createQuery("SELECT p FROM Person p WHERE p.name like :name");
 query.setParameter("name", 'Mik");
 query.getResultList(); // Returns a list of People.
No Comments

Java – Not A Number Gotcha

Examples

java

There are always subtle nuances to every computer language. Recently, I found the following NaN (not a number) nuance in Java:

Bad:

if (value == Double.NaN)
    // Do something...

Good:

if (Double.isNaN(value))

    // Do something....
2 Comments

MySQL and Negative Infinity

Examples

mysql

Storing and retrieving negative infinity in a MySQL database is accomplished by inserting an arbitrarily large negative number.  Negative infinity is handy when storing default values in a database.

The following SQL statement is an example of inserting negative infinity:

INSERT INTO test(negative_infinity) VALUES('-1e500');

Here is the test table:

CREATE TABLE test (
     negative_infinity DOUBLE NOT NULL
) ENGINE=INNODB character set utf8;

The value in the database is stored as the maximum negative value for the data type, double (-1.79769313486232e+308).

mysql> select * from test;
+------------------------+
| negative_infinity      |
+------------------------+
| -1.79769313486232e+308 |
+------------------------+
1 row in set (0.00 sec)

In Java, the Double contsant for max infinity, directly correlates to the value stored by MySQL.

No Comments

Python Create XML Document

Examples

python_logo2

Creating an XML document in Python can be accomplished by executing:

from xml.dom import minidom
doc = minidom.Document()
testElem = doc.createElement("test")
testElem.setAttribute('id', '1234')
doc.appendChild(testElem)
print doc.toxml()

The  example above creates an XML document, creates a child element, sets an attribute and then appends the child element to the document.

The XML document in the above example looks like:

<?xml version="1.0" ?><test id="1234"/>
2 Comments
« Older Posts