Connecting to an Elassandra cluster

Before connecting to the cluster, you need to take note of node addresses and various login credentials.

Node Addresses and Login Credentials

The Connection Info page contains list of Node addresses and login credentials for Cassandra, Elassandra REST API and Kibana.

1. Click Connection Details from Manage Cluster menu to access the Connection Info page.

01_pix.png

2. On the Connection Info page, you can view Node Addresses under Cassandra Node Addresses section.

node_address_pix.png

3. Login credentials can be found under Default Credentials for Password Authentication section.

cluster_credentials_pix.png

4. Elassandra REST API credentials and certificates can be found under Elassandra section.

elassandra_rest_api_pix.png

5. Kibana credentials can be found under Kibana section.

kibana_pix.png

 

Connecting to the cluster using cqlsh

To connect to the cluster using CQLSH, refer our Connecting to Clusters Using CQLSH support article. Connecting and running CQL commands work the same for Elassandra clusters.

 

Elassandra REST API

You can also communicate with the cluster through Elassandra REST API. The following examples show how to interact with REST API using curl command. Replace keywords in <> with the corresponding cluster configs (Refer Node Addresses and Login Credentials section at the start of this article)

1. To view state of the cluster:

curl -XGET https://<elassandra_REST_API_URL>:9201/_cluster/state -u <elassandra_username>:<elassandra_password>

Result:

