-2

This is a small demo website with a flow of 4 categories and each has 4 sub category and I want to extract data as from amazon keyspaces tables which I have created, I am not getting any tutorial, please help I am a beginner

Output of database data on the website which is created in visual studio

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
  • 1
    Welcome to Stack Overflow! I'd like to make a friendly request and ask that you paste the outputs in text instead of screenshots. A lot of the engineers who answer questions here respond during their free time on their mobile devices and screenshots are very difficult to read. It would make it easier to help you if you paste the text instead. Cheers! – Erick Ramirez Apr 15 '23 at 23:40

2 Answers2

1

Have a look at the Cassandra Node.js driver.

Here's a sample code that shows you how to connect to Amazon Keyspaces:

const cassandra = require('cassandra-driver');
const fs = require('fs');
const auth = new cassandra.auth.PlainTextAuthProvider('ServiceUserName', 'ServicePassword');
const sslOptions1 = {
    ca: [
        fs.readFileSync('path_to_file/sf-class2-root.crt', 'utf-8')],      
            host: 'cassandra.us-west-2.amazonaws.com',
                rejectUnauthorized: true
        };
const client = new cassandra.Client({
    contactPoints: ['cassandra.us-west-2.amazonaws.com'],
        localDataCenter: 'us-west-2',
        authProvider: auth,
        sslOptions: sslOptions1,
        protocolOptions: { port: 9142 }
    });

const query = 'SELECT * FROM system_schema.keyspaces';
 
client.execute(query)
    .then( result => console.log('Row from Keyspaces %s', result.rows[0]))
    .catch( e=> console.log(`${e}`));

You will need to download the certificate for your database. See the full instructions here.

If you're interested, we have free tutorials on how to build apps for Cassandra on datastax.com/dev which include example apps + full working code for Javascript and other languages. Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
0

Roshni - you can examples of how to connect to Keyspaces from the language of your choice here - https://github.com/aws-samples/amazon-keyspaces-example. Here is some sample code -

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

// Variables
const cassandra = require('cassandra-driver');
const fs = require('fs');
const sigV4 = require('aws-sigv4-auth-cassandra-plugin');
// custom retry policy for AmazonKeyspaces to retry on same host
const custom_retry = require('./retry/AmazonKeyspacesRetryPolicy.js');
// Max number of retry attempts for custom retry
const Max_retry_attempts = 10
require('dotenv').config();
const region = process.env.AWS_REGION;
const accessKey = process.env.AWS_ACCESS_KEY_ID;
const secretKey = process.env.AWS_SECRET_ACCESS_KEY;

// Check that environment variables are not undefined
if (!region) {
    console.log("You do not have a region set. Set environment variable AWS_REGION");
    process.exit(1);
}

if (!accessKey) {
    console.log("You do not have an access key set. Set environment variable AWS_ACCESS_KEY_ID");
    process.exit(1);
}

if (!secretKey) {
    console.log("You do not have a secret key set. Set environment variable AWS_SECRET_ACCESS_KEY");
    process.exit(1);
}

const auth = new sigV4.SigV4AuthProvider({
    region: region,
    accessKeyId: accessKey,
    secretAccessKey: secretKey
});

const host = 'cassandra.' + region + '.amazonaws.com'
const sslOptions = {
    ca: [
        fs.readFileSync(__dirname + '/resources/sf-class2-root.crt')
    ],
    host: host,
    rejectUnauthorized: true
};

const client = new cassandra.Client({
    contactPoints: [host],
    localDataCenter: region,
    authProvider: auth,
    sslOptions: sslOptions,
    queryOptions: { isIdempotent: true, consistency: cassandra.types.consistencies.localQuorum },
    policies: { retry: new custom_retry.AmazonKeyspacesRetryPolicy(Max_retry_attempts) },
    protocolOptions: { port: 9142 }
});

const query = 'SELECT * FROM system_schema.keyspaces';

const result = client.execute(query).then(
    result => console.log('Row from Keyspaces %s', result.rows[0])
).catch(
    e => console.log(`${e}`)
);

Promise.allSettled([result]).finally(() => client.shutdown());
meet-bhagdev
  • 2,608
  • 18
  • 22