Toplanan günlükleri SLS Java SDK kullanarak sorgulamak için GetLogs API'sini çağırın.
Ön koşullar
Simple Log Service etkinleştirilmiştir. Daha fazla bilgi için bkz. Simple Log Service'i Etkinleştir.
Simple Log Service SDK for Java başlatılmıştır. Daha fazla bilgi için bkz. Simple Log Service SDK for Java'yı Başlatma.
Kullanım notları
-
Bu örnekte Çin (Hangzhou) bölgesi için Simple Log Service genel uç noktası kullanılmaktadır. Uç nokta:
https://cn-hangzhou.log.aliyuncs.com.Projenizle aynı bölgede bulunan diğer Alibaba Cloud hizmetlerinden Simple Log Service'e erişmek istiyorsanız iç Simple Log Service uç noktasını kullanabilirsiniz. Bu uç nokta:
https://cn-hangzhou-intranet.log.aliyuncs.com.Simple Log Service tarafından desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. Uç Nokta.
-
Sonuç kümesinin tam olup olmadığını kontrol etmek için SDK yanıt nesnesinde
IsCompleted()yöntemini çağırın.true: Sorgu başarılı oldu ve sonuç kümesi tamdır.false: Sorgu başarılı oldu ancak sonuç kümesi eksiktir. Tam sonuçları almak için isteği tekrarlayın. Sorguların yanlış sonuçlanmasının olası nedenleri.
Parametreler
İstek parametreleri
Ad | Tür | Gerekli | Açıklama |
project | String | Evet | Projenin adı. Simple Log Service'teki proje, farklı kullanıcıların kaynaklarını izole etmek ve belirli kaynaklara erişimi kontrol etmek için kullanılır. Bkz. Projeleri Yönetme. |
logstore | String | Evet | Günlük deposunun adı. Simple Log Service'teki günlük deposu, günlükleri toplamak, depolamak ve sorgulamak için kullanılır. Bkz. Günlük Depolarını Yönetme. |
from | int | Evet | Sorgulanacak zaman aralığının başlangıcı. Değer bir UNIX zaman damgasıdır. Not
|
to | int | Evet | Sorgulanacak zaman aralığının bitişi. Değer bir UNIX zaman damgasıdır. Not
|
topic | String | Hayır | Günlüklerin konusu. Varsayılan değer boş bir dizedir. Daha fazla bilgi için bkz. Günlük Konuları. |
query | String | Hayır | Bir arama veya analiz ifadesi. Sorgulama ve Analize Genel Bakış. SQL Exclusive kullanmak için analiz ifadesine Not query bir analiz ifadesi (SQL) içeriyorsa line ve offset parametreleri geçerli değildir. Bunları 0 olarak ayarlayın ve sayfalama için LIMIT yan tümcesini kullanın. Sorgu ve analiz sonuçlarını sayfalandırma. |
line | int | Hayır | Yalnızca arama ifadeleri için geçerlidir. Döndürülecek maksimum günlük sayısı. Aralık: 0 ila 100. Varsayılan: 100. |
offset | int | Hayır | Yalnızca arama ifadeleri için geçerlidir. Sorgunun başlangıç satırı. Varsayılan: 0. |
reverse | boolean | Hayır | Günlüklerin zaman damgasına göre azalan sırada döndürülüp döndürülmeyeceği. Hassasiyet: dakika düzeyi.
Önemli
|
powerSql | boolean | Hayır | Dedicated SQL kullanılıp kullanılmayacağı. Yüksek performanslı ve doğru sorgulama ve analiz (Dedicated SQL).
Yapılandırmayı query parametresine ekleyerek Dedicated SQL'yi de etkinleştirebilirsiniz. |
scan | boolean | Hayır | Sorgu için tarama modunun kullanılıp kullanılmayacağı. |
forward | boolean | Hayır | Yalnızca tarama sorguları için. true: sonraki sayfayı alır. false: önceki sayfayı alır. |
Yanıt parametreleri
Ham günlük
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.XX.XX
remote_addr:203.0.XX.XX
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
Sorgulama ve analiz örnekleri
Aşağıdaki Java örnekleri yaygın sorgu kalıplarını göstermektedir.
SLS Java SDK ile GetLogs'u çağırırken:
queryyalnızca bir arama ifadesi içeriyorsa (ör.request_method:POST),lineparametresi döndürülen günlük sayısını kontrol eder (maksimum 100). 100'den fazla günlük döndürmek için SQL'de LIMIT yan tümcesini kullanın. LIMIT yan tümcesi.querybir sorgulama ve analiz ifadesi içeriyorsa (ör.request_method:POST | SELECT host, COUNT(*) AS pv GROUP BY host LIMIT 5),lineparametresi yoksayılır. Satır sayısını kontrol etmek için SQL'de LIMIT yan tümcesini kullanın. LIMIT yan tümcesi.
Örnek 1: Anahtar sözcük kullanarak günlükleri sorgulama
path-0/file-5 anahtar sözcüğünü kullanarak günlükleri sorgulayın. line parametresi sonuçları 3 günlük ile sınırlar.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Use the keyword "path-0/file-5" to query logs.
String query = "path-0/file-5";
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is used to set the query statement. The line parameter is used to control the number of returned logs. The value is 3, and the maximum value is 100.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Yanıt:
-------------Query is started.-------------
Returned query result count :3
from time is :1644573549
to time is :1644573849
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573808
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
log time : 1644573788
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","request_uri":"/request/path-0/file-5"...}
-------------Query is finished.-------------
Process finished with exit code 0
Örnek 2: Alan belirterek günlükleri sorgulama
İstek yönteminin POST olduğu günlükleri sorgulayın. line parametresi sonuçları 3 günlük ile sınırlar.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST.
String query = "request_method:POST";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is used to set the query statement. The line parameter is used to control the number of returned logs. The value is 3, and the maximum value is 100.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Yanıt:
-------------Query is started.-------------
Returned query result count :3
from time is :1644574151
to time is :1644574451
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3604","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"3369","request_method":"POST"...}
log time : 1644574438
Jsonstring : {"remote_addr":"203.0.XX.XX","__topic__":"nginx_access_log","body_bytes_sent":"12714","request_method":"POST"...}
-------------Query is finished.-------------
Process finished with exit code 0
Örnek 3: SQL ifadesi kullanarak günlükleri analiz etme
İstek yönteminin POST olduğu günlükleri sorgulayın ve sayfa görüntüleme sayılarını (PV) sayın.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST and count the number of PVs for the POST requests.
String query = "request_method:POST|select COUNT(*) as pv";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is set to a query and analysis statement. The line parameter is invalid.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Yanıt:
-------------Query is started.-------------
Returned query result count :1
from time is :1644574354
to time is :1644574654
log time : 1644574354
Jsonstring : {"pv":"162","logtime":1644574354}
-------------Query is finished.-------------
Process finished with exit code 0
Örnek 4: GROUP BY yan tümcesini kullanarak günlükleri analiz etme
İstek yönteminin POST olduğu günlükleri sorgulayın ve sonuçları ana bilgisayara göre gruplandırın.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST and group the results by host.
// Use the LIMIT clause in the SQL syntax to limit the number of returned rows to 5.
String query = "request_method:POST|select host, COUNT(*) as pv group by host limit 5";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is set to a query and analysis statement. The line parameter is invalid. The number of rows to return is determined by the LIMIT clause in the query.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Yanıt:
-------------Query is started.-------------
Returned query result count :5
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
-------------Query is finished.-------------
Process finished with exit code 0
Örnek 5: GROUP BY yan tümcesini kullanarak günlükleri analiz etme (200 günlük döndürüldü)
İstek yönteminin POST olduğu günlükleri sorgulayın, ana bilgisayara göre gruplandırın ve LIMIT yan tümcesini kullanarak en fazla 200 satır döndürün.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query for logs where the request method is POST and group the results by host.
// Use the LIMIT clause in the SQL syntax to control the number of rows to return.
String query = "request_method:POST|select host, COUNT(*) as pv group by host limit 0,200";
// Set the time range for the query.
int from = 1754449503;
int to = 1754449510;
// In this example, the query parameter is set to a query and analysis statement. The line parameter is invalid. The number of rows to return is determined by the LIMIT clause in the query.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Yanıt:
-------------Query is started.-------------
Returned query result count :200
from time is :1644574445
to time is :1644574745
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example1.com","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.org","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.net","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.example.edu","logtime":1644574445}
log time : 1644574445
Jsonstring : {"pv":"1","host":"www.aliyundoc.com","logtime":1644574445}
......
-------------Query is finished.-------------
Process finished with exit code 0
Örnek 6: SQL ifadesi kullanarak önceki saatteki toplam günlük sayısını sorgulama
Son bir saatten toplam günlük sayısını almak için *|select count(*) as count kullanın.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
import java.util.Date;
public class GetLogsTest {
public static void main(String[] args) throws LogException {
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String project = "your-project-name";
// Set the endpoint for Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "your-logstore-name";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
// Execute a query in the specified Logstore.
try {
// Query the total number of logs.
String query = "*|select count(*) as count";
// Set the query time range to 1 hour (3,600 seconds).
int from = (int) (new Date().getTime() / 1000 - 3600);
int to = (int) (new Date().getTime() / 1000);
int offset = 0;
int line = 200;
// In this example, the SQL statement in the query parameter is used to query the total number of logs in the specified time range. The line and offset parameters are invalid for SQL queries.
GetLogsRequest request = new GetLogsRequest(project, logStore, from, to, "", query,line,offset,true);
GetLogsResponse logsResponse = client.GetLogs(request);
System.out.println("-------------Query is started.-------------");
System.out.println("Returned query result count :" + logsResponse.GetCount());
System.out.println("from time is :" + from);
System.out.println("to time is :" + to);
for (QueriedLog log : logsResponse.getLogs()) {
LogItem item = log.GetLogItem();
System.out.println("log time : " + item.mLogTime);
System.out.println("Jsonstring : " + item.ToJsonString());
}
System.out.println("-------------Query is finished.-------------");
} 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;
}
}
}
Önceki saatteki toplam günlük sayısı 19.051'dir. Yanıt:
from time is :1675041679
to time is :1675045279
Returned sql result count :1
Jsonstring : {"count":"19051","logtime":1675041679}
-------------Query is finished.-------------
Başvurular
SLS, yerel SDK'sına ek olarak ortak Alibaba Cloud SDK'larını da destekler. Daha fazla bilgi için bkz. Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.
GitHub'daki Alibaba Cloud Simple Log Service SDK for Java.