Complete Cassandra guide for Cassandra beginners with PHP Demo App

Complete Cassandra guide for Cassandra beginners.
So for the past few days I had been trying to get Cassandra running on my PC, I looked upon a lot of resources, got the help but couldn’t get a test app in PHP so decided to make one. Now I have made that test Application so I think I must share this with you.
But before this we must have a little knowledge of Cassandra. So
What is Cassandra?
From Cassandra’s Wiki
Cassandra is a highly scalable, eventually consistent, distributed, structured key-value store.
Yes Cassandra is a nosql solution to save your data in hash maps. It not a traditional relational DBMS rather it is an alternative to Relational DBMSs like MySQL, SQL Server, Oracle, Postgresql and many others.
Who Uses Cassandra:
To get a fair idea of the power of Cassandra I think I must share with you some of the Users of Cassandra. Here is the list
• Facebook
• Digg
• Twitter
• IBM
• Redit
• App Scale
How to get Cassandra Running on Windows:
Pre Req: java must be installed and value of the environment variable “JAVA_HOME” must be set to its JRE Directory (in my case it is “C:\Program Files\Java\jre6”)
1. Get the latest binaries from http://cassandra.apache.org/download/”
2. Extract from Downloaded Archives to a folder say “Cassandra”(I’ll use this name in future)
3. Make three New Folders anywhere in your directory structure with names commitlog, data, saved cache.
4. Go to extracted archive and navigate to folder named “conf”
5. Open the file named “cassandra.yaml” in your favorite text editor.
6. In the “Cassandra.yaml” Set the values of
“data_file_directories” to the path of newly created “data” folder.
“commitlog_directory” to the path of newly created “commitlog” folder.
“saved_caches_directory” to the path of newly created “saved cache” folder.
So my values in “cassandra.yaml” look like this:
data_file_directories: C:\cassandra-data\data
commitlog_directory: C:\cassandra-data\commitlog
saved_caches_directory: C:\cassandra-data\saved cache
7. Add new environment variable “CASSANDRA_HOME” and set its value to the path of your Cassandra folder (Google to
know how to set environment variable).
8. Open cmd and navigate to the bin in your “Cassandra” folder
9. Run this command here “cassandra.bat”and your server will be running.
10. Open another cmd and run the command “Cassandra-cli –h localhost” and this must connect to your Cassandra
server.
11. If you see a message like “Connected to: Test Clusrer on localhost/9160” you are with me up till this point and
your Cassandra is running perfectly.
12. Run these commands on your client one by one
• Create keyspace testApp;
• Create column family Users;

Cassandra Data Model:
To understand how Data is actually modeled in Cassandra I would recommend you to go through these links.

http://arin.me/blog/wtf-is-a-supercolumn-cassandra-data-model
http://www.divconq.com/2010/how-to-add-and-retrieve-data-from-a-cassandra-database

My PHP Test App:
Note: From here this article assumes that you have average knowledge of PHP.
Cassandra offers different high level clients to interact with Cassandra for different technologies. You can have a look on these here: http://wiki.apache.org/cassandra/ClientOptions

My Test Application is developed using “PHPCassa” a high level client for PHP. What you need to do to run this app is fairly simple.
Pre Reqs: PHP Development Environment must be installed (xampp is recommended).
1. Download my source code from: https://sourceforge.net/projects/phpcassandra/files/
2. Extract from the archive anywhere in your directory structure.
3. Copy this folder to the www root of apache server i.e. the htdocs directory(in my case its C:\xampp\htdocs\).
4. Run Index.php in the application folder from your browser.
5. And you are all done.
Note: Application is coded very simply, I don’t feel any average PHP Programmer should have any problem
understanding the code.
Any Queries or Problems?
You can ask right here in the comments. I’ll try to answer the question within my domain of understanding.

Advertisements

About Asad Durrani

A recent graduate Software Engineer. Interested in History, Economics and Policy

