资讯动态

基于 Azure AI Anomaly Detector Java SDK 构建异常检测应用:AAS 技能实战指南

发布时间:2026/10/21 11:26:05 来源:尧图企业网站定制
AI 技能AI 插件人工智能【免费下载链接】agentic-awesome-skillsAAS Core is the local, agent-first control plane for complete catalog discovery, agent-owned selection, stack validation, and planning, backed by 2,600 agentic skills. Includes CLI, local MCP, catalog, plugins, and Workbench.项目地址https://gitcode.com/GitHub_Trending/an/agentic-awesome-skills点击查看免费下载导读本文围绕 agentic-awesome-skills 仓库中收录的azure-ai-anomalydetector-java技能展开系统讲解如何使用 Azure AI Anomaly Detector 官方 Java SDK 实现单变量univariate与多变量multivariate异常检测、时间序列分析与 AI 驱动的监控告警。读完本文你将掌握从 Maven 依赖引入、客户端初始化到批处理检测、流式检测、变化点检测、多变量模型训练/推理与模型管理的完整代码范式并了解该技能在 AAS 技能目录中的定位与调用边界。一、技能概览与项目定位azure-ai-anomalydetector-java是 agentic-awesome-skills 技能体系中面向 Azure 云服务的 Java 技能之一其完整定义位于 plugins/agentic-awesome-skills-claude/skills/azure-ai-anomalydetector-java/SKILL.md同时仓库根目录skills/azure-ai-anomalydetector-java/下也维护了同名技能副本。在技能目录数据中该技能被明确登记为category: cloud云服务类目risk: critical关键风险级别调用前需严格核对权限与数据边界source: community社区来源date_added: 2026-02-27插件兼容目标为codex: supported与claude: supported即 Codex 与 Claude 两类 Agent 均可按本技能执行工作流。以上元数据见 data/skills_index.json。此外data/catalog.json 为该技能生成了检索标签与触发词azure、anomaly、detection、univariate、multivariate等Agent 在收到“异常检测”“时间序列分析”“AI 监控”类任务时可据此自动匹配本技能。二、依赖安装在 Maven 项目中引入 Azure AI Anomaly Detector SDK for Javadependency groupIdcom.azure/groupId artifactIdazure-ai-anomalydetector/artifactId version3.0.0-beta.6/version /dependency要点说明该版本为当前技能文档锁定的 SDK 版本3.0.0-beta.6属于 beta 阶段API 可能随官方迭代调整请以实际引入版本生成的 API 为准若使用 Gradle等价声明为implementation com.azure:azure-ai-anomalydetector:3.0.0-beta.6若还需使用DefaultAzureCredential进行托管身份认证需额外引入com.azure:azure-identity依赖。三、客户端创建同步与异步模式SDK 通过AnomalyDetectorClientBuilder统一构建两类客户端分别对应单变量与多变量检测能力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();实现要点凭据通过AzureKeyCredential注入 API Key密钥与 Endpoint 建议通过环境变量而非硬编码提供避免凭据泄露Endpoint格式形如https://resource.cognitiveservices.azure.com/指向你在 Azure 门户中创建的 Anomaly Detector 认知服务资源构建器复用AnomalyDetectorClientBuilder可链式配置后分别构建单变量与多变量客户端也可复用在异步场景——从构建器 API 结构可以推断其还提供对应的 Async 客户端构建方法如buildMultivariateAsyncClient/buildUnivariateAsyncClient用于响应式流Reactor异步调用。使用 DefaultAzureCredential托管身份认证在 Azure 环境内如 Azure Functions、AKS、VM 托管身份可改用 Azure Identity 的默认凭据链免去显式管理 API Keyimport com.azure.identity.DefaultAzureCredentialBuilder; MultivariateClient client new AnomalyDetectorClientBuilder() .credential(new DefaultAzureCredentialBuilder().build()) .endpoint(endpoint) .buildMultivariateClient();DefaultAzureCredential会依次尝试环境变量凭据、工作负载身份、托管身份、Azure CLI 登录态等认证来源适合将异常检测能力嵌入云上生产系统。四、核心概念单变量异常检测Univariate针对单一指标的时间序列支持三种模式批处理检测Batch Detection一次性分析整条时间序列返回每个数据点是否为异常流式检测Streaming / Last Point Detection基于历史数据实时判断最新一个数据点是否异常适合持续监控场景变化点检测Change Point Detection识别时间序列中统计特性发生突变的时刻趋势变化、均值漂移等。多变量异常检测Multivariate可同时检测 300 个相互关联的信号correlated signals底层采用**图注意力网络Graph Attention Network, GAT**建模变量间相关性比逐变量独立检测更能发现系统级联合异常使用流程为经典三步训练Train→ 推理Inference→ 结果查询Results其中训练与批推理均属长时运行操作Long-Running Operation。五、核心代码模式5.1 单变量批处理检测import com.azure.ai.anomalydetector.models.*; import java.time.OffsetDateTime; import java.util.List; ListTimeSeriesPoint 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()); } }关键参数TimeSeriesPoint(timestamp, value)单点数据结构时间戳使用 ISO-8601 格式TimeGranularity.DAILY数据粒度需与真实采样频率一致如HOURLY、DAILY、MONTHLY等setSensitivity(95)敏感度取值范围 0-99值越高检出异常越多、误报风险也越高数据点下限至少 12 个点更多数据有助于提升检测精度。5.2 单变量最后点检测流式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()); }该模式复用批处理同款UnivariateDetectionOptions包含历史序列但只对最新数据点作出判定并返回期望值expected value与上/下边界upper/lower margin便于与监控阈值联动告警。5.3 变化点检测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)); } }与异常检测不同变化点检测关注“何时发生了结构性变化”返回每个位置的isChangePoint布尔标记与confidenceScores置信度分数常用于业务量突增、版本发布后的指标漂移分析。5.4 多变量模型训练多变量检测需要先基于 Azure Blob Storage 中的历史数据训练专用模型长时运行操作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());参数含义setDataSource(...)训练数据所在的 ZIP 压缩包含 CSV的 Blob URL需携带 SAS Token 鉴权setStartTime/setEndTime训练数据的起止时间窗口setSlidingWindow(200)滑动窗口大小控制用于预测的历史长度推荐 200-1000依据模式复杂度调整setDisplayName(...)模型展示名便于在模型列表中区分训练完成后必须保存modelId后续推理均以其为入参可通过getMultivariateModel(modelId)轮询训练状态。5.5 多变量批推理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()); } }要点推理同样以 Blob 上的 ZIP 数据为输入需指定推理时间窗口setTopContributorCount(10)每个异常点最多返回贡献度最高的 10 个变量用于根因定位推理任务异步执行通过返回的resultId调用getBatchDetectionResult轮询获取结果每个异常结果携带severity严重度分数与时间戳可直接驱动分级告警。5.6 多变量最后点推理实时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()); } }适用于对多指标系统做实时监控VariableValues以“变量名 时间戳列表 数值列表”三元组提交当前快照返回结果的interpretation列表给出各变量的贡献度contributionScore可快速判断哪个信号引发了系统级异常。5.7 模型管理// List all models PagedIterableAnomalyDetectionModel 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);listMultivariateModels()返回分页集合便于盘点与清理历史模型不再使用的模型应及时deleteMultivariateModel(modelId)释放额度避免资源浪费。六、错误处理所有网络与 API 调用失败均以HttpResponseException形式抛出应统一捕获并记录状态码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()); }实践中建议在此基础上补充对 429限流与 5xx服务端瞬时错误实现指数退避重试对训练/推理类长时操作增加超时与轮询上限在日志中记录modelId/resultId便于失败后追溯任务状态。七、环境变量推荐通过环境变量注入资源端点与密钥避免硬编码AZURE_ANOMALY_DETECTOR_ENDPOINThttps://resource.cognitiveservices.azure.com/ AZURE_ANOMALY_DETECTOR_API_KEYyour-api-key其中resource为 Azure 中创建的 Anomaly Detector 资源名称your-api-key为资源密钥可在 Azure 门户“密钥和终结点”页获取。八、最佳实践清单最少数据点单变量检测至少需要 12 个数据点数据越多、覆盖周期越完整检测精度越高粒度对齐TimeGranularity必须与实际数据采样频率一致错配会显著降低检测效果敏感度调优Sensitivity取值范围 0-99值越高检出异常越多建议结合误报容忍度做交叉验证调参多变量滑动窗口按模式复杂度选择 200-1000 的slidingWindow窗口过短难以捕捉长周期关联过长则对实时性不友好错误处理所有 API 调用务必捕获HttpResponseException并对限流与瞬时错误做重试凭据安全优先使用DefaultAzureCredentialAzure 内托管身份或环境变量注入 Key禁止将密钥写入代码仓库。九、触发短语与适用场景本技能在 Agent 协作中由以下触发短语激活anomaly detection Javadetect anomalies time seriesmultivariate anomaly Javaunivariate anomaly detectionstreaming anomaly detectionchange point detectionAzure AI Anomaly Detector对应的典型使用场景包括运维指标监控CPU/内存/流量联合异常、业务关键指标交易量、转化率突变检测、IoT 传感器时间序列分析以及云资源用量与成本的智能告警。这些触发词与 data/catalog.json 中登记的triggers保持一致Agent 可据此在技能目录中自动检索与路由。十、使用边界与限制作用域限制仅在任务明确匹配上述异常检测范围时使用本技能不要将其扩展到无关的通用编码任务结果性质检测输出不能替代针对具体环境的验证、测试或专家评审尤其是生产告警阈值与误报评估输入前提当缺少必要输入如 Endpoint、密钥、训练数据、时间窗口、权限或安全边界不明确、成功标准缺失时应停下来向用户澄清而非擅自假设后执行。结语azure-ai-anomalydetector-java技能为 Java 开发者提供了一条从“接入 SDK”到“训练多变量模型并实时告警”的完整路径。以本技能文档为骨架、以 data/skills_index.json 与 data/catalog.json 中的目录元数据为佐证你可以将单变量批处理/流式检测、变化点检测、多变量训练-推理闭环直接落地为可靠的 AI 监控组件在使用时注意锁定 SDK 版本、对齐数据粒度、调优敏感度并为长时任务设计轮询与重试机制即可在生产环境中稳定运行。赞分享AI 技能AI 插件人工智能【免费下载链接】agentic-awesome-skillsAAS Core is the local, agent-first control plane for complete catalog discovery, agent-owned selection, stack validation, and planning, backed by 2,600 agentic skills. Includes CLI, local MCP, catalog, plugins, and Workbench.项目地址https://gitcode.com/GitHub_Trending/an/agentic-awesome-skills点击查看免费下载相关推荐基于 AAS 技能库使用 Azure AI VoiceLive Java SDK 构建实时双向语音会话基于 AAS 技能库使用 Azure AI VoiceLive Java SDK 构建实时双向语音会话 本技术指南以 agentic awesome skillAI 技能AI 插件人工智能基于 AAS 技能库的 azure/ai-voicelive TypeScript 实时语音 AI 应用开发实战指南基于 AAS 技能库的 azure/ai voicelive TypeScript 实时语音 AI 应用开发实战指南 导读本文以 AASagentic aAI 技能AI 插件人工智能基于 AAS 技能库掌握 Azure AI Projects Java SDKAzure AI Foundry 项目级资源管理实战指南基于 AAS 技能库掌握 Azure AI Projects Java SDKAzure AI Foundry 项目级资源管理实战指南 本指南以 AASAgAI 技能AI 插件人工智能上一篇Return YouTube Dislike 核心机制 FAQ 全解析数据来源、缓存更新与隐私安全下一篇Skill Seekers 文档质量审计实战版本漂移、失效链接与 CLI 文档陈旧问题的系统排查创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