如何向配置指定空间的Tarantool分片发起查询请求
hotbox Space Got it, let's walk through how to query your sharded hotbox space in Tarantool. You've set up a solid set of indexes, so we'll cover everything from basic shard-key lookups to cross-shard queries for your non-shard indexes, plus some handy tips.
First: Confirm Shard Setup
Before diving into queries, make sure you've properly initialized the shard module and configured your cluster. If you haven't done this yet, here's a quick recap (adjust the replica set details to match your cluster):
local shard = require('shard') -- Initialize shard with your hotbox space configuration shard.init({ hotbox = { replicasets = { {master = 'shard1-host:3301', replicas = {'shard1-replica:3302'}}, {master = 'shard2-host:3301', replicas = {'shard2-replica:3302'}} -- Add more replicasets as needed }, -- Optional: Explicitly set shard key if it's not the first field (your index 'one' uses field 1, so this is default) -- shard_key = {1, 'string'} } })
1. Querying via the Shard Key (Index one)
Your one index is a hash index on the first string field—this is almost certainly your shard key (the field used to split data across shards). Queries using this index are the most efficient because they route directly to the correct shard.
Option 1: Use the shard.call() Method
This is a low-level, flexible way to call any space method across shards:
-- Get a single record by shard key (uses index 'one' by default) local single_record = shard.call('hotbox', 'get', {'my_unique_shard_key'}) -- Select multiple records matching the shard key (though hash indexes usually return one record) local matching_records = shard.call('hotbox', 'select', {'my_unique_shard_key', {index = 'one'}})
Option 2: Use the Shard Space Wrapper
The shard module creates a wrapper for your space that acts just like a regular Tarantool space—this is more intuitive for most use cases:
local hotbox_shard = shard.hotbox -- Get a record by shard key local record = hotbox_shard:get('my_unique_shard_key') -- Select with index 'one' (explicit, though not strictly necessary here) local results = hotbox_shard:select('my_unique_shard_key', {index = 'one'})
2. Querying Non-Shard-Key Indexes (Indexes two, three, four, five)
Indexes like two (tree on field 2), three (tree on field 3), etc., aren't tied to your shard key. To query these, you'll need to scan all shards and aggregate results using shard.map().
Example: Query Index two (String Tree Index)
-- Find all records where field 2 equals 'target_value' local all_matches = shard.map('hotbox', function(space) return space:select('target_value', {index = 'two'}) end)
Example: Range Query on Index three (Unsigned Tree Index)
-- Get all records where field 3 is greater than or equal to 100 local range_results = shard.map('hotbox', function(space) return space:select({100}, {index = 'three', iterator = 'GE'}) end)
Example: Boolean Match on Index four
-- Fetch all records where field 4 is true local boolean_matches = shard.map('hotbox', function(space) return space:select(true, {index = 'four'}) end)
Aggregating Results Across Shards
You can combine shard.map() with reduce() to compute aggregates like counts or sums:
-- Count total records where field 5 is greater than 500 local total_count = shard.map('hotbox', function(space) local res = space:select({500}, {index = 'five', iterator = 'GT'}) return #res end):reduce(function(a, b) return a + b end)
3. Pro Tips for Sharded Queries
- Prioritize Shard Key Queries: Whenever possible, structure your queries to use the shard key (index
one)—this avoids full cluster scans and is much faster. - Direct Shard Routing: If you know which shard a key belongs to, use
shard.route()to query that shard directly:local target_shard = shard.route('hotbox', 'my_shard_key') local results = target_shard:select('my_shard_key', {index = 'one'}) - Handle Empty Results: Remember that
shard.map()returns a list of results from each shard—you may need to flatten or filter empty sets depending on your use case.
内容的提问来源于stack exchange,提问作者iPhosgen