36 responses to “Complete Cassandra guide for Cassandra beginners with PHP Demo App

  1. Alex

    Thanks Asad

    It helped

  2. Diego

    Hello.
    I couldn’t run. I tried WAMP and XAMPP, but it gave me a fatal erro.
    “Uncaught exception ‘NoServerAvailable’ with message ‘An attempt was made to connect to every server twice, but all attempts failed. The last error was: exception ‘cassandra_InvalidRequestException’ with message ‘Keyspace testApp does not exist’ in C:\wamp\www\Cassandra\phpcassa\connection.php on line 233”
    Does code php create the keyspace or I have to create it?

  3. your server is not running properly, go to xampp control panel and start server. You’ll be getting some error. Try to fix that issue first.

  4. Chips

    Many thanks to you. This is what I’ve been looking for.

  5. osman

    You are the man

  6. pravat

    good morning.
    now i want to store an image into cassandra by using php.and also retrieve.
    can u pls help me.pls reply to my email

  7. This is just a start, any place if there is short summary, why Cassandra is used?. And also diff between Hadoop and all?

  8. chaitanya

    hi i had successfully installed cassandra…i got “Connected to: Test Clusrer on localhost/9160” this message..but to check wheather database is created or not

  9. chaitanya

    what is [default@unknown] im getting this in ommand prompt after connected to cluster and welcome to Cassandra..

  10. I will have to check as it has been a long time I worked with Cassandra, you can check for your issue, if it doesnt work do let me know

    • chaitanya

      what is the meaning of “Waiting for schema agreement…
      … schemas agree across the cluster”. This message is displayed when “keyspace” is created. i.e. Database is created or not..? after that i executed “Create column family Users” but i got “Nit authorized to working keyspace”…In browser
      Fatal error: Uncaught exception ‘cassandra_NotFoundException’ in D:\xampp\htdocs\Cassandra\phpcassa\columnfamily.php:205 Stack trace: #0 D:\xampp\htdocs\Cassandra\index.php(8): ColumnFamily->__construct(Object(ConnectionPool), ‘Users’) #1 {main} thrown in D:\xampp\htdocs\Cassandra\phpcassa\columnfamily.php on line 205…Can u Help me please..?

  11. chaitanya

    Hi Asad Durrani
    I got solved my issue…but small clarification where does the php form values stores…it is not updated in my mysql database…

    • Blackie

      Hello Chaitanya.
      Can let me know how you solve this issue?

      Fatal error: Uncaught exception ‘NoServerAvailable’ with message ‘An attempt was made to connect to every server twice, but all attempts failed. The last error was: exception ‘cassandra_InvalidRequestException’ with message ‘Keyspace ‘testApp’ does not exist’ in C:\xampp\htdocs\phpcassa\thrift\Thrift.php:574 Stack trace: #0 C:\xampp\htdocs\phpcassa\thrift\packages\cassandra\Cassandra.php(1969): TBase->_read(‘Cassandra_set_k…’, Array, Object(TBinaryProtocolAccelerated)) #1 C:\xampp\htdocs\phpcassa\thrift\packages\cassandra\Cassandra.php(152): cassandra_Cassandra_set_keyspace_result->read(Object(TBinaryProtocolAccelerated)) #2 C:\xampp\htdocs\phpcassa\thrift\packages\cassandra\Cassandra.php(113): CassandraClient->recv_set_keyspace() #3 C:\xampp\htdocs\phpcassa\connection.php(102): CassandraClient->set_keyspace(‘testApp’) #4 C:\xampp\htdocs\phpcassa\connection.php(84): ConnectionWrapper->set_keyspace(‘testApp’) #5 C:\xampp\htdocs\phpcassa\connection.php(222): ConnectionWrapper->__construct(‘testApp’, ‘127.0.0.1:9160’, NULL in C:\xampp\htdocs\phpcassa\connection.php on line 233

  12. chaitanya

    Is there any tool similar to phpmyadmin for cassandra…

  13. nidhin

    How can i solve this error?Please help me

    Fatal error: Uncaught exception ‘NoServerAvailable’ with message ‘An attempt was made to connect to every server twice, but all attempts failed. The last error was: exception ‘cassandra_InvalidRequestException’ with message ‘Keyspace testApp does not exist’ in D:\xampp\htdocs\Cassandra\phpcassa\thrift\Thrift.php:574 Stack trace: #0 D:\xampp\htdocs\Cassandra\phpcassa\thrift\packages\cassandra\Cassandra.php(1969): TBase->_read(‘Cassandra_set_k…’, Array, Object(TBinaryProtocolAccelerated)) #1 D:\xampp\htdocs\Cassandra\phpcassa\thrift\packages\cassandra\Cassandra.php(152): cassandra_Cassandra_set_keyspace_result->read(Object(TBinaryProtocolAccelerated)) #2 D:\xampp\htdocs\Cassandra\phpcassa\thrift\packages\cassandra\Cassandra.php(113): CassandraClient->recv_set_keyspace() #3 D:\xampp\htdocs\Cassandra\phpcassa\connection.php(102): CassandraClient->set_keyspace(‘testApp’) #4 D:\xampp\htdocs\Cassandra\phpcassa\connection.php(84): ConnectionWrapper->set_keyspace(‘testApp’) #5 D:\xampp\htdocs\Cassandra\phpcassa\connection.php(22 in D:\xampp\htdocs\Cassandra\phpcassa\connection.php on line

  14. nidhin

    How to get row count in cassandra?

  15. Blackie

    Sorry about asking a dumb question.
    I already turn on my XAMPP and double click on the index.php that you provided.

    Why I just display a simply coding without any user interface.
    That is a right view?

  16. Blackie

    Asad Durrani
    Do you mind to give me your email or Facebook link to discuss my Cassandra with you?

    Because I need to solve some problem very urgent in order to complete my research.

  17. wdw test

    hello sir,
    i have downloaded the zip file. and i found “phpcassa” folder in that. i want to ask that whether this folder is inbuilt or you created it. and if i have to create new application then can i use this folder as it is?

  18. vivek

    I am getting this error

    C:\xampp\Cassandra>bin\cassandra.bat -f
    Starting Cassandra Server
    Error opening zip file or JAR manifest missing : C:\xampp\Cassandra;\lib\jamm-0.2.5.jar
    Error occurred during initialization of VM
    agent library failed to init: instrument

  19. vivek

    Missed One statement
    • Create keyspace testApp;
    • Use testApp;
    • Create column family Users;

  20. Hi,
    First i want to thank you for your test application in Cassandra. I am downloaded that file and it seems working great. You are using both update and insert in same command INSERT.I have a question that, how can i update using UPDATE command without using insert.

  21. Hello,

    I am getting error like this. Any one can help me ??

    C:\cassandra-data\bin>cassandra.bat
    WARNING! Powershell script execution unavailable.
    Please use ‘powershell Set-ExecutionPolicy Unrestricted’
    on this user-account to run cassandra with fully featured
    functionality on this platform.
    Starting with legacy startup options
    Starting Cassandra Server
    Error occurred during initialization of VM
    Could not reserve enough space for object heap

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: