question

chandrasekar.b03_190734 avatar image
chandrasekar.b03_190734 asked oded.erner_188979 commented

How do I get around the "'connection' is not defined" error in the workshop example code?

In week 3 (option 1), I was unable to test db_connection.py and Ex02_Connect_to_Cassandra.py codes in the terminal. I have also installed Cassandra-driver using 'pip' command. I also used gitpod to execute but I encountered the same error there. So kindly help me solve this issue in connecting my Cassandra!!

This is my db_connection.py code:

#!/usr/bin/env python3      
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider

# This is the Zip file you downloaded
SECURE_CONNECT_BUNDLE = 'F:\education\project data\Cassandra\Cassandra Workshop series\Session materials\cassandra-workshop-series-master updated\cassandra-workshop-series-master\week3-AppDev-crud\crud-python/secure-connect-killrvideocluster.zip'

# This is the username, recommended value was KVUser
USERNAME = "KVUser";

# This is the password, recommended value was KVPassword
PASSWORD = "KVPassword";

# This is the keyspace name, recommended value was killrvideo
KEYSPACE = "killrvideo"; 

class Connection:
    def __init__(self):
        self.secure_connect_bundle=SECURE_CONNECT_BUNDLE
        self.path_to_creds=''
        self.cluster = Cluster(
            cloud={
                'secure_connect_bundle': self.secure_connect_bundle
            },
            auth_provider=PlainTextAuthProvider(USERNAME, PASSWORD)
        )
        self.session = self.cluster.connect(KEYSPACE)
    def close(self):
        self.cluster.shutdown()
        self.session.shutdown()

This is my Ex02_Connect_to_Cassandra.py code:

#!/usr/bin/env python3
from db_connection import Connection

print('========================================')
print('Start exercise')
try:
connection = Connection()
output = connection.session.execute("SELECT * FROM system.local")
for row in output:
    print("Your are now connected to cluster '%s'", row.cluster_name)
except:
    print('Failure')
else:
    print('Success')
finally:
print('Closing connection (up to 10s)')
connection.close()
print('========================================')
workshop
3 comments
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

francisdomoney@aol.com avatar image francisdomoney@aol.com commented ·

I have the same problem

0 Likes 0 ·
oded.erner_188979 avatar image oded.erner_188979 commented ·

i have the same issue ...

0 Likes 0 ·
oded.erner_188979 avatar image oded.erner_188979 oded.erner_188979 commented ·

check you have the correct path to secure-connect-killrvideocluster.zip

0 Likes 0 ·

1 Answer

bettina.swynnerton avatar image
bettina.swynnerton answered chandrasekar.b03_190734 edited

Hi,

in most cases that I've seen, this error can be tracked down to an incorrect path for the secure connection bundle.

This is the path from the example in the question:

SECURE_CONNECT_BUNDLE = 'F:\education\project data\Cassandra\Cassandra Workshop series\Session materials\cassandra-workshop-series-master updated\cassandra-workshop-series-master\week3-AppDev-crud\crud-python/secure-connect-killrvideocluster.zip'

it's a mix of a Windows and linux path, and it doesn't look correct.

Whether you are on Gitpod or on a local install, double-check the path. On Gitpod, you can do pwd to see the path, and ls to verify that the secure bundle is in this location.

gitpod /workspace/cassandra-workshop-series/week3-AppDev-crud $ pwd
/workspace/cassandra-workshop-series/week3-AppDev-crud
gitpod /workspace/cassandra-workshop-series/week3-AppDev-crud $ ls
crud-java    crud-python  materials  README_JAVA.MD        README.MD         secure-connect-killrvideocluster.zip
crud-nodejs  images       notebooks  README_JAVASCRIPT.MD  README_PYTHON.MD
gitpod /workspace/cassandra-workshop-series/week3-AppDev-crud $ 

Use this exact path for the connection details, and add the name of the secure bundle after that.

/workspace/cassandra-workshop-series/week3-AppDev-crud/secure-connect-killrvideocluster.zip

if you are working with an IDE locally, again, determine the exact location of the file and make sure that the path entered is valid.

I hope this helps.

2 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

chandrasekar.b03_190734 avatar image chandrasekar.b03_190734 commented ·

Thank you so much @bettina.swynnerton , now it is working on both gitpod as well as on my local system. But it is sometime connecting perfectly and when i execute it after a while, it again shows the same error on my local system.

0 Likes 0 ·
bettina.swynnerton avatar image bettina.swynnerton ♦♦ chandrasekar.b03_190734 commented ·

Perfect! That's what I was hoping to hear, thanks!

0 Likes 0 ·