重点技术-20170713-阿里云-日志服务-实时查询LogSearch

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/namelessfighter/article/details/80547342

实时消费LogHub与实时查询LogSearch区别

功能 日志查询(LogSearch) 日志收集与消费(LogHub)
关键词查找 支持 不支持
小量数据读取
全量数据读取 慢(100条日志100ms,不建议这样使用) 快 (1MB日志10ms,推荐方式)
读取是否区分topic 区分 不区分,只以shard作为标识
读取是否区分shard 不区分,查询所有shard 区分,单次读取需要指定shard
费用 较高
适用场景 监控、问题调查等需要过滤数据的场景 流式计算、批量处理等全量处理场景




---------Maven依赖
---------
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>aliyun-log</artifactId>
<version>0.6.6</version>
</dependency>
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>loghub-client-lib</artifactId>
<version>0.6.5</version>
</dependency>

---------JAVA代码 ---------
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import aliyun.S_AliyunAccount;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Histogram;
import com.aliyun.openservices.log.common.LogContent;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetHistogramsRequest;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetHistogramsResponse;
import com.aliyun.openservices.log.response.GetLogsResponse;

public class LogSearch
{
public static void main(String args[]) throws LogException, InterruptedException
{
String endpoint = S_AliyunAccount.LogEndpoint;
String accessKeyId = S_AliyunAccount.AccessKeyId;
String accessKeySecret = S_AliyunAccount.AccessSecretKey;
String project = S_AliyunAccount.LogProject;
String logstore = S_AliyunAccount.LogStore;

Client client = new Client(endpoint, accessKeyId, accessKeySecret);

// 查询日志分布情况
String query = "location=LogPushTonyYang and level=17* and (__topic__:testtopic or __topic__:aa)";
String dateString = "2017-07-01";
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
date = sdf.parse(dateString);
}
catch (ParseException e)
{
e.printStackTrace();
}
int from = (int) (date.getTime() / 1000);
int to = (int) (new Date().getTime() / 1000);
GetHistogramsResponse res3 = null;
String topic = "testtopic";// 当topic为null或""时,topic不起作用
while (true)
{
GetHistogramsRequest req3 = new GetHistogramsRequest(project, logstore, topic, query, from, to);
res3 = client.GetHistograms(req3);
if (res3 != null && res3.IsCompleted()) // IsCompleted() 返回值
// true,表示查询结果是准确的
// false,则查询失败,重试
{
break;
}
Thread.sleep(200);
}
System.out.println("Total count of logs is " + res3.GetTotalCount());
//for (Histogram ht : res3.GetHistograms())
//{
// //搜索到的日志,在各时间段内的分布情况
// System.out.printf("from %d, to %d, count %d.\n", ht.GetFrom(), ht.GetTo(), ht.GetCount());
//}
// 查询具体日志数据
long total_log_lines = res3.GetTotalCount();
System.out.println("total_log_lines=" + total_log_lines);
int log_offset = 0;
int log_line = 100;//每次读取日志条数
while (log_offset <= total_log_lines)
{
GetLogsResponse res4 = null;
for (int retry_time = 0; retry_time < 3; retry_time++)
{
// 如果读取失败,最多重试 3 次
GetLogsRequest req4 = new GetLogsRequest(project, logstore, from, to, topic, query,
log_offset, log_line, false);

res4 = client.GetLogs(req4);
if (res4 != null && res4.IsCompleted())
{
break;
}
Thread.sleep(200);
}
for (QueriedLog log : res4.GetLogs())
{
StringBuilder content = new StringBuilder();
content.append(log.GetLogItem().GetTime() + "\t");
for (LogContent cont : log.GetLogItem().GetLogContents())
{
content.append(cont.GetKey() + "=" + cont.GetValue() + "\t");
}
System.out.println(content.toString());
}
log_offset += log_line;
}
}
}

猜你喜欢

转载自blog.csdn.net/namelessfighter/article/details/80547342