Converting string to UTF8Type in FluentCassandra

183 Views Asked by At

I am working with FluentCassandra in F# and attempting to convert a string to a UTF8Type in order to use the ExecuteNonQuery method. Has anyone been successful doing this?

Thanks,

Tom

1

There are 1 best solutions below

0
On BEST ANSWER

Thank you Jack P. and Daniel for pointing me in the right direction.

To provide more examples so others can benefit, I am writing a wrapper on top of FluentCassandra in F# to make CRUD functionality much simpler by utilizing the succinctness of F#. I am using Nick Berardi's code as an example for this wrapper:

https://github.com/fluentcassandra/fluentcassandra/blob/master/test/FluentCassandra.Sandbox/Program.cs

For example, if you want to check if a keyspace exists, simply calling the KeySpaceExists(keyspaceName) would allow for checking if a keyspace exists, using CreateKeyspace(keyspaceName) would allow for creation of a keyspace, etc. An example of the library I am creating is here:

namespace Test

open System
open System.Collections.Generic
open System.Configuration
open System.Linq
open System.Text
open System.Windows
open FluentCassandra
open FluentCassandra.Connections
open FluentCassandra.Types
open FluentCassandra.Linq

module Cassandra =
    let GetAppSettings (key : string) = ConfigurationManager.AppSettings.Item(key)

    let KeyspaceExists keyspaceName =
        let server = new Server(GetAppSettings("Server"))
        let db = new CassandraContext(keyspaceName, server)
        let keyspaceNameExists = db.KeyspaceExists(keyspaceName)
        db.Dispose()
        keyspaceNameExists

    let CreateKeyspace keyspaceName = 
        let server = new Server(GetAppSettings("Server"))
        let db = new CassandraContext(keyspaceName, server)
        let schema = new CassandraKeyspaceSchema(Name=keyspaceName)
        let keyspace = new CassandraKeyspace(schema,db)
        if KeyspaceExists(keyspaceName)=false then keyspace.TryCreateSelf()
        db.Dispose()

    let DropKeyspace (keyspaceName : string ) =
        let server = new Server(GetAppSettings("Server"))
        let db = new CassandraContext(keyspaceName, server)
        match db.KeyspaceExists(keyspaceName)=true with
            // value has "ignore" to ignore the string returned from FluentCassandra
            | true -> db.DropKeyspace(keyspaceName) |> ignore
            | _ -> ()
        db.Dispose()

    let ColumnFamilyExists (keyspaceName, columnFamilyName : string) = 
        let server = new Server(GetAppSettings("Server"))
        let db = new CassandraContext(keyspaceName, server)
        let schema = new CassandraKeyspaceSchema(Name=keyspaceName)
        let keyspace = new CassandraKeyspace(schema,db)
        let columnFamilyNameExists = db.ColumnFamilyExists(columnFamilyName)
        db.Dispose()
        columnFamilyNameExists

    let CreateColumnFamily (keyspaceName, columnFamilyName: string) = 
        if ColumnFamilyExists(keyspaceName,columnFamilyName)=false then
            let server = new Server(GetAppSettings("Server"))
            let db = new CassandraContext(keyspaceName, server)
            let schema = new CassandraKeyspaceSchema(Name=keyspaceName)
            let keyspace = new CassandraKeyspace(schema,db)

            if ColumnFamilyExists(keyspaceName,columnFamilyName)=false then
                keyspace.TryCreateColumnFamily(new CassandraColumnFamilySchema(FamilyName = columnFamilyName, KeyValueType = CassandraType.AsciiType, ColumnNameType = CassandraType.IntegerType, DefaultColumnValueType = CassandraType.UTF8Type))

    let ExecuteNonQuery(keyspaceName, query: string) = 
        let server = new Server(GetAppSettings("Server"))
        let db = new CassandraContext(keyspaceName, server)
        let schema = new CassandraKeyspaceSchema(Name=keyspaceName)
        let keyspace = new CassandraKeyspace(schema,db)
        let queryUTF8 = FluentCassandra.Types.UTF8Type.op_Implicit query
        try
            db.ExecuteNonQuery(queryUTF8)
            true
        with
            | _ -> false

This library allows for very easy one line commands to utilize the FluentCassandra functionality. Of course this is just the start and I plan on amending the above library further.

open System
open System.Linq
open System.Collections.Generic
open System.Configuration
open FluentCassandra.Connections
open FluentCassandra.Types
open FluentCassandra.Linq
open Test.Cassandra

[<EntryPoint>]
let main argv =
    CreateKeyspace("test1")
    printfn "%s" (ColumnFamilyExists("test1", "table1").ToString())
    printfn "%s" (KeyspaceExists("test1").ToString())
    CreateColumnFamily("test1","table1")
    printfn "%s" (ColumnFamilyExists("test1", "table1").ToString())
    let result = ExecuteNonQuery("test1", "CREATE TABLE table2 (id bigint primary key, name varchar)")
    printfn "%s" (result.ToString())

    let wait = System.Console.ReadLine()
    0

Specifically with converting the query string to a UTF8Type, Daniel's approach of utilizing UTF8Type.op_Implicit str worked. You can see how I applied it in the ExecuteNonQuery function above. Thanks again for your help!