DoMetaQuery (Java SDK V2)
The OSS Java SDK V2 lets you call DoMetaQuery to perform aggregations or conditional queries on object metadata in a bucket.
Prerequisites
The OSS Java SDK V2 is installed. If you use Maven, add the
com.aliyun:alibabacloud-oss-v2dependency to the pom.xml file.Access credentials are configured by using environment variables. The example in this topic uses
EnvironmentVariableCredentialsProviderto read credentials from theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables.
Permissions
By default, an Alibaba Cloud account has all permissions. A RAM user or RAM role has no permissions by default. The Alibaba Cloud account or an account administrator must grant the required permissions to the RAM user or RAM role.
API | Action | Description |
DoMetaQuery |
| Performs an aggregation or conditional query on metadata. |
Sample code
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.dataprocess.OSSDataProcessClient;
import com.aliyun.sdk.service.oss2.dataprocess.OSSDataProcessClientBuilder;
import com.aliyun.sdk.service.oss2.dataprocess.models.*;
import java.util.Arrays;
import java.util.Collections;
public class DoMetaQuerySample {
public static void main(String[] args) throws Exception {
String endpoint = null;
String region = "cn-hangzhou";
String bucket = "examplebucket";
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
OSSDataProcessClientBuilder clientBuilder = OSSDataProcessClient.newBuilder()
.credentialsProvider(provider)
.region(region);
if (endpoint != null) {
clientBuilder.endpoint(endpoint);
}
try (OSSDataProcessClient client = clientBuilder.build()) {
Aggregation agg = Aggregation.newBuilder()
.field("Size")
.operation("sum")
.build();
DoMetaQueryResult result = client.doMetaQuery(
DoMetaQueryRequest.newBuilder()
.bucket(bucket)
.mode("basic")
.metaQueryBody(MetaQueryDoBody.newBuilder()
.query("{\"Field\": \"Size\", \"Value\": \"0\", \"Operation\": \"gt\"}")
.sort("Size")
.order("asc")
.maxResults(5)
.aggregations(Collections.singletonList(agg))
.build())
.build());
System.out.printf("Status code:%d, request id:%s%n",
result.statusCode(), result.requestId());
if (result.files() != null) {
for (File f : result.files()) {
System.out.printf("File: uri=%s, filename=%s, size=%d%n",
f.uri(), f.filename(), f.size());
}
}
if (result.aggregations() != null) {
for (AggregationInfo aggInfo : result.aggregations()) {
System.out.printf("Aggregation: field=%s, operation=%s, value=%s%n",
aggInfo.field(), aggInfo.operation(), aggInfo.value());
}
}
if (result.nextToken() != null) {
System.out.printf("Next token:%s%n", result.nextToken());
}
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}References
For the synchronous sample code, see DoMetaQuery.java.
For the asynchronous sample code, see DoMetaQueryAsync.java.