How to connect with Amazon Keyspaces using IntelliJ, PyCharm or DataGrip IDEs
Published: 2022-12-12 19:20:16

<!-- wp:paragraph -->
<p>&nbsp;Amazon Keyspaces is a database service highly compatible with Apache Cassandra. It is a readily available service that can handle a high range of requests per second. Hence, Amazon Web Services (AWS) customers use&nbsp;<a target="_blank" href="https://aws.amazon.com/keyspaces/" rel="noreferrer noopener">Amazon Keyspaces (for Apache Cassandra)</a>&nbsp;in order to modernize their Cassandra workloads.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What is Apache Cassandra?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Apache Cassandra is a non-relational database that offers fast read and write performance. It is similar to a traditional relational database but doesn't rely on tables. Instead, the data is stored across multiple nodes and is accessed via a column-oriented structure called a ring.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It's an open-source project, meaning anyone can freely modify and distribute it.You can read more on Apache Cassandra&nbsp;<a target="_blank" href="https://aws.amazon.com/keyspaces/what-is-cassandra/" rel="noreferrer noopener">here</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Why use Amazon Keyspaces (for Apache Cassandra)?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Amazon Keyspaces is compatible with Apache Cassandra. It is very convenient for developers because it allows them to run Cassandra workloads on AWS without needing to master new application code and developer tools.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Amazon Keyspaces is serverless, so you do not need to provision, patch, or manage servers. You need only pay for the services you use, making Keyspaces a highly time and cost-efficient tool.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Connecting to amazon keyspaces from your IDE:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To better develop with Amazon Keyspaces, it's beneficial to be able to connect to it directly from your development environment. This lets you write code and iterate on schemas within the same tool.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To connect to your Amazon Keyspaces from a local IDE, follow the instructions provided in this tutorial. In addition, I've written a guide to creating new Amazon Keyspaces tables and running CQL queries as well. The ultimate goal of the tutorial is to enable you to prepare a working development environment where you can start building modern applications with Amazon Keyspaces.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The funtionalities covered in this post are available in the Enterprise version of the various IDEs developed by JetBrains. Otherwise, you can also download the evaluation version of these softwares, which are free only for 30 days.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What you need for the Setup:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a target="_blank" href="https://docs.aws.amazon.com/keyspaces/latest/devguide/programmatic.credentials.html" rel="noreferrer noopener">Service-specific credentials</a>&nbsp;for an existing AWS IAM user are a prerequisite to follow the steps in this post. Service-specific credentials are similar to the password and username Cassandra uses for access management. AWS's service-specific credentials are also associated with a specific IAM user. Read&nbsp;<a target="_blank" href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_keyspaces.html" rel="noreferrer noopener">Using IAM with Amazon Keyspaces (for Apache Cassandra)</a>&nbsp;for more details.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Remember that service-specific credentials aren't the only way to authorize access to Amazon Keyspaces resources. Suppose you can modify the code or access its external configuration. In that case, using the AWS authentication plugin for Cassandra drivers is recommended, which is available for multiple programming languages.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Remember that service-specific credentials aren’t the only way to authorize access to Amazon Keyspaces resources. Suppose you can modify the code or access its external configuration. In that case, using the AWS authentication plugin for Cassandra drivers is recommended.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>How to connect to amazon keyspaces:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Since Amazon Keyspaces is serverless and requires no infrastructure setup, you need only connect to one of the service endpoints. We will use a public service endpoint in this tutorial.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Follow the instructions below&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Go to your IDE and create a new project.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Open the&nbsp;<strong>View</strong>&nbsp;menu and choose&nbsp;<strong>Tool Windows</strong>. Then click on<strong>&nbsp;Database</strong>.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose the&nbsp;<strong>Data Source Properties.</strong></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Click on the&nbsp;<strong>Add icon</strong>&nbsp;(+) on the Data Sources tab.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose&nbsp;<strong>Apache Cassandra</strong>.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2021/09/03/DBBLOG-1585-DB_select_property.png" alt=""/><figcaption class="wp-element-caption">Source : AWS </figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>JetBrain IDEs do not list Amazon Keyspacesas one of the databases yet. But it is compatible with Apache Cassandra, so you can use your existing module to connect with Amazon Keyspaces.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Now, choose&nbsp;<strong>Download missing driver files</strong>. The IDE will download the drivers required to connect with Amazon Keyspaces.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>The following screen will appear next.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Enter an Amazon Keyspaces service endpoint for&nbsp;<strong>Host</strong>&nbsp;on the&nbsp;<strong>General tab</strong>.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>When you use Apache Cassandra without connecting to Amazon Keyspaces, it will typically connect you to the IP address of a seed node. In contrast, Amazon Keyspaces requires you to enter one of the Amazon Keyspaces&nbsp;<a target="_blank" href="https://docs.aws.amazon.com/keyspaces/latest/devguide/programmatic.endpoints.html" rel="noreferrer noopener">service endpoints</a>&nbsp;for your AWS Region as the hostname.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>For&nbsp;<strong>Port</strong>, enter 9142.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Amazon Keyspaces employs Transport Layer Security (TLS) and only accepts connections secured using TLS.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>In the field labeled&nbsp;<strong>User</strong>, enter your username.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>&nbsp;Enter your password in the Password field.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The URL is generated, and the URL field will be populated automatically after you provide the host, user, and password information.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Go to the&nbsp;<strong>SSH/SSL tab&nbsp;</strong>and choose<strong>&nbsp;Use SSL.</strong></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>After this, the remaining fields will become visible.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>The next step is to download Amazon digital certificate. You can either download it&nbsp;<a target="_blank" href="https://certs.secureserver.net/repository/sf-class2-root.crt" rel="noreferrer noopener">directly</a>&nbsp;:</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>You must convert your Amazon digital certificate to a truststore file now. You can use the given code for this purpose:</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>openssl x509 -outform der -in sf-class2-root.crt -out temp_file.der

keytool -import -alias Cassandra -Keystore cassandra_truststore.jks -file temp_file.der</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Next you have to create a password for the Keystore and then trust this certificate. See the following article for details and an example:</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><a href="https://docs.aws.amazon.com/keyspaces/latest/devguide/using_java_driver.html" target="_blank" rel="noreferrer noopener">Using a Cassandra Java Client Driver to Access Amazon Keyspaces Programmatically</a></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>&nbsp;The cassandra_truststore.jks file is generated now. Type the file location in the CA file field and the password you chose in the last step.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2021/09/03/DBBLOG-1585-ssh_screen2.png" alt=""/><figcaption class="wp-element-caption">Source : AWS </figcaption></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Change sslenabled to true.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Change consistencyLevel to LOCAL_QUORUM.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2021/09/03/DBBLOG-1585-adanced_tab.png" alt=""/><figcaption class="wp-element-caption">Source : AWS , How to connect with amazon Keyspaces</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Amazon Keyspaces supports LOCAL_QUORUM only and for write operations.For more information about Amazon Keyspaces and their consistency levels you can visit&nbsp;<a target="_blank" href="https://docs.aws.amazon.com/keyspaces/latest/devguide/consistency.html" rel="noreferrer noopener">here&nbsp;</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2><strong>Validate the connection</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In the next step, we must verify whether Amazon Keyspaces is properly connected to your IDE. for this purpose, click on&nbsp;<strong>&nbsp;Test Connection</strong>. If you specified everything correctly, a pop-up similar to the following screenshot would become visible on your computer screen:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2021/09/03/DBBLOG-1585-test_Connection.png" alt=""/><figcaption class="wp-element-caption">Source : AWS</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2><strong>Run queries</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Your IDE is now connected to the Amazon Keyspaces service. Now, using&nbsp;<a target="_blank" href="https://docs.aws.amazon.com/keyspaces/latest/devguide/what-is-cql.html" rel="noreferrer noopener">CQL</a>&nbsp;(Cassandra Query Language), we will run a few sample queries:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Open the&nbsp;<strong>View</strong>&nbsp;menu and choose&nbsp;<strong>Tool Windows.</strong></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Now click on Database.</strong></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose a data source with right-click and choose&nbsp;<strong>New</strong>, then&nbsp;<strong>Query Console.</strong></li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>See&nbsp;<a target="_blank" href="https://www.jetbrains.com/help/datagrip/working-with-database-consoles.html#working-with-console-files" rel="noreferrer noopener">Working with console files</a>&nbsp;to get better acquainted with the console features.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Creating a list of existing tables:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Enter the following query:</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>SELECT * FROM system_schema_mcs.tables;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>A list of all existing tables will be created with this CQL statement, including any internal system tables that Amazon Keyspaces may have created.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Click the&nbsp;<strong>Run</strong>&nbsp;icon on the toolbar. A list of system tables should appear on your screen even if you have not created any tables yet.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2021/09/03/DBBLOG-1585-cql_panel.png" alt=""/><figcaption class="wp-element-caption">Source : AWS (How to connect with aws keyspaces)</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Creating and populating a new keyspace:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, we will create and populate a new keyspace and a new table using the IDE.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Choose your database connection with a right-click</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Now click on&nbsp;<strong>+New&nbsp;</strong>and then choose&nbsp;<strong>Keyspaces</strong>.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Choose a name for your keyspace (for example, employee_ks) and type it in the Name field.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Click&nbsp;<strong>OK</strong>.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2021/09/03/DBBLOG-1585-create_keyspaces.png" alt=""/><figcaption class="wp-element-caption">Source : AWS How to connect Amazon Keyspaces</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The IDE will choose the SimpleSrategy strategy by default. The value of the replication factor will be chosen as 1 by default too.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There is no need to manage replication factors since the per-GB price of Amazon Keyspaces storage already includes replication.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>See the following resources for further information;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a target="_blank" href="https://docs.aws.amazon.com/keyspaces/latest/devguide/getting-started.ddl.html#getting-started.keyspaces" rel="noreferrer noopener">Creating a Keyspace</a>&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>&nbsp; <a target="_blank" href="https://aws.amazon.com/keyspaces/pricing" rel="noreferrer noopener">Amazon Keyspaces (for Apache Cassandra) Pricing</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Creating and populating a table with data:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now that you have created a new keyspace, you can create a new table and populate it with any desired data.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>For creating a table, employee_by_id_division in the employee_ks keyspace, enter the following CQL statements into the IDE.&nbsp;</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>CREATE TABLE IF NOT EXISTS "employee_ks"."employee_by_id_division"(

   id int,

   division text,

   name text,

   pay_scale int,

   role text,

   vacation_hrs float,

    PRIMARY KEY (id,division))

    WITH CLUSTERING ORDER BY("division" ASC) and custom_properties = {

   'capacity_mode':{'throughput_mode':'PROVISIONED', 'write_capacity_units':5, 'read_capacity_units':15},

   'point_in_time_recovery':{'status':'enabled'},

   'encryption_specification':{'encryption_type':'AWS_OWNED_KMS_KEY'}

} AND TAGS = {'project':'blog', 'costcenter' : '101', 'any_key':'any_val'} ;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now we must populate the new table with data and insert any number of new records. CQL insert statements are used to insert new data:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>
INSERT INTO "employee_ks"."employee_by_id_division" (id, division, name, pay_scale, role, vacation_hrs)

   VALUES (100, 'Engineering', 'Superman', 10, 'IC', 80) IF NOT EXISTS

