Feeds:
Posts
Comments

Here is a state of the art report I wrote on SQL and NoSQL, and a way to bring them closer. This is actually the theme of my master thesis, so you should probably get some more posts on this topic in the future.

Hasta.😉

Artigo-MI-STAR

I’ve noticed that if you want to run a cassandra cluster on your own pc, for the purpose of small tests, there is no guide in the wiki to do just that.

Therefore, here is how I’ve done it.

First of you’ll need to create an alias for you network interface:

Mac OS
ifconfig en0 alias 192.168.0.2

Linux
ifconfig eth0:0 192.168.0.2

Here I’ve chosen the en0 (or eth0) interface, but you can choose the one you like, and also the IP address you like.

The first file you’ll have to edit is the conf/cassandra.yaml:

  • Change the commit_log, data and saved_caches directories, so it doesn’t conflict with the ones from the previous “node”
  • Change the rpc_port (used for Thrift or Avro) to one that is free
  • Change the listen_address to the IP of your “fake” interface

Next open the conf/cassandra-env.sh file and change the JMX_PORT.

The last file to edit is the bin/cassandra.in.sh where you’ll need to change all the occurences of $CASSANDRA_HOME to the path of the “node”. For example, if you’re bin directory is in /XXX/YYY/node2/bin, the path is /XXX/YYY/node2.

You can do this to create as many nodes as you want, and then just run them as usual, with bin/cassandra -f

A lot has changed from Cassandra 0.6 to 0.7, and sometimes it is hard to find examples of how things work. I’ll be posting how to’s on some of the most usual operations you might want to perform when using Cassandra, written in Java.

First of you have to establish a connection to the server:

TFramedTransport transport = new TFramedTransport(new TSocket("localhost", 9160));
Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
transport.open();

Here I’m using localhost and the default port for Cassandra, but this can be configured.

One difference to the previous versions of Cassandra is that the connection can be bound to a keyspace, and can be set as so:

client.set_keyspace("Keyspace");

With the connection established you need only the data to insert, now. This data is passed to the server in the form of mutations (org.apache.cassandra.thrift.Mutation).

In this example, I’ll be adding a column to a row in a column family in the predefined keyspace.

List<Mutation> insertion_list = new ArrayList<Mutation>();

Column col_to_add = new Column(ByteBuffer.wrap(("name").getBytes("UTF8")), ByteBuffer.wrap(("value").getBytes("UTF8")),System.currentTimeMillis());

Mutation mut = new Mutation();
mut.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(col_to_add));
insertion_list.add(mut);

Map<String,List<Mutation>> columnFamilyValues = new HashMap<String,List<Mutation>>();
columnFamilyValues.put("columnFamily",insertion_list);

Map<ByteBuffer,<String,List<Mutation>>> rowDefinition = new HashMap<ByteBuffer,<String,List<Mutation>>>();
rowDefinition.put(ByteBuffer.wrap(("key").getBytes("UTF8")), columnFamilyValues);

client.batch_mutate(rowDefinition,ConsistencyLevel.ONE);

The code is pretty much self explaining, apart from some values that can be reconfigured at will, as the encoding of the strings (I’ve used UTF8), and the consistency level of the insertion (I’ve used ONE).

In the case of the consistency levels you should check out Cassandra’s wiki, to better understand it’s usage.

To close the connection to the server it as easy as,

transport.close();

Hope you find this useful. Next I’ll give an example of how to get data from the server, as soon as I have some time.😉

For all of you that use the Mac OS Terminal, you’ve probably felt the frustation of opening a new tab and it opening on the $HOME path, unlike the Linux one’s, that open in the path you were in.

Well, I’ve written a script that kind of solves this problem, and adds some extra functionality that I find really helpfull.

#!/bin/bash

COMMAND=$1

if [ -z "$1" ]
then
  COMMAND="cd $(pwd)"
fi

/usr/bin/osascript 2>/dev/null <<EOF
activate application "Terminal"
tell application "System Events"
  keystroke "t" using {command down}
end tell
tell application "Terminal"
  activate
  do script "$COMMAND" in window 1
end tell     
return       
EOF

First let’s take a look at the applescript part (that’s the part between EOF). Applescript code is very readable, but what it does is to open a new tab in the terminal, and then run the code in the COMMAND variable in that newly open window.

Then, there is that little bit of bash code, that assigns the string passed as an argument to be run or, if none is provided, it changes the directory to the one you were in.

So, you can open a new tab, by calling the script or, and this is the very handy thing for writing other scripts, open a new tab and run code in that tab, by calling the script with the string as an argument.

In my previous post I explained how to create a datepicker with dynamic internationalization. There is one catch though, in different languages the representation of the date can be different, for example, February 5th can be 2/5/2011 in the USA and 5/2/2011 in Portugal. The rails default is the first.

This means that you’ll have to take this into account when using the string that you get from the form where you are using the datepicker. You have two options, change the way the dates are displayed, by altering all the languages javascripts, or parse the string as it gets to the controller.

The latter can be achieved with this piece of code:

 
todo.due_date =  DateTime.strptime(params[:date],"%d/%m/%Y").to_time

Note that I’ve chosen that format for the string, but it can be any format according to the ruby’s Time class.

There is one other problem though, the user can, maliciously or by distraction, insert an invalid date. We can strengthen our code to prevent this, by catching the exception thrown.

begin 
  todo.due_date =  DateTime.strptime(params[:date],"%d/%m/%Y").to_time
rescue ArgumentError
  flash[:error] = t("flash.invalid_date")
  redirect_to somewhere_in_the_app_path
  return
end

So, if the exception occurs, we set the flash error message (using the translation helper), redirect to the appropriate path, and then return, so that it does not complain of having multiple render or redirect calls.

The jQuery UI datepicker is internationalizable, by chosing from one of the languages in the regional array, as such:

$(selector).datepicker($.datepicker.regional['en-GB']);

As is easy to see, this changes the datepicker language to english. In order for any other language, apart from english (which is the default), to work, we need to include a javascript file that defines the strings to be shown.

We can either include all the languages (http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/i18n/jquery-ui-i18n.min.js), or just the ones we need, that can be found here.

So far so good. But what if we want to include only the file we need, according to the system’s locale?

It’s pretty simple, and it prevents a user from having to download files he is not going to use, but just the one for the language he is viewing the site.

First, you’ll have to create a helper that checks the current locale and includes the file accordingly, so it can be called from the views that use the datepicker.

def include_i18n_calendar_javascript
  content_for :head do
    javascript_include_tag case I18n.locale
      when :en then "jquery.ui.datepicker-en-GB.js"
      when :pt then "jquery.ui.datepicker-pt-BR.js"
      else raise ArgumentError, "Locale error"
    end
  end
end

As you can see, this has a case that chooses the file according to the locale, an returns it to the javascript_include_tag helper that generates the HTML for the inclusion of a javascript file and places it in the header with the content_for helper.

Now you only have to call the helper in the view and add some javascript.

var counter = 0;
var locale = "
for(i in $.datepicker.regional){
  if(counter == 1)
  { locale=i; break; }
  counter++;
}

Because the regional array is not exactly an array, but an hash (or an associative array, in javascript terms), we will have to iterate through each of it’s objects. The one we want is the second, that the reason for the break. This object is the string the key in that associative array for the definitions of the locale we want. In the case of the first example, it would be “en-GB”.

Now, we just initialize the datepicker with this variable:

$.datepicker.setDefaults( $.datepicker.regional[ '' ] ); 
$( ".datepicker" ).datepicker($.datepicker.regional[locale]);

And that’s it. Now your datepickers are internationalized in a dynamic way.

PS: Of course you’ll need a textfield that has the HTML class datepicker (or any other you choose).

A sumidade loquaz, perdulária pederasta tergiversou-se rubicundo e taciturno do seu perene engodo. Pois o seu fenecimento fugaz e frugal, sem arroubos, de uma petiz plissada realmente pérfida irrompeu numa balbúrdia ígnea e belicosa de jaez veneta. Ruando, perscrutando lugares recônditos à procura de homizio no âmago desse tão ignóbil como parco instante, permutei admoestas com injúrias, indagando ao facto que encerrou no seu incólume estrangulamento fleumático.