This topic describes how to call the GetHistograms operation of Log Service SDK for Java to query the distribution of logs within a specific time range and provides sample code.
Prerequisites
- Log Service is activated. For more information, see Activate Log Service.
- An AccessKey pair is created and obtained. For more information, see AccessKey pair.
An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you log on as a RAM user that has permissions to call API operations or perform routine O&M tasks. Make sure that the RAM user is granted the permissions to manage Log Service resources. For more information, see Create a RAM user and authorize the RAM user to access Log Service.
- A Java development environment is installed.
Log Service SDK for Java supports Java runtime environment (JRE) 6.0 or later. You can run the java -version command to check the version of your Java development environment. You can download the installation package from the Java official website, and then install the Java development environment.
- Log Service SDK for Java is installed. For more information, see Install Log Service SDK for Java.
- A project is created. For more information, see Sample code that is used to create a project.
- Logs are written to a Logstore and indexing is enabled. For more information, see Sample code that is used to create a Logstore and Create indexes.
Usage notes
In this example, the public Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com
. If you want to access a project by using other Alibaba Cloud services in the same region as Log Service, use an internal endpoint in the following format: https://cn-hangzhou-intranet.log.aliyuncs.com
. For information about the mappings between the supported regions and endpoints in Log Service, see Endpoints.
Raw log
body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.103.10
remote_addr:203.0.103.10
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66
Sample code
The following sample code provides an example on how to query the number of visits from 47.100.XX.XX within an hour:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Histogram;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetHistogramsResponse;
public class GetHistograms {
public static void main(String[] args) throws LogException {
// An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you log on as a RAM user that has permissions to call API operations or perform routine O&M tasks.
String accessId = "yourAccessKeyId";
String accessKey = "yourAccessKeySecret";
// The name of the project.
String projectName = "ali-test-project";
// The Log Service endpoint. In this example, the Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with an actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// The name of the Logstore.
String logstoreName = "ali-test-logstore";
// Query the number of visits from 47.100.XX.XX within an hour.
String query = "remote_addr:47.100.XX.XX";
System.out.println(String.format("ready to get histograms from %s",logstoreName));
// The fromTime and toTime variables specify the start time and end time of a time range within which you want to query log data. The values of the variables are UNIX timestamps. Set the time range to 1 hour.
int fromTime = (int) (System.currentTimeMillis()/1000 - 3600);
int toTime = fromTime + 3600;
GetHistogramsResponse response = client.GetHistograms(projectName,logstoreName,fromTime,toTime,"",query);
for (Histogram histogram : response.GetHistograms()) {
// Only the interval within which logs are distributed can be returned.
if (0 < histogram.GetCount()){
// The number of logs that are returned.
System.out.println("log number is :" + histogram.GetCount());
// The start time of the subinterval.
System.out.println("from time is :" + histogram.GetFrom());
// The end time of the subinterval.
System.out.println("to time is :" + histogram.GetTo());
// Specifies whether the query result in the subinterval is complete.
System.out.println("is completed :" + histogram.IsCompleted());
}
}
System.out.println(String.format("get histograms from %s success",logstoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
The following response shows the expected output. The query result indicates that the number of visits from 47.100.XX.XX within 1 hour is 10, and the query result is complete.
ready to get histograms from nginx-moni
log number is :10
from time is :1667875920
to time is :1667875980
is completed :true
get histograms from nginx-moni success
References
- Alibaba Cloud OpenAPI Explorer provides debugging capabilities, SDKs, examples, and related documents. You can use OpenAPI Explorer to debug Log Service API operations without the need to manually encapsulate and sign requests. For more information, visit OpenAPI Explorer.
- Log Service provides the command-line interface (CLI) to meet the requirements for automated configurations in Log Service. For more information, see Log Service CLI.
- For more information about the GetHistograms operation, see GetHistograms.
- For more information about sample code, see Alibaba Cloud Log Service SDK for Java on GitHub.