I must admit, I didn’t know about Cassandra, other than perhaps a demo of Cassandra and/or MongoDB at a .NET user meeting.  So I decided it was time to give it a try.

  1. Download the tar http://cassandra.apache.org/download/.  This gave me a file called apache-cassandra-3.10-bin.tar.gz on my download folder.  I then created a c:\apache-cassandra-3.10 folder, and unpacked it using Total Commander (from Ghisler).  I knew it could hand tar files.  I’m not sure what other people use.
  2. I open the “Command Prompt”, and navigated to the above folder, then the bin subdirectory, and tried to run “Cassandra.bat”.  This lead to several issues and changes required:
  3. It runs a Powershell program, and if it fails for any reason, the logic falls through to an error about setting the “set-executionpolicy” to unrestricted.  I had done this along time ago on my machine, so this error was quite misleading.
  4. Had to set the environment variable JAVA_HOME to “c:\Program Files (x86)\Java\jre1.8.0_121”.   This might be slightly different on your machine.
  5. Then got  “the heap error”, and made change to configuration file, as shown in the “file compare” below (my change on left, the original on the right):
  6. Then I re-ran “Cassandra.bat” and everything seemed to start.  As a Windows user, you have to know it doesn’t run as a windows service, so you have to leave the command prompt window open the whole time you are using the database.
  7. Being a .NET developer, at first I tried to jump into C#.  From Visual Studio 2013 I ran the NUGET command:
    Install-Package CassandraCSharpDriverThis lead to the error:Install failed. Rolling back…
    Install-Package : Could not install package ‘Microsoft.Extensions.Logging 1.0.2’. You are trying to install this package into a project that targets ‘.NETFramework,Version=v4.5’, but the package does
    not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
    At line:1 char:1After some googling and guessing, I tried an older version and it worked:Install-Package CassandraCSharpDriver -Version 3.1.0Mind you, at this point, I really had no idea what I was doing it.
  8. I copied some code from the C# tutorial on this site:  https://academy.datastax.com/resources/getting-started-apache-cassandra-and-c-net
    // Connect to the demo keyspace on our cluster running at 127.0.0.1
    Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
    ISession session = cluster.Connect("demo");

    It failed on the Connect command, with the following .NET error:

    An unhandled exception of type ‘Cassandra.InvalidQueryException’ occurred in Cassandra.dll

    Additional information: Keyspace ‘demo’ does not exist

  9. So, now I realized I needed a demo database, or a tool to see what database are installed.
  10. I like GUI’s (Graphical User Interfaces) so I googled and found reference to DevCenter, downloaded it.  Got a file called DevCenter-1.6.0-win-x86.zip, so I created a folder C:\DataStax\DevCenter and unzipped it.  No MSI install needed.   Just run DevCenter.exe and the nice GUI opens up.
  11. Recall that I’m winging this whole experiment, trying to use past SQL Server Knowledge and tidbits picked up from the .NET meetup.  I ran the commands below, but one at a time. I’ll explain them after the picture.
  12. To see if the demo database existed, I ran:SELECT * FROM system_schema.keyspaces;The command and results are shown below:“demo” didn’t exist ,and neither did “nealdemo” the first time I ran this.
  13. So I preceded to create the keyspace called “nealdemo”.CREATE KEYSPACE NealDemo
    WITH replication = {‘class’: ‘SimpleStrategy’, ‘replication_factor’ : 3};
    SELECT * FROM system_schema.keyspaces;And it actually worked fine the first time!
  14. Then I needed a table, so I ran this:use nealdemo;
    CREATE TABLE archive (
    filename text PRIMARY KEY,
    fileext text,
    folder text,
    filedate date,
    dateinserted date,
    filecontents blob
    ) WITH comment=’archive of files’
    AND read_repair_chance = 1.0;My experiment is to archive some files on disk into a database instead, including the entire contents of the file (and to play with some very big files that contains XML and EDI related to BizTalk B2B data integration).If you don’t include the “use nealdemo” it fails.
  15. I was then ready to insert a couple of rows:use nealdemo;
    INSERT INTO archive (filename, fileext, folder, filedate, dateinserted, filecontents)
    VALUES (‘test1.csv’, ‘csv’, ‘C:\Archive\EDI’, ‘2017-04-07’, ‘2017-04-07’, textAsBlob(‘file contents’));use nealdemo;
    INSERT INTO archive (filename, fileext, folder, filedate, dateinserted, filecontents)
    VALUES (‘test2.csv’, ‘csv’, ‘C:\Archive\EDI’, ‘2017-04-04’, ‘2017-04-07’, textAsBlob(‘innards of the file’));The first problem I had was trying to specify the blob as just a text string.  Then I google and found that I need to wrap it with the function “textAsBlob”.
  16. Then I was ready to query my data:use nealdemo;
    select * from archive;And that worked!   So once I got into DevCenter and ran a few commands, things were looking good.
  17. Now the C# above also runs (by substituting “nealdemo” for “demo”) and it has to be lower case, even though I typed “NealDemo” when I created the keyspace. I.e. it connects and establishes the connection
  18. So I took it one step further, and added some code to retrieve data and display it – similar to the sample code I found on the site above:static void test1()
    {
    // Connect to the demo keyspace on our cluster running at 127.0.0.1
    Cluster cluster = Cluster.Builder().AddContactPoint(“127.0.0.1”).Build();
    // looks like keyspacename (parm below) has to be all lower case
    ISession session = cluster.Connect(“nealdemo”);

    // Try data retrieval
    RowSet rows = session.Execute(“select * from archive”);
    foreach (Row row in rows)
    Console.WriteLine(“{0} {1}”, row[“filename”], row[“fileext”]);

    }

    It’s not fancy or anything, but I’m impressed – I got the results back:

Summary: Not too bad for a 3-4 hour experiment. A few frustrations were encountered. I will move forward now and try to do something a little more useful and exciting with it, and learn more about the architecture of Cassandra and it’s CQL commands (note it’s CQL, not SQL!).

Filed under: NoSQL