Examples

Git Clone Datastax Java Driver

cd /home/hadoop/AdithyaJ/Cassandra/java

sudo apt install git -y

git clone https://github.com/datastax/java-driver.git
cd /home/hadoop/AdithyaJ/Cassandra/java/java-driver/driver-examples

Change driver version from 3.5.1-SNAPSHOT to 3.5.0 in pom.xml

sed -i 's/3.5.1-SNAPSHOT/3.5.0/g' pom.xml

Recursively replace IPs (Optional)

grep -rl '{"127.0.0.1"}' . | xargs sed -i 's/{"127.0.0.1"}/{"54.167.235.32", "54.161.222.196", "54.173.204.71", "34.238.155.63", "34.201.243.133"}/g'

Clean and compile

sudo apt install maven -y

mvn compile

Run Examples

Basic

mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.basic.CreateAndPopulateKeyspace" -Dexec.cleanupDaemonThreads=false

Connected to cluster: admaticcluster
title                           album                   artist
-------------------------------+-----------------------+--------------------
La Petite Tonkinoise            Bye Bye Blackbird       Joséphine Baker
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.basic.ReadCassandraVersion" -Dexec.cleanupDaemonThreads=false

Cassandra version is: 3.11.2
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.basic.ReadTopologyAndSchemaMetadata" -Dexec.cleanupDaemonThreads=false

Connected to cluster: admaticcluster
Datatacenter: dc1; Host: /172.31.94.157; Rack: rack1
Datatacenter: dc1; Host: /172.31.94.135; Rack: rack1
Datatacenter: dc1; Host: /172.31.87.84; Rack: rack1
Datatacenter: dc1; Host: /172.31.92.130; Rack: rack1
Datatacenter: dc1; Host: /172.31.85.62; Rack: rack1
Keyspace: keyspace1; Table: standard1
Keyspace: keyspace1; Table: counter1
Keyspace: keyspace1; Table: emp
Keyspace: system_traces; Table: sessions
Keyspace: system_traces; Table: events
Keyspace: system; Table: built_views
Keyspace: system; Table: sstable_activity
Keyspace: system; Table: views_builds_in_progress
Keyspace: system; Table: compaction_history
Keyspace: system; Table: hints
Keyspace: system; Table: prepared_statements
Keyspace: system; Table: peers
Keyspace: system; Table: IndexInfo
Keyspace: system; Table: local
Keyspace: system; Table: batchlog
Keyspace: system; Table: batches
Keyspace: system; Table: available_ranges
Keyspace: system; Table: paxos
Keyspace: system; Table: peer_events
Keyspace: system; Table: transferred_ranges
Keyspace: system; Table: size_estimates
Keyspace: system; Table: range_xfers
Keyspace: system_distributed; Table: parent_repair_history
Keyspace: system_distributed; Table: view_build_status
Keyspace: system_distributed; Table: repair_history
Keyspace: system_schema; Table: dropped_columns
Keyspace: system_schema; Table: tables
Keyspace: system_schema; Table: types
Keyspace: system_schema; Table: functions
Keyspace: system_schema; Table: indexes
Keyspace: system_schema; Table: keyspaces
Keyspace: system_schema; Table: columns
Keyspace: system_schema; Table: aggregates
Keyspace: system_schema; Table: triggers
Keyspace: system_schema; Table: views
Keyspace: simplex; Table: songs
Keyspace: simplex; Table: playlists
Keyspace: system_auth; Table: role_permissions
Keyspace: system_auth; Table: resource_role_permissons_index
Keyspace: system_auth; Table: roles
Keyspace: system_auth; Table: role_members

Datatypes

mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.datatypes.Blobs" -Dexec.cleanupDaemonThreads=false

Writing retrieved buffer to /tmp/blob9102968157457405513.png

JSON

mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.json.JacksonJsonColumn" -Dexec.cleanupDaemonThreads=false

Retrieved row:
id           1
user         alice (30)
user (raw)   {"name":"alice","age":30}

Retrieved row:
id           2
user         bob (35)
user (raw)   {"name":"bob","age":35}
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.json.JacksonJsonFunction" -Dexec.cleanupDaemonThreads=false

Retrieved row:
id           1
user         alice (30)
user (raw)   {"name": "alice", "age": 30}
scores       {"call_of_duty":4.8,"pokemon_go":9.7}
scores (raw) {"call_of_duty": 4.8, "pokemon_go": 9.7}

Retrieved row:
id           2
user         bob (35)
user (raw)   {"name": "bob", "age": 35}
scores       {"pokemon_go":12.4,"zelda":8.3}
scores (raw) {"pokemon_go": 12.4, "zelda": 8.3}
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.json.JacksonJsonRow" -Dexec.cleanupDaemonThreads=false

Retrieved user: alice (id 1, age 30)
Retrieved user: bob (id 2, age 35)
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.json.Jsr353JsonColumn" -Dexec.cleanupDaemonThreads=false

