<jupyter_start><jupyter_text>Lesson 1 Demo 2: Creating a Table with Apache Cassandraimg file = Cassandralogo  Walk through the basics of Apache Cassandra:Creating a table Inserting rows of dataRunning a simple SQL query to validate the information.   Use a python wrapper/ python driver called cassandra to run the Apache Cassandra queries. This library should be preinstalled but in the future to install this library you can run this command in a notebook to install locally: `! pip install cassandra-driver`More documentation can be found here:  https://datastax.github.io/python-driver/  Import Apache Cassandra python package<jupyter_code>import cassandra<jupyter_output><empty_output><jupyter_text>Create a connection to the database1. Connect to the local instance of Apache Cassandra *['127.0.0.1']*.2. The connection reaches out to the database (*studentdb*) and uses the correct privileges to connect to the database (*user and password = <PASSWORD>*).3. Once we get back the cluster object, we need to connect and that will create our session that we will use to execute queries.    *Note 1:* This block of code will be standard in all notebooks<jupyter_code>from cassandra.cluster import Cluster
try: 
    cluster = Cluster(['127.0.0.1']) #If you have a locally installed Apache Cassandra instance
    session = cluster.connect()
except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>Test the Connection and Error Handling Code*Note:* The try-except block should handle the error: We are trying to do a `select *` on a table but the table has not been created yet.<jupyter_code>try: 
    session.execute("""select * from music_libary""")
except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>Create a keyspace to the work in *Note:* We will ignore the Replication Strategy and factor information right now as those concepts are covered in depth in Lesson 3. Remember, this will be the strategy and replication factor on a one node local instance.<jupyter_code>try:
    session.execute("""
    CREATE KEYSPACE IF NOT EXISTS udacity 
    WITH REPLICATION = 
    { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"""
)

except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>Connect to our Keyspace.*Compare this to how a new session in PostgreSQL is created.*<jupyter_code>try:
    session.set_keyspace('udacity')
except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>Begin with creating a Music Library of albums. Each album has a lot of information we could add to the music library table. We will  start with album name, artist name, year.   But ...Stop We are working with Apache Cassandra a NoSQL database. We can't model our data and create our table without more information. Think about what queries will you be performing on this data?  We want to be able to get every album that was released in a particular year. `select * from music_library WHERE YEAR=1970`*To do that:*  We need to be able to do a WHERE on YEAR. YEAR will become my partition key,artist name will be my clustering column to make each Primary Key unique. **Remember there are no duplicates in Apache Cassandra.****Table Name:** music_library**column 1:** Album Name**column 2:** Artist Name**column 3:** Year PRIMARY KEY(year, artist name) Now to translate this information into a Create Table Statement. More information on Data Types can be found here: https://datastax.github.io/python-driver/*Note:* Again, we will go in depth with these concepts in Lesson 3.<jupyter_code>query = "CREATE TABLE IF NOT EXISTS music_library "
query = query + "(year int, artist_name text, album_name text, PRIMARY KEY (year, artist_name))"
try:
    session.execute(query)
except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>The query should run smoothly.  Insert two rows<jupyter_code>query = "INSERT INTO music_library (year, artist_name, album_name)"
query = query + " VALUES (%s, %s, %s)"

try:
    session.execute(query, (1970, "The Beatles", "Let it Be"))
except Exception as e:
    print(e)
    
try:
    session.execute(query, (1965, "The Beatles", "Rubber Soul"))
except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>Validate your data was inserted into the table.*Note:* The for loop is used for printing the results. If executing queries in the cqlsh, this would not be required.*Note:* Depending on the version of Apache Cassandra you have installed, this might throw an "ALLOW FILTERING" error instead of printing the 2 rows that we just inserted. This is to be expected, as this type of query should not be performed on large datasets, we are only doing this for the sake of the demo.<jupyter_code>query = 'SELECT * FROM music_library'
try:
    rows = session.execute(query)
except Exception as e:
    print(e)
    
for row in rows:
    print (row.year, row.album_name, row.artist_name)<jupyter_output><empty_output><jupyter_text>Validate the Data Model with the original query.`select * from music_library WHERE YEAR=1970`<jupyter_code>query = "select * from music_library WHERE YEAR=1970"
try:
    rows = session.execute(query)
except Exception as e:
    print(e)
    
for row in rows:
    print (row.year, row.album_name, row.artist_name)<jupyter_output><empty_output><jupyter_text>Drop the table to avoid duplicates and clean up.<jupyter_code>query = "drop table music_library"
try:
    rows = session.execute(query)
except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>Close the session and cluster connection<jupyter_code>session.shutdown()
cluster.shutdown()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07887771307570143, 'fraction_numerical': 0.007940709370037057, 'mean_word_length': 5.004237288135593, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '117813', 'license': 'MIT', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1575, 'n_words': 697, 'path': 'data_modelling/01_indroduction_to_data_modelling/support_material/l1-demo-2-creating-a-table-with-apache-cassandra.ipynb', 'repo_name': 'MonNum5/udacity_data_engineer', 'size': '5664'}