Mnesia Quickstart

5 01 2010

Basic introduction on Mnesia

Mnesia is a RDBMS. But it belongs to the nosql category of RDBMS. Reason being the query language is not sql but Erlang. That makes it very easy to store and retrieve without having to go through Object Relational Mapping. So we can actually call Mnesia an object relational database.

Why and where would one want to Mnesia?

Erlang in general is used to program highly distributed and fault tolerant systems. Even though it has its roots in the telecommunication industry, it has proven useful in several other sites like ejabbered, Facebook chat etc. Mnesia is just a part of Erlang and is built with Erlang.

Hence it gives you configurable degree of Fault-tolerance (by means of replication).

Another important feature of mnesia is the Dirty read interface. It is possible to read, write and search Mnesia tables without protecting the operation inside a transaction.

Quickstart

The below steps should get you started on mnesia, if you are using it for the first time.

Required Software – Erlang

I use ubuntu as my OS. But that should not make it much different on any other OS.

Now lets start with some code. It is useful to install the table-viewer (erlang-tv).

The goal of this exercise is to create table called person, insert few records and read them.

create a file called Person.hrl

-record(person, {name,      %% atomic, unique key
age,        %% age
married_to, %% name of partner or undefined
children }).%% list of children

create a file called Person.erl

-module(person).
-include("<path_to_person.hrl>/person.hrl").
-export([init/0]).
-export([insert/0]).
-export([read/1]).
init() ->
mnesia:create_table(person,[{attributes,record_info(fields,person)}])
.
insert() ->
T = fun() ->
X = #person{name=john,
age=36,
married_to=ana,
children=[josh,kelly,samantha]
},
mnesia:write(X)
end,
mnesia:transaction(T)
.
read(Name) ->
R = fun() ->
mnesia:read(person,Name,write)
end,
mnesia:transaction(R)

Start command line erlang. Type in the below command from the directory which contains the above two files

erl mnesia dir .

The above command conveys that the current directory will be used to store Mnesia files.

Compile the person.erl

>c(person).

{ok,person}

Start mnesia

>mnesia:start().

Create person table.

>person:init().

Insert a record.

>person:insert().

Use table viewer to check if the record has been inserted.

>tv:start().

This will launch table view application. By default the table viewer shows the ETS tables. To look at the table we just created go to view menu and select Mnesia tables.

Read the record using Mnesia:read()

>person:read(klacke).
{atomic,[{person,john,36,ana,[josh,kelly,samantha]}]}

In my next post I will cover Mnesia queries as List Comprehension.

Advertisements

Actions

Information

6 responses

12 04 2010
Peter veentjer

Interesting post. When can we expect the next entry about list comprehension?

12 04 2010
harikrishnan83

Thanks for the comment. List comprehension will appear soon. Trying to come up with good examples.

4 10 2011
Onty

Can i ask, is there a way to switch directories , like to choose the directory containning the 2 files?

5 10 2011
harikrishnan83

I am not an expert in this area. Maybe you can run below command in a different directory to generate the beam file.
erlc person.erl

Person.erl includes Person.hrl file. You can copy the beam file to any directory you need and use it there. Hope this helps.

19 12 2011
anders pearson

Just a couple notes on things in this post that might trip up newbies:

1) The filenames should be all lowercase. ‘person.erl’ and ‘person.hrl’ not ‘Person.erl’ and ‘Person.hrl’ as written.

2) the source listing for person.erl is missing a “.” at the end of the read function so it gives a compile error as-is.

3) the source listing for person.erl inserts a ‘john’ user but the read example retrieves a ‘klacke’ user. That should be “person:read(john).”

None of these are terribly hard to figure out if you’re familiar with Erlang, but I remember how frustrating it can be to read a tutorial on something new, try to follow along and have things not work.

19 12 2011
harikrishnan83

Thanks a lot. Appreciate the comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: