Tablestore Java SDK'yi kullanarak arama dizini sorgu sonuçları üzerinde ölçüm hesaplayabilir veya bu sonuçları gruplayabilirsiniz. Histogramlar, iç içe geçmiş gruplamalar ve gruplardaki en üst satırlar gibi yöntemleri kullanabilirsiniz.
Önkoşullar
Tablestore Java SDK paketini yükleyin ve bir istemci başlatın.
Çalışma Mantığı
Arama dizini sorgusu tamamlandıktan sonra toplama işlemi, eşleşen tüm satırlar için ölçümler hesaplar veya satırları gruplar. Ölçüm toplamaları, alanların minimum, maksimum, toplam, ortalama, sayı, farklı değer sayısı veya yüzdeliklerini hesaplar. Gruplamalar ise satırları alan değerlerine, birden fazla alana, sayısal aralıklara, coğrafi mesafelere, filtrelere, sayısal aralıklara (histogram), tarih aralıklarına veya coğrafi ızgaralara göre gruplar. Bir grubun içinde ayrıca ölçüm toplamaları veya alt gruplamalar da tanımlayabilirsiniz.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Toplama işleminde kullanılan bir arama dizini alanı için sıralama ve toplama özelliklerinin etkinleştirilmesi gerekir. Desteklenen alan türleri toplama türüne göre değişir. Arama dizini alan türleri ve bunların veri tablosu alan türleriyle eşleşmeleri hakkında bilgi için Veri türleri konusuna bakın.
Toplamalar sorgu eşleşmeleri üzerinde çalışır. Toplama içeren bir istek, yalnızca satır sorgulayan bir istekten daha karmaşıktır. Yanıtta satırlara ihtiyacınız yoksa
limitparametresini0olarak ayarlayın.Farklı değer sayısı, yüzdelikler ve alan gruplamaları yaklaşık hesaplamalar kullanır. 10.000'in altındaki farklı değer sayıları gerçek değere yakındır. 100 milyon farklı değer durumunda hata oranı yaklaşık %2'dir. Uç noktalardaki yüzdelikler genellikle daha doğrudur. Örneğin, P1 ve P99 değerleri P50'ye göre genellikle daha doğrudur. Alan gruplamalarının paralel hesaplanması da küçük bir hataya neden olabilir.
Birden fazla toplamayı birleştirebilirsiniz. Çok sayıda toplama veya derin iç içe geçme, istek karmaşıklığını artırır ve gecikme süresini yükseltebilir. İç içe geçme sınırları için Arama dizini sınırları konusuna bakın.
Verileri sorgulamak için search yöntemini çağırın. Ölçüm toplamaları için SearchQuery.aggregationList parametresini, gruplamalar için ise SearchQuery.groupByList parametresini yapılandırın.
SearchResponse search(SearchRequest request)
Aşağıdaki örnek, bir arama dizinindeki tüm satırları sorgular; fiyatların minimum, maksimum, toplam, ortalama ve sayı değerlerini, farklı kategori sayısını ve P50 yüzdelik değerini hesaplar; ayrıca satırları kategoriye göre gruplar.
SearchQuery searchQuery = SearchQuery.newBuilder()
.query(QueryBuilders.matchAll())
.limit(0)
.addAggregation(AggregationBuilders.min("min_price", "price"))
.addAggregation(AggregationBuilders.max("max_price", "price"))
.addAggregation(AggregationBuilders.sum("sum_price", "price"))
.addAggregation(AggregationBuilders.avg("avg_price", "price"))
.addAggregation(AggregationBuilders.count("price_count", "price"))
.addAggregation(AggregationBuilders.distinctCount(
"category_count", "category"))
.addAggregation(AggregationBuilders.percentiles(
"price_percentiles", "price")
.percentiles(Arrays.asList(50.0)))
.addGroupBy(GroupByBuilders.groupByField(
"category_group", "category").size(10))
.build();
SearchRequest request =
new SearchRequest("example_table", "example_index", searchQuery);
SearchResponse response = client.search(request);
AggregationResults aggregationResults = response.getAggregationResults();
System.out.println(aggregationResults
.getAsMinAggregationResult("min_price").getValue());
System.out.println(aggregationResults
.getAsMaxAggregationResult("max_price").getValue());
System.out.println(aggregationResults
.getAsSumAggregationResult("sum_price").getValue());
System.out.println(aggregationResults
.getAsAvgAggregationResult("avg_price").getValue());
System.out.println(aggregationResults
.getAsCountAggregationResult("price_count").getValue());
System.out.println(aggregationResults
.getAsDistinctCountAggregationResult("category_count").getValue());
System.out.println(aggregationResults
.getAsPercentilesAggregationResult("price_percentiles")
.getPercentilesAggregationItems());
GroupByFieldResult groupResult = response.getGroupByResults()
.getAsGroupByFieldResult("category_group");
for (GroupByFieldResultItem item :
groupResult.getGroupByFieldResultItems()) {
System.out.println(item.getKey() + ": " + item.getRowCount());
}
Parametreler
Sorgu İsteği
request parametresinin türü SearchRequest şeklindedir. Aşağıdaki tabloda parametreleri açıklanmaktadır.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sorgu Yapılandırması
request.searchQuery parametresinin türü SearchQuery şeklindedir. Aşağıdaki tabloda toplama ile ilgili parametreler açıklanmaktadır.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ölçüm Toplamaları
Aşağıdaki parametre nesnelerini request.searchQuery.aggregationList[] listesine ekleyin. aggName ilgili sonucu tanımlar ve bir istekte benzersiz olmalıdır.
MinAggregation, MaxAggregation ve AvgAggregation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SumAggregation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CountAggregation
|
|
|
|
|
|
|
|
|
|
|
|
Tüm sorgu eşleşmelerini saymak için SearchQuery içinde trackTotalCount parametresini yapılandırın ve SearchResponse.totalCount değerini okuyun. Bir arama dizinindeki tüm satırları saymak için MatchAllQuery kullanın.
DistinctCountAggregation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PercentilesAggregation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TopRowsAggregation
TopRowsAggregation özelliğini bir gruplamanın alt toplamasi olarak kullanın.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request.columnsToGet parametresi, döndürülen nitelik sütunlarını kontrol eder. Nitelik sütunlarını doğrudan arama dizininden döndürmek için arama dizinini oluştururken alanları depolayın. Hiçbir sütun belirtilmezse yalnızca birincil anahtarlar döndürülür.
Gruplamalar
Aşağıdaki parametre nesnelerini request.searchQuery.groupByList[] listesine ekleyin. groupByName ilgili sonucu tanımlar ve bir istekte benzersiz olmalıdır.
GroupByField
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
groupBySorters[] aşağıdaki değerleri destekler.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByComposite
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tablestore Java SDK, nextToken değerini bir dize olarak temsil eder. Belirteci kalıcı hale getirirseniz veya aktarırsanız içeriğini değiştirmeyin.
GroupByRange
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByGeoDistance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByFilter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByHistogram
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByDateHistogram
Tarih histogramı toplaması, Tablestore Java SDK 5.16.1 ve sonraki sürümler tarafından desteklenir. Arama dizinleri için Date alan türü, Tablestore Java SDK 5.13.9 ve sonraki sürümler tarafından desteklenir. Sürüm bilgileri için Tablestore Java SDK sürüm geçmişi konusuna bakın.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GroupByGeoGrid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Yanıt
search yöntemi SearchResponse döndürür. Aşağıdaki tabloda toplama ile ilgili alanlar açıklanmaktadır.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ölçüm Toplaması Sonuçları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Gruplama Sonuçları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Örnekler
Alt Toplamalar ve Alt Gruplamalar Kullanma
Aşağıdaki örnek, satırları kategoriye göre gruplar, her kategorideki en yüksek fiyatı hesaplar ve ardından her kategorideki satırları şehre göre gruplar. Grup sıralama kuralları eklendikleri sırayla etkili olur.
SearchQuery searchQuery = SearchQuery.newBuilder()
.query(QueryBuilders.matchAll())
.limit(0)
.addGroupBy(GroupByBuilders.groupByField(
"category_group", "category")
.size(10)
.addGroupBySorter(GroupBySorter.groupKeySortInAsc())
.addSubAggregation(AggregationBuilders.max(
"max_price", "price"))
.addSubGroupBy(GroupByBuilders.groupByField(
"city_group", "city").size(10)))
.build();
SearchRequest request =
new SearchRequest("example_table", "example_index", searchQuery);
SearchResponse response = client.search(request);
GroupByFieldResult result = response.getGroupByResults()
.getAsGroupByFieldResult("category_group");
for (GroupByFieldResultItem item :
result.getGroupByFieldResultItems()) {
double maxPrice = item.getSubAggregationResults()
.getAsMaxAggregationResult("max_price")
.getValue();
GroupByFieldResult cityResult = item.getSubGroupByResults()
.getAsGroupByFieldResult("city_group");
System.out.println(item.getKey() + ": " + maxPrice);
System.out.println(cityResult.getGroupByFieldResultItems());
}
Çok Alanlı Gruplamada Sayfalama
GroupByComposite, çok sütunlu anahtarları düz bir yapıda döndürür ve nextToken ile sayfalama yapmayı destekler.
GroupByComposite.Builder compositeBuilder = GroupByBuilders
.groupByComposite("category_city_group")
.addSources(GroupByBuilders.groupByField(
"category", "category")
.addGroupBySorter(GroupBySorter.groupKeySortInAsc()))
.addSources(GroupByBuilders.groupByField(
"city", "city")
.addGroupBySorter(GroupBySorter.groupKeySortInAsc()))
.size(100);
String nextToken = null;
do {
GroupByComposite groupBy = nextToken == null
? compositeBuilder.build()
: compositeBuilder.nextToken(nextToken).build();
SearchQuery searchQuery = SearchQuery.newBuilder()
.query(QueryBuilders.matchAll())
.limit(0)
.addGroupBy(groupBy)
.build();
SearchRequest request = new SearchRequest(
"example_table", "example_index", searchQuery);
SearchResponse response = client.search(request);
GroupByCompositeResult result = response.getGroupByResults()
.getAsGroupByCompositeResult("category_city_group");
for (GroupByCompositeResultItem item :
result.getGroupByCompositeResultItems()) {
System.out.println(item.getKeys() + ": " + item.getRowCount());
}
nextToken = result.getNextToken();
} while (nextToken != null);
Aralık, Mesafe ve Filtreye Göre Gruplama
Aşağıdaki kod, üç gruplama türünün temel yapılandırmalarını gösterir. Bunları aynı SearchQuery içinde birleştirebilirsiniz.
GroupByRange priceRanges = GroupByBuilders
.groupByRange("price_ranges", "price")
.addRange(0, 100)
.addRange(100, 500)
.build();
GroupByGeoDistance distanceRanges = GroupByBuilders
.groupByGeoDistance("distance_ranges", "location")
.origin(30.2741, 120.1551)
.addRange(0, 10000)
.addRange(10000, 100000)
.build();
GroupByFilter categoryFilters = GroupByBuilders
.groupByFilter("category_filters")
.addFilter(QueryBuilders.term("category", "books"))
.addFilter(QueryBuilders.term("category", "games"))
.build();
Sayısal ve Tarih Histogramları Oluşturma
Aşağıdaki örnek, satırları 20'lik bir sayısal aralığa ve bir aylık bir tarih aralığına göre gruplar.
GroupByHistogram priceHistogram = GroupByBuilders
.groupByHistogram("price_histogram", "price")
.interval(20)
.offset(0)
.minDocCount(1L)
.addFieldRange(0, 100)
.addGroupBySorter(GroupBySorter.groupKeySortInAsc())
.build();
GroupByDateHistogram dateHistogram = GroupByBuilders
.groupByDateHistogram("date_histogram", "event_date")
.interval(1, DateTimeUnit.MONTH)
.fieldRange("2026-01-01", "2026-06-01")
.timeZone("+08:00")
.minDocCount(1L)
.addGroupBySorter(GroupBySorter.groupKeySortInAsc())
.build();
SearchQuery searchQuery = SearchQuery.newBuilder()
.query(QueryBuilders.matchAll())
.limit(0)
.addGroupBy(priceHistogram)
.addGroupBy(dateHistogram)
.build();
SearchResponse response = client.search(new SearchRequest(
"example_table", "example_index", searchQuery));
Coğrafi Izgaraya Göre Gruplama
Aşağıdaki örnek, coğrafi bir alanı yaklaşık 39 km × 19 km boyutundaki GeoHash ızgaralarına göre gruplar.
SearchQuery searchQuery = SearchQuery.newBuilder()
.query(QueryBuilders.matchAll())
.limit(0)
.addGroupBy(GroupByBuilders.groupByGeoGrid(
"geo_grid", "location")
.precision(GeoHashPrecision.GHP_39KM_19KM_4)
.size(100))
.build();
SearchResponse response = client.search(new SearchRequest(
"example_table", "example_index", searchQuery));
GroupByGeoGridResult result = response.getGroupByResults()
.getAsGroupByGeoGridResult("geo_grid");
System.out.println(result.getGroupByGeoGridResultItems());
Gruplardan Satır Döndürme
Aşağıdaki örnek, satırları kategoriye göre gruplar ve her kategorideki en yüksek fiyata sahip satırı döndürür.
SearchQuery searchQuery = SearchQuery.newBuilder()
.query(QueryBuilders.matchAll())
.limit(0)
.addGroupBy(GroupByBuilders.groupByField(
"category_group", "category")
.size(10)
.addSubAggregation(AggregationBuilders.topRows(
"top_price")
.limit(1)
.sort(new Sort(Arrays.asList(
new FieldSort(
"price", SortOrder.DESC))))))
.build();
SearchRequest.ColumnsToGet columnsToGet =
new SearchRequest.ColumnsToGet();
columnsToGet.setColumns(Arrays.asList("category", "price"));
SearchRequest request =
new SearchRequest("example_table", "example_index", searchQuery);
request.setColumnsToGet(columnsToGet);
SearchResponse response = client.search(request);
GroupByFieldResult result = response.getGroupByResults()
.getAsGroupByFieldResult("category_group");
for (GroupByFieldResultItem item :
result.getGroupByFieldResultItems()) {
List<Row> rows = item.getSubAggregationResults()
.getAsTopRowsAggregationResult("top_price")
.getRows();
System.out.println(item.getKey() + ": " + rows);
}
Çok Alanlı Gruplama Karşılaştırması
Birden fazla alana göre gruplamak için birden fazla GroupByField yapılandırmasını iç içe yerleştirin veya doğrudan GroupByComposite kullanın. Sayfalama gereksinimlerine, yanıt yapısına ve sıralama kurallarına göre seçim yapın.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|