nodetool rangekeysample
Displays a sample of partition keys from each token range owned by the node.
Synopsis
Section titled “Synopsis”nodetool [connection_options] rangekeysampleSee connection options for connection options.
Description
Section titled “Description”nodetool rangekeysample returns a sample of partition keys from each token range owned by the local node. The samples are obtained from the SSTable index files, providing a quick way to see representative partition keys without scanning all data.
How It Works
Section titled “How It Works”Cassandra maintains partition key samples in memory for each SSTable, derived from the SSTable index. These samples are used internally for:
- Estimating partition counts
- Calculating data distribution statistics
- Optimizing read operations
The rangekeysample command exposes these samples, showing actual partition key values that exist in each token range on the node.
What the Output Represents
Section titled “What the Output Represents”Each line in the output represents a sampled partition key. The keys shown are:
- Token values derived from partition keys in SSTables on the local node
- Distributed across token ranges the node owns
- A statistical sample, not an exhaustive list
- Representative of data distribution patterns
Output Format
Section titled “Output Format”The command outputs token values (not the partition key strings directly) with a header:
RangeKeySample:<token_value_1><token_value_2><token_value_3>...Example Output
Section titled “Example Output”RangeKeySample:-9223372036854775808-8523372036854775000-7123372036854775123-6023372036854775456...Token Values vs Partition Keys
The output shows token values (the hash of partition keys), not the human-readable partition key strings. To find which partition key corresponds to a token, use SELECT * FROM table WHERE token(partition_key) = <token>.
Arguments
Section titled “Arguments”This command takes no arguments. It samples keys from all keyspaces and tables on the node.
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”nodetool rangekeysampleSave Samples to File
Section titled “Save Samples to File”nodetool rangekeysample > /tmp/key_samples.txtCount Sample Size
Section titled “Count Sample Size”nodetool rangekeysample | wc -lView First 20 Samples
Section titled “View First 20 Samples”nodetool rangekeysample | head -20Filter for Specific Key Patterns
Section titled “Filter for Specific Key Patterns”# Find samples matching a patternnodetool rangekeysample | grep "user_"
# Find samples starting with specific prefixnodetool rangekeysample | grep "^order"Use Cases
Section titled “Use Cases”Investigating Data Distribution
Section titled “Investigating Data Distribution”Examine what partition keys exist on a specific node to understand data placement:
# Sample keys on each node to compare distributionfor node in node1 node2 node3; do echo "=== $node ===" ssh "$node" "nodetool rangekeysample" | wc -ldoneUneven sample counts may indicate data skew or hot spots.
Identifying Partition Key Patterns
Section titled “Identifying Partition Key Patterns”Discover what types of partition keys exist in the cluster:
# Get unique prefixes to understand key naming patternsnodetool rangekeysample | cut -c1-10 | sort | uniq -c | sort -rn | head -20Validating Data After Migration
Section titled “Validating Data After Migration”After migrating data, verify that expected partition keys are present:
# Check if specific key patterns existnodetool rangekeysample | grep -c "expected_prefix"Debugging Hot Partitions
Section titled “Debugging Hot Partitions”When investigating potential hot partitions, sample keys to identify candidates:
# Sample keys and cross-reference with known hot partition patternsnodetool rangekeysample > samples.txt# Compare with application logs showing slow queriesEstimating Partition Count
Section titled “Estimating Partition Count”While not exact, the sample count gives a rough indication of partition density:
# Samples per nodenodetool rangekeysample | wc -l# Higher counts suggest more partitionsPre-Migration Analysis
Section titled “Pre-Migration Analysis”Before cluster migration or expansion, understand current key distribution:
# Document current key samples for comparison after migrationnodetool rangekeysample > pre_migration_samples_$(hostname).txtUnderstanding the Sample
Section titled “Understanding the Sample”Sample Size
Section titled “Sample Size”The number of keys returned depends on:
- Total partitions on the node
- SSTable count per table
- Sampling interval configured in Cassandra (default samples every 128th key)
- Index entries in each SSTable
Sampling Rate
Section titled “Sampling Rate”Cassandra's SSTable index sampling interval is configured in cassandra.yaml:
# Default: sample 1 key per 128 partitionsindex_summary_resize_interval_in_minutes: 60index_summary_capacity_in_mb: 0 # Auto-calculated based on heapInterpreting Results
Section titled “Interpreting Results”| Observation | Possible Meaning |
|---|---|
| Few samples | Node has few partitions or few SSTables |
| Many samples | Node stores many partitions |
| Patterns in keys | Application key design visible |
| No output | Node may have no data or SSTables |
Limitations
Section titled “Limitations”Important Considerations
- Not exhaustive - Only returns sampled keys, not all partition keys
- Local node only - Shows keys from the node where command is run
- All tables combined - Cannot filter by keyspace or table
- Point-in-time - Represents data at execution time
- No token information - Does not show which token range each key belongs to
- Memory-based - Samples are from index summaries held in memory
Getting Complete Key Lists
Section titled “Getting Complete Key Lists”For exhaustive partition key lists (not just samples), use CQL:
-- Warning: This can be expensive on large tablesSELECT DISTINCT token(partition_key), partition_keyFROM keyspace.table;Or use sstablekeys tool for offline analysis:
# List all keys in an SSTablesstablekeys /var/lib/cassandra/data/keyspace/table-uuid/nb-1-big-Data.dbCombining with Other Commands
Section titled “Combining with Other Commands”With Token Ring Information
Section titled “With Token Ring Information”# Compare key samples with token rangesecho "=== Token Ranges ==="nodetool ring | head -20
echo ""echo "=== Key Samples ==="nodetool rangekeysample | head -20With Table Statistics
Section titled “With Table Statistics”# Correlate samples with partition countsecho "=== Estimated Partitions ==="nodetool tablestats my_keyspace.my_table | grep "Number of partitions"
echo ""echo "=== Sample Count ==="nodetool rangekeysample | wc -lAcross All Nodes
Section titled “Across All Nodes”#!/bin/bash# collect_key_samples.sh - Gather samples from all nodes
OUTPUT_DIR="/tmp/key_samples_$(date +%Y%m%d)"mkdir -p $OUTPUT_DIR
# Get list of node IPs from local nodetool statusnodes=$(nodetool status | grep "^UN" | awk '{print $2}')
for node in $nodes; do echo "Collecting from $node..." ssh "$node" "nodetool rangekeysample" > "$OUTPUT_DIR/samples_$node.txt" count=$(wc -l < "$OUTPUT_DIR/samples_$node.txt") echo " $count samples collected"done
echo ""echo "Samples saved to $OUTPUT_DIR"
# Summaryecho ""echo "=== Sample Counts by Node ==="wc -l $OUTPUT_DIR/samples_*.txtTroubleshooting
Section titled “Troubleshooting”Empty Output
Section titled “Empty Output”If the command returns no output:
# Check if node has datanodetool tablestats | grep "Space used"
# Check if SSTables existls /var/lib/cassandra/data/*/*/*.db | head
# Node may need compaction to generate index summariesnodetool compactionstatsVery Few Samples
Section titled “Very Few Samples”Few samples may indicate:
- Low partition count
- Few SSTables (data mostly in memtables)
- Recent node with limited data
# Force flush to create SSTablesnodetool flush
# Then re-samplenodetool rangekeysample | wc -lCommand Hangs
Section titled “Command Hangs”If the command takes too long:
# May indicate memory pressure or large index summaries# Check JMX connectivitynodetool info
# Check for memory issuesnodetool gcstatsBest Practices
Section titled “Best Practices”Usage Guidelines
- Use for exploration - Helpful for understanding data, not production monitoring
- Combine with other tools - Cross-reference with
ring,tablestats,getendpoints - Sample all nodes - For complete picture, gather from entire cluster
- Consider timing - Run after compaction for most accurate representation
- Save for comparison - Store samples before and after major changes
Related Commands
Section titled “Related Commands”| Command | Relationship |
|---|---|
| ring | View token ring and ownership |
| getendpoints | Find which nodes store a specific key |
| describering | Detailed ring information |
| tablestats | Table statistics including partition estimates |
| status | Cluster status and data load per node |