Created
September 21, 2012 14:51
-
-
Save krdlab/3761928 to your computer and use it in GitHub Desktop.
practice: Database.Cassandra (using cassy)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Database.Cassandra.Basic | |
import System.Environment (getArgs) | |
import Control.Monad.IO.Class (liftIO) | |
import Control.Monad.Instances () | |
import Data.Binary (encode, decode) | |
import qualified Data.ByteString.Lazy.Char8 as BS (putStrLn) | |
keySpace :: KeySpace | |
keySpace = "test_keyspace" | |
columnFamily :: ColumnFamily | |
columnFamily = "user" | |
newServer :: String -> String -> Server | |
newServer h p = (h, read p) | |
main :: IO () | |
main = do | |
[host, port] <- getArgs | |
pool <- createCassandraPool [newServer host port] 1 1 300 keySpace | |
runCas pool $ do | |
insert columnFamily "u999" QUORUM [col "name" "zzz", col "point" $ encode (999 :: Int)] | |
getCol columnFamily "u999" ("name" :: String) QUORUM >>= liftIO . printName | |
getCol columnFamily "u999" ("point" :: String) QUORUM >>= liftIO . printPoint | |
-- XXX pool の開放はどうやるのか? | |
printName :: Maybe Column -> IO () | |
printName c = case c of | |
Just column -> do | |
let n = colVal column | |
BS.putStrLn n | |
Nothing -> putStrLn "not found" | |
printPoint :: Maybe Column -> IO () | |
printPoint c = case c of | |
Just column -> do | |
let p = decode $ colVal column :: Int | |
print p | |
Nothing -> putStrLn "not found" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment