Copyright | (c) Saurabh Rawat 2016 |
---|---|
License | Apache |
Maintainer | saurabh.rawat90@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
This is a cassandra driver. It currently has:
- Select
- Insert
- Update
- Delete
- Prepared Queries
- Batch Queries
- Automatic Records Conversion
- Collections
The driver gets the list of all nodes in the cluster and load balances amongst them. So you can connect to any one node in the cluster and it will take care of the rest.
Initialize the driver by calling
CQL.init host port retryInterval
for example
CQL.init "127.0.0.1" (PortNumber 9042) (RetryInterval 1000000)
Example:
data Emp = Emp { empID :: Int64, deptID :: Int32, alive :: Bool , id :: UUID, name :: CQLString, salary :: CQLDouble, someList :: CQLList Int32, someSet :: CQLSet CQLDouble, someMap :: CQLMap CQLString CQLString } deriving(Show, Eq) deriveBuildRec ''Emp main :: IO () main = do candle <- CQL.init "127.0.0.1" (PortNumber 9042) (RetryInterval 1000000) res <- flip runReaderT candle $ runExceptT $ do let q = create "keyspace demodb WITH REPLICATION = {'class' :SimpleStrategy
,replication_factor
: 1}" runCQL LOCAL_ONE q --create a table let tableQuery = "TABLE demodb.emp (empID bigint,deptID int,alive boolean,id uuid,name varchar,salary double," ++ "someset setdouble,somelist listint,somemap maptext,PRIMARY KEY (empID, deptID))" let q = create tableQuery runCQL LOCAL_ONE q --execute prepared queries p <- prepare "INSERT INTO demodb.emp (empID,deptID,alive,id,name,salary,somelist,someset,somemap) VALUES (?,?,?,?,?,?,?,?,?)" execCQL LOCAL_ONE p [ put (104::Int64), put (15::Int32), put True, put $ fromJust $ fromString "38d0ceb1-9e3e-427c-bc36-0106398f672b", put $ CQLString "Hot Shot", put $ CQLDouble 100000.0, put ((CQLList [1,2,3,4,5,6]) :: CQLList Int32), put ((CQLSet $ fromList [CQLDouble 0.001, CQLDouble 1000.0]) :: CQLSet CQLDouble), put $ CQLMap $ DMS.fromList [(CQLString "some", CQLString Things)]] -- execute prepared queries and get results p <- prepare "select empID, deptID, alive, id, name, salary, someset, somemap, somelist from demodb.emp where empid = ? and deptid = ?" res <- execCQL LOCAL_ONE p [ put (104::Int64), put (15::Int32)] liftIO $ print $ catMaybes ((fmap fromRow res)::[Maybe Emp]) liftIO $ print (fromCQL (Prelude.head res) (CQLString "salary")::Maybe Double) liftIO $ print (fromCQL (Prelude.head res) (CQLString "name")::Maybe CQLString) --select rows from table let q = select "demodb.emp" and' "deptID" (15::Int32) rows <- runCQL LOCAL_ONE q liftIO $ print $ catMaybes ((fmap fromRow res)::[Maybe Emp]) --batch queries p <- prepare "INSERT INTO demodb.emp (empID, deptID, alive, id, name, salary) VALUES (?, ?, ?, ?, ?, ?)" let q = batch (update "demodb.emp" where' "empID" (104::Int64) # and' "deptID" (15::Int32)) <> batch (update "demodb.emp" where' "empID" (104::Int64) # and' "deptID" (15::Int32)) <> prepBatch p [ put (101::Int64), put (13::Int32), put True, put $ fromJust $ fromString "48d0ceb1-9e3e-427c-bc36-0106398f672b", put $ CQLString "Hot1 Shot1", put $ CQLDouble 10000.0] runBatch q --drop a table let q = drop' "table demodb.emp" runCQL LOCAL_ONE q --drop a keyspace let q = drop' "keyspace demodb" runCQL LOCAL_ONE q print res
- init :: HostName -> PortID -> RetryInterval -> ExceptT ShortStr IO Candle
- data Consistency
- = ANY
- | ONE
- | TWO
- | THREE
- | QUORUM
- | ALL
- | LOCAL_QUORUM
- | EACH_QUORUM
- | SERIAL
- | LOCAL_SERIAL
- | LOCAL_ONE
- data Candle
- newtype RetryInterval = RetryInterval Int
- data Rows
- type Row = Map CQLString (Word16, Maybe Word16, Maybe Word16, Bytes)
- newtype Bytes = Bytes ByteString
- newtype CQLDouble = CQLDouble Double
- newtype CQLString = CQLString ByteString
- newtype CQLMap k v = CQLMap (Map k v)
- newtype CQLSet el = CQLSet (Set el)
- newtype CQLList el = CQLList [el]
- newtype ShortStr = ShortStr ByteString
- newtype LongStr = LongStr ByteString
- deriveBuildRec :: Name -> Q [Dec]
- fromRow :: BuildRec a => Row -> Maybe a
- (#) :: Q -> Q -> Q
- create :: String -> Q
- drop' :: String -> Q
- select :: String -> Q
- limit :: Int -> Q
- update :: String -> Q
- with :: Binary k => String -> k -> Q
- delete :: String -> Q
- where' :: Binary k => String -> k -> Q
- and' :: Binary k => String -> k -> Q
- data Prepared
- runCQL :: Consistency -> Q -> ExceptT ShortStr (ReaderT Candle IO) [Row]
- prepare :: ByteString -> ExceptT ShortStr (ReaderT Candle IO) Prepared
- execCQL :: Consistency -> Prepared -> [Put] -> ExceptT ShortStr (ReaderT Candle IO) [Row]
- data LoggedBatch
- data BatchQuery
- batch :: Q -> LoggedBatch
- prepBatch :: Prepared -> [Put] -> LoggedBatch
- runBatch :: Batchable a => a -> ExceptT ShortStr (ReaderT Candle IO) [Row]
Documentation
init :: HostName -> PortID -> RetryInterval -> ExceptT ShortStr IO Candle Source #
The first function you need to call. It initializes the driver and connects to the cluster. You only need to specify one node from your cluster here. Retryinterval is the interval with which connection to a node will be retried in case of disconnection.
data Consistency Source #
Consistency levels.
newtype RetryInterval Source #
Data Types
CQLList [el] |
Auto derive conversion for record types
DSL for creating queries
Prepare and run queries
runCQL :: Consistency -> Q -> ExceptT ShortStr (ReaderT Candle IO) [Row] Source #
Run a query directly.
prepare :: ByteString -> ExceptT ShortStr (ReaderT Candle IO) Prepared Source #
Prepare a query, returns a prepared query which can be fed to execCQL for execution.
execCQL :: Consistency -> Prepared -> [Put] -> ExceptT ShortStr (ReaderT Candle IO) [Row] Source #
Execute a prepared query.
Batching
data LoggedBatch Source #
data BatchQuery Source #
batch :: Q -> LoggedBatch Source #
Create a simple batch statement, with query string and parameters.