关于 azure-ai-anomalydetector-java
面向 Java 开发者的 Azure AI Anomaly Detector SDK 参考,涵盖单变量和多变量时间序列检测。内容将服务视为历史及迁移参考,不适用于新建云部署。
主要功能
- 01单变量批量检测
- 02最新数据点异常检测
- 03变点检测
- 04多变量模型训练与推理
- 05模型列表及状态查询
适用场景
- 01分析历史时间序列数据
- 02检查新数据读数中的异常
- 03识别时间序列趋势变化
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-ai-anomalydetector-java安装后在 Claude Code 中输入/azure-ai-anomalydetector-java确认已加载。
详细介绍
Azure AI Anomaly Detector SDK for Java
使用 Azure AI Anomaly Detector SDK for Java 构建异常检测应用程序。
服务状态
Microsoft 的退役指南指出,已于 2023 年 9 月禁用新资源,并计划于 2026 年 10 月退役该服务。请将这些 SDK 片段视为历史/迁移参考,而非用于新云部署的方案。实施前,请核实实际资源生命周期,并评估文档中提到的 Fabric/开源替代方案。
代码片段省略了外围应用程序代码和导入语句,并非经过测试的完整程序。对于异步训练/推理,请在截止时间前轮询终止状态,并在访问结果前处理失败情况。请使用经批准的数据、有效期短且遵循最小权限原则的存储访问方式,并保护报告的私密性;切勿泄露 SAS 令牌。
安装
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-anomalydetector</artifactId>
<version>3.0.0-beta.6</version>
</dependency>
创建客户端
同步和异步客户端
import com.azure.ai.anomalydetector.AnomalyDetectorClientBuilder;
import com.azure.ai.anomalydetector.MultivariateClient;
import com.azure.ai.anomalydetector.UnivariateClient;
import com.azure.core.credential.AzureKeyCredential;
String endpoint = System.getenv("AZURE_ANOMALY_DETECTOR_ENDPOINT");
String key = System.getenv("AZURE_ANOMALY_DETECTOR_API_KEY");
// Multivariate client for multiple correlated signals
MultivariateClient multivariateClient = new AnomalyDetectorClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildMultivariateClient();
// Univariate client for single variable analysis
UnivariateClient univariateClient = new AnomalyDetectorClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildUnivariateClient();
使用 DefaultAzureCredential
import com.azure.identity.DefaultAzureCredentialBuilder;
MultivariateClient client = new AnomalyDetectorClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(endpoint)
.buildMultivariateClient();
核心概念
单变量异常检测
- 批量检测:一次性分析整个时间序列
- 流式检测:实时检测最新数据点
- 变点检测:检测时间序列中的趋势变化
多变量异常检测
- 检测 300 多个相关信号中的异常
- 使用图注意力网络处理信号间的相关性
- 三步流程:训练 → 推理 → 结果
核心模式
单变量批量检测
import com.azure.ai.anomalydetector.models.*;
import java.time.OffsetDateTime;
import java.util.List;
List<TimeSeriesPoint> series = List.of(
new TimeSeriesPoint(OffsetDateTime.parse("2023-01-01T00:00:00Z"), 1.0),
new TimeSeriesPoint(OffsetDateTime.parse("2023-01-02T00:00:00Z"), 2.5)
// ... more data points (minimum 12 points required)
);
UnivariateDetectionOptions options = new UnivariateDetectionOptions(series)
.setGranularity(TimeGranularity.DAILY)
.setSensitivity(95);
UnivariateEntireDetectionResult result = univariateClient.detectUnivariateEntireSeries(options);
// Check for anomalies
for (int i = 0; i < result.getIsAnomaly().size(); i++) {
if (result.getIsAnomaly().get(i)) {
System.out.printf("Anomaly detected at index %d with value %.2f%n",
i, series.get(i).getValue());
}
}
单变量最新数据点检测(流式)
UnivariateLastDetectionResult lastResult = univariateClient.detectUnivariateLastPoint(options);
if (lastResult.isAnomaly()) {
System.out.println("Latest point is an anomaly!");
System.out.printf("Expected: %.2f, Upper: %.2f, Lower: %.2f%n",
lastResult.getExpectedValue(),
lastResult.getUpperMargin(),
lastResult.getLowerMargin());
}
变点检测
UnivariateChangePointDetectionOptions changeOptions =
new UnivariateChangePointDetectionOptions(series, TimeGranularity.DAILY);
UnivariateChangePointDetectionResult changeResult =
univariateClient.detectUnivariateChangePoint(changeOptions);
for (int i = 0; i < changeResult.getIsChangePoint().size(); i++) {
if (changeResult.getIsChangePoint().get(i)) {
System.out.printf("Change point at index %d with confidence %.2f%n",
i, changeResult.getConfidenceScores().get(i));
}
}
多变量模型训练
import com.azure.ai.anomalydetector.models.*;
import com.azure.core.util.polling.SyncPoller;
// Prepare training request with blob storage data
ModelInfo modelInfo = new ModelInfo()
.setDataSource("https://storage.blob.core.windows.net/container/data.zip?sasToken")
.setStartTime(OffsetDateTime.parse("2023-01-01T00:00:00Z"))
.setEndTime(OffsetDateTime.parse("2023-06-01T00:00:00Z"))
.setSlidingWindow(200)
.setDisplayName("MyMultivariateModel");
// Train model (long-running operation)
AnomalyDetectionModel trainedModel = multivariateClient.trainMultivariateModel(modelInfo);
String modelId = trainedModel.getModelId();
System.out.println("Model ID: " + modelId);
// Check training status
AnomalyDetectionModel model = multivariateClient.getMultivariateModel(modelId);
System.out.println("Status: " + model.getModelInfo().getStatus());
多变量批量推理
MultivariateBatchDetectionOptions detectionOptions = new MultivariateBatchDetectionOptions()
.setDataSource("https://storage.blob.core.windows.net/container/inference-data.zip?sasToken")
.setStartTime(OffsetDateTime.parse("2023-07-01T00:00:00Z"))
.setEndTime(OffsetDateTime.parse("2023-07-31T00:00:00Z"))
.setTopContributorCount(10);
MultivariateDetectionResult detectionResult =
multivariateClient.detectMultivariateBatchAnomaly(modelId, detectionOptions);
String resultId = detectionResult.getResultId();
// Poll for results
MultivariateDetectionResult result = multivariateClient.getBatchDetectionResult(resultId);
for (AnomalyState state : result.getResults()) {
if (state.getValue().isAnomaly()) {
System.out.printf("Anomaly at %s, severity: %.2f%n",
state.getTimestamp(),
state.getValue().getSeverity());
}
}
多变量最新数据点检测
MultivariateLastDetectionOptions lastOptions = new MultivariateLastDetectionOptions()
.setVariables(List.of(
new VariableValues("variable1", List.of("timestamp1"), List.of(1.0f)),
new VariableValues("variable2", List.of("timestamp1"), List.of(2.5f))
))
.setTopContributorCount(5);
MultivariateLastDetectionResult lastResult =
multivariateClient.detectMultivariateLastAnomaly(modelId, lastOptions);
if (lastResult.getValue().isAnomaly()) {
System.out.println("Anomaly detected!");
// Check contributing variables
for (AnomalyContributor contributor : lastResult.getValue().getInterpretation()) {
System.out.printf("Variable: %s, Contribution: %.2f%n",
contributor.getVariable(),
contributor.getContributionScore());
}
}
模型管理
// List all models
PagedIterable<AnomalyDetectionModel> models = multivariateClient.listMultivariateModels();
for (AnomalyDetectionModel m : models) {
System.out.printf("Model: %s, Status: %s%n",
m.getModelId(),
m.getModelInfo().getStatus());
}
// Delete a model
multivariateClient.deleteMultivariateModel(modelId);
错误处理
import com.azure.core.exception.HttpResponseException;
try {
univariateClient.detectUnivariateEntireSeries(options);
} catch (HttpResponseException e) {
System.out.println("Status code: " + e.getResponse().getStatusCode());
System.out.println("Error: " + e.getMessage());
}
环境变量
AZURE_ANOMALY_DETECTOR_ENDPOINT=https://<resource>.cognitiveservices.azure.com/
AZURE_ANOMALY_DETECTOR_API_KEY=<your-api-key>
最佳实践
- 最少数据点:单变量检测至少需要 12 个数据点;更多数据可提高准确性
- 粒度匹配:让
TimeGranularity与实际数据频率保持一致 - 灵敏度调优:较高的值(0-99)可检测到更多异常
- 多变量训练:根据模式复杂度使用 200-1000 的滑动窗口
- 错误处理:始终处理 API 错误的
HttpResponseException
触发短语
- “Java 异常检测”
- “检测时间序列异常”
- “Java 多变量异常”
- “单变量异常检测”
- “流式异常检测”
- “变点检测”
- “Azure AI Anomaly Detector”
适用场景
当任务涉及执行概述中描述的工作流或操作时,可使用此技能。
限制
- 仅当任务与上述范围明确匹配时,才使用此技能。
- 请勿将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少必需的输入、权限、安全边界或成功标准,请暂停并请求澄清。
评论
支持 Markdown