{"id":2648,"date":"2011-01-17T00:36:54","date_gmt":"2011-01-16T19:06:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2648"},"modified":"2011-01-17T15:11:56","modified_gmt":"2011-01-17T09:41:56","slug":"getting-up-and-running-with-cassandra","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-up-and-running-with-cassandra\/","title":{"rendered":"Getting up and Running With Cassandra"},"content":{"rendered":"<p>Luckily, I got some time outside my usual obligations in the project, to learn something new and I devoted the time to getting up and running with <a href=\"http:\/\/wiki.apache.org\/cassandra\/\" target=\"_blank\">cassandra<\/a> . Getting starting up with it was a bit bumpy, as the case almost always is when you start with something entirely new.<\/p>\n<p>Lets get Started..<\/p>\n<p>1) First step of course is to get the binary files for the cassandra from <a href=\"http:\/\/cassandra.apache.org\/\" target=\"_blank\">here<\/a><\/p>\n<p>2) It is assumed that you have got jdk&gt;1.5 installed on your machine and JAVA_HOME variable is set. Now extract the tar file to the location you want (I extracted in the \/opt folder and created a soft link to it with name cassandra).<\/p>\n<p>3) Create the necessary directories for cassandra in \/var\/lib and \/var\/log folders and change their ownership<\/p>\n<p>[java]<\/p>\n<p>sudo mkdir -p \/var\/lib\/cassandra<\/p>\n<p>sudo chown -R &quot;user&quot; \/var\/lib\/cassandra<\/p>\n<p>sudo mkdir -p \/var\/log\/cassandra<\/p>\n<p>sudo chown -R &quot;user&quot; \/var\/log\/cassandra<\/p>\n<p>[\/java]<\/p>\n<p>Now move the extracted cassandra project move to the bin folder inside it (in my case \/opt\/cassandra\/bin) and type the command<\/p>\n<p>[java]<\/p>\n<p>.\/cassandra\u00a0 -f<\/p>\n<p>[\/java]<\/p>\n<p>The -f switch will ensure that cassandra runs in the foreground and its logs will print to standard output.<\/p>\n<p>Now a few lines similar to a java stack trace will appear. I expected to see some kind of &#8220;successful or server running message&#8221; but there was none. So if you are not seeing any FATAL or ERROR messages it means you have succeeded in running cassandra. \ud83d\ude42<\/p>\n<p>Now notice in that stack trace that your cassandra runs on port 9160 by default.<\/p>\n<p>So to connect to cassandra open a new terminal and start the cassandra CLI available in the same bin folder.<\/p>\n<p>[java]<\/p>\n<p>.\/cassandra-cli<\/p>\n<p>[\/java]<\/p>\n<p>You will see a message like<\/p>\n<p>Welcome to cassandra CLI.<\/p>\n<p>Type &#8216;help;&#8217; or &#8216;?&#8217; for help. Type &#8216;quit;&#8217; or &#8216;exit;&#8217; to quit.<br \/>\n[default@unknown]<\/p>\n<p>typing help here will show you a list of commands available.<\/p>\n<p>Now, we will connect to the cassandra server instance we started earlier.<\/p>\n<p>[java]<\/p>\n<p>connect localhost\/9160;<\/p>\n<p>[\/java]<\/p>\n<p>Don&#8217;t miss that semicolon ;). All statements here must end with semicolon.<\/p>\n<p>You will now be connected to a &#8220;Test Cluster&#8221;. It is the default cluster which comes with cassandra. Cluster is a container which encapsulates many &#8216;keyspaces&#8217; and keyspaces are things similar to a database in relational DBMS. I better leave the data model topic here and concentrate on task at hand. I intend to take up data model topic in a separate blog.<\/p>\n<p>So, Now we have a cluster to work in.<\/p>\n<p>We will create a keyspace (database in relational DBMS world) and then enter and retrieve some data from it.<\/p>\n<p>First of all lets see what all keyspaces are already available. Invoke<\/p>\n<p>[java]<\/p>\n<p>show keyspaces;<\/p>\n<p>[\/java]<\/p>\n<p>The\u00a0 keyspaces you see are used by cassandra and are not to be intended to be used by the us. So lets make our own keyspace.<\/p>\n<p>[java]<\/p>\n<p>create keyspace CustomKeySpace with replication_factor=1;<\/p>\n<p>use CustomKeySpace;<\/p>\n<p>[\/java]<\/p>\n<p>What we have just done is created a new keyspace and started using in. (Quite similar to create database &lt;database-name&gt;; and use &lt;database-name&gt;;) Lets leave replication_factor for now.<\/p>\n<p>[default@unknown]<br \/>\n[default@unknown] use CustomKeySpace;<br \/>\nAuthenticated to keyspace: CustomKeySpace<br \/>\n[default@CustomKeySpace]<\/p>\n<p>notice the change from [default@unknown] to [default@CustomKeySpace]. It shows you are a &#8220;default&#8221; user which earlier was not using a keyspace and is now using CustomKeySpace. Another way to look at it is default user is &#8220;logged&#8221; into CustomKeySpace.<\/p>\n<p>Next step is to create a table, just that it is called a column-family here.<\/p>\n<p>[java]<\/p>\n<p>create column_family user;<\/p>\n<p>[\/java]<\/p>\n<p>lets enter some data to this column family now.<\/p>\n<p>[java]<\/p>\n<p>[default@CustomKeySpace] set user [&#8216;sachin&#8217;] [&#8216;lname&#8217;]= &#8216;Anand&#8217; ;<br \/>\nValue inserted.<br \/>\n[default@CustomKeySpace] set user [&#8216;sachin&#8217;] [&#8217;email&#8217;]= &#8216;sachin[at]intelligrape[dot]com&#8217; ;<\/p>\n<p>Value inserted.<\/p>\n<p>[\/java]<\/p>\n<p>Now we created two columns for user &#8216;sachin&#8217; one is called [&#8216;lname&#8217;] and contains value &#8216;Anand&#8217;, other is called &#8217;email&#8217; and contains value sachin[at]intelligrape[dot]com.<\/p>\n<p>To count the number of columns for a record<\/p>\n<p>[java]<\/p>\n<p>count user [&#8216;sachin&#8217;];<\/p>\n<p>[\/java]<\/p>\n<p>To retrieve the values from the database &#8212; you guessed it we will use get.<\/p>\n<p>[java]<\/p>\n<p>[default@CustomKeySpace] get user [&#8216;sachin&#8217;];<br \/>\n=&gt; (column=656d61696c, value=73616368696e40696e74656c6c6967726170652e636f6d, timestamp=1295199962515000)<br \/>\n=&gt; (column=[java]666e616d65, value=4562656e, timestamp=1295199873677000)<br \/>\nReturned 2 results.<br \/>\n[\/java]<\/p>\n<p>problem here column names and values are coming in hex code here. So, we need to add some metadata to tell what kind of values we are expecting. here we go..<\/p>\n<p>[java]<\/p>\n<p>[default@CustomKeySpace] update column family user with column_metadata=[{column_name:lname, validation_class:UTF8Type},{column_name:email, validation_class:UTF8Type}];<\/p>\n<p>[\/java]<\/p>\n<p>Again writing get user command<\/p>\n<p>[java]<\/p>\n<p>[default@MyKeySpace] get user [&#8216;sachin&#8217;];<br \/>\n=&gt; (column=656d61696c, value=sachin@intelligrape.com, timestamp=1295199962515000)<br \/>\n=&gt; (column=666e616d65, value=Anand, timestamp=1295199873677000)<br \/>\nReturned 2 results.<br \/>\n[\/java]<\/p>\n<p>So we have got the results we wanted. So in this small article we learnt how to start cassandra connect to it. create a new keyspace add a column_family and set and get data from it. I will be back with more on this for sure.<\/p>\n<p>Thanks &amp; Regards.<\/p>\n<p>Sachin Anand<\/p>\n<p>Email\u00a0 : sachin[at]intelligrape[dot]com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Luckily, I got some time outside my usual obligations in the project, to learn something new and I devoted the time to getting up and running with cassandra . Getting starting up with it was a bit bumpy, as the case almost always is when you start with something entirely new. Lets get Started.. 1) [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[501,502],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2648"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2648"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2648\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}