{"cluster_name":"Elassandra_Cluster","version":11,"state_uuid":"9ESEjhiJTQCaaD1-kus7Gw","master_node":"75375ebd-4692-4af4-8cda-6d2bbd48e0b3","blocks":{},"nodes":{"75375ebd-4692-4af4-8cda-6d2bbd48e0b3":{"name":"34.208.26.143","status":"ALIVE","transport_address":"34.208.26.143:9300","attributes":{"rack":"us-west-2c","data":"true","data_center":"AWS_VPC_US_WEST_2","master":"true"}},"04c0eeb2-c76c-4e1b-b4e2-4e296cf4d935":{"name":"35.165.13.99","status":"ALIVE","transport_address":"35.165.13.99:9300","attributes":{"rack":"us-west-2a","data":"true","data_center":"AWS_VPC_US_WEST_2","master":"true"}},"7471bfc1-fad8-421f-b67a-b061627f18a3":{"name":"52.34.201.185","status":"ALIVE",

2. To add Index:

curl -XPUT "https://<elassandra_REST_API_URL>:9201/samples" -d '{
    "settings" : {
        "index" : {
            "number_of_replicas" : 2,
            "index.search_strategy_class" : "RandomSearchStrategy",
        "index.token_ranges_bitset_cache" : true
        }
    }
}' -u <elassandra_username>:<elassandra_password>

Result:

{"acknowledged":true}

The command creates a keyspace called “samples”. View the keyspace using cqlsh:

iccassandra@cqlsh> DESCRIBE samples;

Result:

CREATE KEYSPACE samples WITH replication = {'class': 'NetworkTopologyStrategy', 'AWS_VPC_US_WEST_2': '3'}  AND durable_writes = true;

3. Add Mapping: This adds a table and creates the mapping from CQL Types to Elasticsearch types used in secondary index. It uses the Elasticsearch mapping API with some CQL additions.

curl -XPUT "https://<elassandra_REST_API_URL>:9201/samples/_mapping/earthquakes" -d '{
  "earthquakes": {
    "properties": {
      "id": {
        "type": "string",
        "index": "not_analyzed",
        "cql_collection": "singleton"
      },
      "location": {
        "type": "geo_point"
      },
      "mag": {
        "type": "double",
        "cql_collection": "singleton",
        "cql_partition_key": false,
        "cql_primary_key_order": 1
      },
      "place": {
        "type": "string",
        "index": "analyzed",
        "cql_collection": "singleton"
      },
      "time": {
        "type": "date",
        "cql_collection": "singleton",
        "format": "strict_date_optional_time||epoch_millis||EEE MMM dd HH:mm:ss zzz yyyy",
        "cql_partition_key": true,
        "cql_primary_key_order": 0
      },
      "url": {
        "type": "string",
        "index": "not_analyzed",
        "cql_collection": "singleton"
      },
      "felt": {
        "type": "boolean",
        "cql_collection": "singleton"
      },
      "cdi": {
        "type": "double",
        "cql_collection": "singleton"
      },
      "tsunami": {
        "type": "boolean",
        "cql_collection": "singleton"
      },
      "sig": {
        "type": "integer",
        "cql_collection": "singleton"
      },
      "type": {
          "type": "string",
          "index": "not_analyzed",
          "cql_collection": "singleton"
      },
      "title": {
          "type": "string",
          "index": "analyzed",
          "cql_collection": "singleton"
      }
    }
  }
}' -u <elassandra_username>:<elassandra_password> -w "\n"

Result:

{"acknowledged":true} 

Table “earthquakes” is created under keyspace “samples”. View table using cqlsh:

iccassandra@cqlsh> USE samples;
iccassandra@cqlsh:samples> DESCRIBE earthquakes;

Result:

CREATE TABLE samples.earthquakes (
    time timestamp,
    mag double,
    cdi double,
    felt boolean,
    id text,
    location list<frozen>,
    place text,
    sig int,
    title text,
    tsunami boolean,
    type text,
    url text,
    PRIMARY KEY (time, mag)
) WITH CLUSTERING ORDER BY (mag ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = 'Auto-created by Elassandra'
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';
CREATE CUSTOM INDEX elastic_earthquakes_felt_idx ON samples.earthquakes (felt) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_type_idx ON samples.earthquakes (type) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_sig_idx ON samples.earthquakes (sig) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_id_idx ON samples.earthquakes (id) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_url_idx ON samples.earthquakes (url) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_title_idx ON samples.earthquakes (title) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_cdi_idx ON samples.earthquakes (cdi) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_tsunami_idx ON samples.earthquakes (tsunami) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_place_idx ON samples.earthquakes (place) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_mag_idx ON samples.earthquakes (mag) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
CREATE CUSTOM INDEX elastic_earthquakes_location_idx ON samples.earthquakes (values(location)) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';

4. Indexing: Rows or documents can be indexed through CQL or via REST

curl -XPUT "https://<elassandra_REST_API_URL>:9201/samples/earthquakes/1469922907000" -d '{
 "id": "pr16212011",
 "mag": 2.0,
 "cdi": 0,
 "felt": false,
 "location": [{lat: 18.290100, lon: -67.235600}],
 "place": "Mona Passage, Puerto Rico",
 "sig": 62,
 "title": "M 2.0 - Mona Passage, Puerto Rico",
 "tsunami": false,
 "type": "earthquake",
 "url": "https://earthquake.usgs.gov/earthquakes/eventpage/pr16212011"
}' -u <elassandra_username>:<elassandra_password>

Result:

{"_index":"samples","_type":"earthquakes","_id":"1469922907000","_version":1,"_shards":{"total":3,"successful":1,"failed":0},"created":true}

5. Data or documents in a CSV file can be indexed using CQLSH from the command line.

$ cat <csv_filename> | cqlsh cluster_ip 9042 -u <cassandra_username> -p <cassandra_password> -e "COPY <keyspace_name>.<table_name> FROM stdin"

6. To retrieve a document

curl -XGET "https://elassandra_REST_API_URL>:9201/samples/earthquakes/1469922907000" -u <elassandra_username>:<elassandra_password>

Result:

{"_index":"samples","_type":"earthquakes","_id":"1469922907000","_version":1,"found":true,"_source":{"time":"2016-07-30T23:55:07.000Z"}}

This is equivalent to the following CQL command:

iccassandra@cqlsh> SELECT * FROM samples.earthquakes WHERE time = '1469922907000';

7. Searching: Following are some examples of search commands using the REST API

Find rows with “Japan” in the title

curl -XGET https://<elassandra_REST_API_URL>:9201/samples/earthquakes/_search?q=title:japan -u <elassandra_username>:<elassandra_password>

Find rows with specific magnitude

curl -XGET https://<elassandra_REST_API_URL>:9201/samples/earthquakes/_search?q=mag:6.3 -u <elassandra_username>:<elassandra_password>

Multi Word Query

curl -XGET https://<elassandra_REST_API_URL>:9201/samples/earthquakes/_search?q=title:Raoul%20New%20Zealand -u <elassandra_username>:<elassandra_password>

Search for rows that has all the words being searched

curl -XGET https://<elassandra_REST_API_URL>:9201/samples/earthquakes/_search?pretty=true -d '{ "query" : { "match" : { "place" : {"query" : "Raoul New Zealand", "operator" : "and"} }}}' -u <elassandra_username>:<elassandra_password>

8. Aggregation: Here is an aggregate query that groups and counts earthquake by magnitude.

curl -XGET https://<elassandra_REST_API_URL>:9201/samples/earthquakes/_search?pretty=true -d '{                                         "size" : 0,
"aggs" : {
        "magnitudes" : {
        	"histogram" : {
        		"field" : "mag",
        		"interval" : 1
        	}
      	}
       }
}' -u <elassandra_username>:<elassandra_password>

Result:

{
  "took" : 268,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "hits" : {
    "total" : 75062,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "magnitudes" : {
      "buckets" : [ {
        "key" : 1,
        "doc_count" : 44067
      }, {
        "key" : 2,
        "doc_count" : 14433
      }, {
        "key" : 3,
        "doc_count" : 3490
      }, {
        "key" : 4,
        "doc_count" : 11413
      }, {
        "key" : 5,
        "doc_count" : 1545
      }, {
        "key" : 6,
        "doc_count" : 103
      }, {
        "key" : 7,
        "doc_count" : 11
      } ]
    }
  }
}
Last updated:
If you have questions regarding this article, feel free to add it to the comments below.

0 Comments

Please sign in to leave a comment.