Using a Cassandra Perl client driver to access Amazon Keyspaces programmatically - Amazon Keyspaces (for Apache Cassandra)

Using a Cassandra Perl client driver to access Amazon Keyspaces programmatically

This section shows you how to connect to Amazon Keyspaces by using a Perl client driver. For this code sample, we used Perl 5. Amazon Keyspaces requires the use of Transport Layer Security (TLS) to help secure connections with clients.

Important

To create a secure connection, our code samples use the Starfield digital certificate to authenticate the server before establishing the TLS connection. The Perl driver doesn't validate the server's Amazon SSL certificate, which means that you can't confirm that you are connecting to Amazon Keyspaces. The second step, to configure the driver to use TLS when connecting to Amazon Keyspaces is still required, and ensures that data transferred between the client and server is encrypted.

  1. Download the Cassandra DBI driver from https://metacpan.org/pod/DBD::Cassandra and install the driver to your Perl environment. The exact steps depend on the environment. The following is a common example.

    cpanm DBD::Cassandra
  2. Create a file for your application.

    touch cqlapp.pl
  3. Add the following sample code to the cqlapp.pl file.

    use DBI; my $user = "ServiceUserName"; my $password = "ServicePassword"; my $db = DBI->connect("dbi:Cassandra:host=cassandra.us-east-2.amazonaws.com;port=9142;tls=1;", $user, $password); my $rows = $db->selectall_arrayref("select * from system_schema.keyspaces"); print "Found the following Keyspaces...\n"; for my $row (@$rows) { print join(" ",@$row['keyspace_name']),"\n"; } $db->disconnect;
    Important

    Ensure that the ServiceUserName and ServicePassword match the user name and password you obtained when you generated the service-specific credentials by following the steps to Generate service-specific credentials.

    Note

    For a list of available endpoints, see Service endpoints for Amazon Keyspaces.

  4. Run the application.

    perl cqlapp.pl