INSERT INTO "employee_ks"."employee_by_id_division" (id, division, name, pay_scale, role, vacation_hrs)

   VALUES (120, 'Management', 'Batman', 12, 'ZZ', 80) IF NOT EXISTS;

INSERT INTO "employee_ks"."employee_by_id_division" (id, division, name, pay_scale, role, vacation_hrs)

   VALUES (130, 'Security', 'Joker', 12, 'XX', 80) IF NOT EXISTS;

//Duplicate key

INSERT INTO "employee_ks"."employee_by_id_division" (id, division, name, pay_scale, role, vacation_hrs)

   VALUES (100, 'Engineering', 'New Superman', 12, 'IC', 40) IF NOT EXISTS;

select * from "employee_ks"."employee_by_id_division</code></pre>
<!-- /wp:code -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>The results of our INSERT statement are showcased in the screenshot below.&nbsp;</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d2908q01vomqb2.cloudfront.net/887309d048beef83ad3eabf2a79a64a389ab1c9f/2021/09/03/DBBLOG-1585-Keyspace_cql_editor.png" alt=""/><figcaption class="wp-element-caption">Source : AWS Connectivity with IDE's</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2><strong>Clean up</strong></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The last and crucial step is to clean up the resources you used. Saving any resources that you do not need will incur extra charges. You can use the drop keyspace command to clean up the remaining resources created during this tutorial. This will delete the keyspace and all tables in that keyspace:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>drop keyspace employee_ks;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Read&nbsp;<a target="_blank" href="https://docs.aws.amazon.com/keyspaces/latest/devguide/getting-started.clean-up.html" rel="noreferrer noopener">Delete a Table and Keyspace in Amazon Keyspaces</a>&nbsp;to learn more about deleting a keyspace.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Conclusion:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this tutorial, I showed you how to connect to an Amazon Keyspace directly from your IDE. Now it's time to explore the keyspaces features directly within the IDE instead of through the console. This tutorial shows how to run database queries directly within the IDE.</p>
<!-- /wp:paragraph -->