Retrieved row:
id           1
user         {"name":"alice","age":30}
user (raw)   {"name":"alice","age":30}

Retrieved row:
id           2
user         {"name":"bob","age":35}
user (raw)   {"name":"bob","age":35}
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.json.Jsr353JsonFunction" -Dexec.cleanupDaemonThreads=false

Retrieved row:
id           1
user         {"name":"alice","age":30}
user (raw)   {"name": "alice", "age": 30}
scores       {"call_of_duty":4.8,"pokemon_go":9.7}
scores (raw) {"call_of_duty": 4.8, "pokemon_go": 9.7}

Retrieved row:
id           2
user         {"name":"bob","age":35}
user (raw)   {"name": "bob", "age": 35}
scores       {"pokemon_go":12.4,"zelda":8.3}
scores (raw) {"pokemon_go": 12.4, "zelda": 8.3}
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.json.Jsr353JsonRow" -Dexec.cleanupDaemonThreads=false

Retrieved user: {"id":1,"age":30,"name":"alice"}
Retrieved user: {"id":2,"age":35,"name":"bob"}
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.json.PlainTextJson" -Dexec.cleanupDaemonThreads=false

Entry #1 as JSON: {"id": 1, "name": "Mouse", "specs": {"color": "silver"}}
Entry #2's specs as JSON: {"layout": "qwerty"}
Entry #1 as JSON: {"id": 1, "name": "Mouse", "specs": {"color": "silver"}}
Entry #2's specs as JSON: {"layout": "qwerty"}

Paging

mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.paging.ForwardPagingRestUi" -Dexec.cleanupDaemonThreads=false

REST Service started on http://localhost:8080/users, press CTRL+C to stop
To explore this example, start with the following request and walk from there:
curl -i http://localhost:8080/users/1/videos
curl -i http://localhost:8080/users/1/videos

HTTP/1.1 200 OK
Date: Thu, 29 Mar 2018 07:21:16 GMT
Content-type: application/json
Content-length: 686

{"videos":[{"videoid":148,"title":"video 148","added":4800000},{"videoid":147,"title":"video 147","added":4700000},{"videoid":146,"title":"video 146","added":4600000},{"videoid":145,"title":"video 145","added":4500000},{"videoid":144,"title":"video 144","added":4400000},{"videoid":143,"title":"video 143","added":4300000},{"videoid":142,"title":"video 142","added":4200000},{"videoid":141,"title":"video 141","added":4100000},{"videoid":140,"title":"video 140","added":4000000},{"videoid":139,"title":"video 139","added":3900000}],"nextPage":"http://localhost:8080/users/1/videos?page=001b0010000f000800000000003b8260040000008bf07ffffff5f07ffffff5c86a0e6c1ed72fe6acc57a7f364fad630004"}
mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.paging.RandomPagingRestUi" -Dexec.cleanupDaemonThreads=false

REST Service started on http://localhost:8080/users, press CTRL+C to stop
To explore this example, start with the following request and walk from there:
curl -i http://localhost:8080/users/1/videos
curl -i http://localhost:8080/users/1/videos

HTTP/1.1 200 OK
Date: Thu, 29 Mar 2018 07:22:54 GMT
Content-type: application/json
Content-length: 609

{"videos":[{"videoid":148,"title":"video 148","added":4800000},{"videoid":147,"title":"video 147","added":4700000},{"videoid":146,"title":"video 146","added":4600000},{"videoid":145,"title":"video 145","added":4500000},{"videoid":144,"title":"video 144","added":4400000},{"videoid":143,"title":"video 143","added":4300000},{"videoid":142,"title":"video 142","added":4200000},{"videoid":141,"title":"video 141","added":4100000},{"videoid":140,"title":"video 140","added":4000000},{"videoid":139,"title":"video 139","added":3900000}],"previousPage":null,"nextPage":"http://localhost:8080/users/1/videos?page=2"}

Retry

mvn exec:java -Dexec.mainClass="com.datastax.driver.examples.retry.DowngradingRetry" -Dexec.cleanupDaemonThreads=false

Connected to cluster: cluster1
Writing at QUORUM (retry count: 0)
Write succeeded at QUORUM
Reading at QUORUM (retry count: 0)
Read succeeded at QUORUM
sensor_id                             date        timestamp                     value
-------------------------------------+-----------+-----------------------------+--------------------+
756716f7-2e54-4715-9f00-91dcbea6cf50  2018-02-26  2018-02-26T12:53:46.345+0000  2.34
756716f7-2e54-4715-9f00-91dcbea6cf50  2018-02-26  2018-02-26T12:54:27.488+0000  2.47
756716f7-2e54-4715-9f00-91dcbea6cf50  2018-02-26  2018-02-26T12:56:33.739+0000  2.52

results matching ""

    No results matching ""