{"id":3572,"date":"2016-06-06T16:33:47","date_gmt":"2016-06-06T22:33:47","guid":{"rendered":"http:\/\/benincosa.com\/?p=3572"},"modified":"2016-06-06T16:33:47","modified_gmt":"2016-06-06T22:33:47","slug":"cassandra-startup-with-docker-and-golang","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=3572","title":{"rendered":"Cassandra Startup with Docker and Golang"},"content":{"rendered":"<p>I use Docker:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:sh decode:true \">docker pull cassandra\r\nmkdir ucs-cassandra-db\r\ndocker run -d -P \u00a0--name cassandra -v `pwd`\/ucs-cassandra-db\/:\/var\/lib\/cassandra cassandra<\/pre>\n<p>(Notice that using boot2docker or docker machine on virtual box doesn&#8217;t work very well because the directories are created under different user name. \u00a0You have two choices: \u00a0Don&#8217;t mount persistent directories with boot2docker or log into the virtual box guest (docker-machine ssh dev) and launch the container there.)<\/p>\n<p>Make sure its up:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:sh decode:true \">docker inspect cassandra\r\ndocker logs cassandra<\/pre>\n<p>Now lets create a Keyspace (or Database in SQL terms). \u00a0Connect to it by connecting to cassandra where the 9042 port is mapped:<\/p>\n<pre class=\"lang:sh decode:true\">docker ps\r\n\r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\na1ec821d4f59 cassandra \"\/docker-entrypoint.s\" 34 minutes ago Up 34 minutes 0.0.0.0:32772-&gt;7000\/tcp, 0.0.0.0:32771-&gt;7001\/tcp, 0.0.0.0:32770-&gt;7199\/tcp, 0.0.0.0:32769-&gt;9042\/tcp, 0.0.0.0:32768-&gt;9160\/tcp cassandra\r\n<\/pre>\n<p>Here we see that port 9042 is mapped to 32769. \u00a0To connect we run:<\/p>\n<pre class=\"lang:sh decode:true \">cqlsh 192.168.99.100 32769<\/pre>\n<p>Now we can create the keyspace:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:tsql decode:true\">cqlsh&gt; CREATE KEYSPACE ucstechspecs WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '1' };\r\ncqlsh&gt; USE ucstechspecs;\r\ncqlsh&gt; CREATE TABLE fabricInterconnects (partNumber text, name text, attributes map&lt;text, text&gt;, pictures list&lt;text&gt;, PRIMARY KEY (partNumber));<\/pre>\n<p>Now let&#8217;s use it with a Go program.<\/p>\n<pre class=\"\"><code>go get github.com\/gocql\/gocql<\/code>\r\n<\/pre>\n<p>Our code is pretty simple:<\/p>\n<pre class=\"lang:go decode:true\">package main\r\n\r\nimport (\r\n  \"fmt\"\r\n  \"log\"\r\n\r\n  \"github.com\/gocql\/gocql\"\r\n)\r\n\r\nfunc main() {\r\n\r\n  fmt.Printf(\"Test\\n\")\r\n  cluster := gocql.NewCluster(\"192.168.99.100\")\r\n  cluster.Port = 32869\r\n  cluster.ProtoVersion = 4 \r\n  cluster.Keyspace = \"ucstechspecs\"\r\n  session, err := cluster.CreateSession()\r\n  defer session.Close()\r\n  if err != nil {\r\n    fmt.Printf(\"%s\\n\", err)\r\n    return\r\n  }\r\n\r\n  m := make(map[string]string)\r\n  m[\"color\"] = \"red\"\r\n  m[\"car\"] = \"ferrari\"\r\n\r\n  pictures := []string{\"http:\/\/fi.1.org\", \"http:\/\/lmgtfy\"}\r\n\r\n  err = session.Query(`INSERT INTO fabricInterconnects (partNumber, name, attributes, pictures) VALUES (?, ?, ?, ?)`,\r\n    \"UCSFI-61234\", \"UCS FI 6120\", m, pictures).Exec()\r\n\r\n  log.Println(\"Inserted data\")\r\n  if err != nil {\r\n    log.Fatal(err)\r\n  }\r\n\r\n  var model string\r\n  iter := session.Query(`SELECT name FROM fabricInterconnects WHERE partNumber = ? `,\r\n    \"UCSFI-61234\").Iter()\r\n  for iter.Scan(&amp;model) {\r\n    fmt.Println(\"Model: \", model)\r\n  }\r\n  if err := iter.Close(); err != nil {\r\n    log.Fatal(err)\r\n  }\r\n}<\/pre>\n<p>If that worked then you created an entry and go data back. \u00a0You are now off to the races writing golang code to a cassandra database.<\/p>\n<p>TODO:<\/p>\n<ol>\n<li>Scale out to multiple cassandra servers<\/li>\n<li>Show how this works in <a href=\"http:\/\/mantl.io\">mantil.io<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I use Docker: &nbsp; docker pull cassandra mkdir ucs-cassandra-db docker run -d -P \u00a0&#8211;name cassandra -v `pwd`\/ucs-cassandra-db\/:\/var\/lib\/cassandra cassandra (Notice that using boot2docker or docker machine on virtual box doesn&#8217;t work very well because the directories are created under different user name. \u00a0You have two choices: \u00a0Don&#8217;t mount persistent directories with boot2docker or log into the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[212,778,123],"tags":[833,1011,779],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3572"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3572"}],"version-history":[{"count":1,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3572\/revisions"}],"predecessor-version":[{"id":3573,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3572\/revisions\/3573"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}