Apache Any23 快速入门与集成示例
Apache Any23 2.7 快速入门手册:从 HTML 样例构造、rover 默认抽取到 N-Triples/N-Quads/Turtle/JSON-LD 格式切换,再到指定提取器与 Java 库集成示例,全程实测回显,十分钟上手。
Apache Any23 快速入门与集成示例
目标:用最短路径掌握 Apache Any23 2.7 的三种使用形态——CLI 抽取、输出格式切换与 Java 库集成,通过一个包含 RDFa、Microdata 与 JSON-LD 的 HTML 样例完整走通"页面到三元组"流程,所有回显均为真实环境实测。
一、环境信息
| 项目 | 值 |
|---|---|
| 操作系统 | Ubuntu 22.04.5 LTS |
| Any23 版本 | Apache Any23 2.7(最终正式发布版) |
| Java 版本 | OpenJDK 11.0.32.1 |
| 安装目录 | /opt/any23-2.7 |
| 样例目录 | /opt/any23-demo |
| 执行账号 | root |
二、版本与生命周期说明
- Apache Any23 于 2023 年 7 月进入 Apache Attic,项目停止维护(EOL),本文以 2.7 最终正式版实测。
- 2.8 仅停留在 SNAPSHOT 阶段从未发布正式版本,集成时以 2.7 为准。
💡 技术拓展:Any23 已停止维护,若项目仍需要"从 HTML/文档抽取结构化元数据"的能力,社区建议评估 Apache Tika(tika.apache.org),其提供更完整的文档解析、元数据提取与 REST 服务,维护活跃;Maven 依赖方面也可关注 Apache Jena(jena.apache.org)提供原生 RDF 处理能力。
三、准备样例数据
构造一个同时包含三种语义标注的 HTML 页面:head 区 meta、JSON-LD 脚本、body 区 RDFa 与 Microdata:
mkdir -p /opt/any23-demo && cat > /opt/any23-demo/sample.html <<'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>Any23 实测示例页面</title>
<meta name="author" content="Apache Any23 Team">
<meta name="keywords" content="RDF, metadata, extraction">
<link rel="license" href="http://www.apache.org/licenses/LICENSE-2.0">
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"name": "Apache Software Foundation",
"url": "http://apache.org"
}
</script>
</head>
<body>
<div vocab="http://schema.org/" typeof="Book">
<span property="name">Anything To Triples</span>
<span property="author">Apache Any23</span>
</div>
<div itemscope itemtype="http://schema.org/Person">
<span itemprop="name">Alice</span>
</div>
</body>
</html>
EOF
验证结果:样例文件创建完成,后续所有命令均以该文件为输入。
四、CLI 快速入门
4.1 默认抽取(N-Triples)
直接执行 rover,输出默认格式 N-Triples:
any23 rover /opt/any23-demo/sample.html
------------------------------------------------------------------------
Apache Any23 :: rover
------------------------------------------------------------------------
<file:/opt/any23-demo/sample.html> <http://vocab.sindice.net/any23#author> "Apache Any23 Team"@zh-CN .
<file:/opt/any23-demo/sample.html> <http://vocab.sindice.net/any23#keywords> "RDF, metadata, extraction"@zh-CN .
<file:/opt/any23-demo/sample.html> <http://purl.org/dc/terms/title> "Any23 实测示例页面" .
_:node1k4r135pex2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
_:node1k4r135pex2 <http://schema.org/name> "Alice"@zh-CN .
<file:/opt/any23-demo/sample.html> <http://www.w3.org/1999/xhtml/microdata#item> _:node1k4r135pex2 .
<file:/opt/any23-demo/sample.html> <http://www.w3.org/1999/xhtml/vocab#license> <http://www.apache.org/licenses/LICENSE-2.0> .
<file:/opt/any23-demo/sample.html> <http://www.w3.org/ns/rdfa#usesVocabulary> <http://schema.org/> .
_:n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> .
_:n0 <http://schema.org/name> "Anything To Triples"@zh-CN .
_:n0 <http://schema.org/author> "Apache Any23"@zh-CN .
------------------------------------------------------------------------
Apache Any23 SUCCESS
Total time: 127s
Finished at: Tue Oct 13 16:23:32 GMT 2026
Final Memory: 79M/506M
------------------------------------------------------------------------
验证结果:默认模式产出 10 条 N-Triples,覆盖 meta(author、keywords)、title、Microdata(Person)、RDFa(Book)与 license 链接,其中 JSON-LD 的 Organization 因远程上下文解析超时未产出(详见 5.1 节)。
4.2 指定提取器快速抽取
收敛提取器集合后秒级完成(实测 1 秒):
any23 rover -e html-head-title,html-head-meta,html-rdfa11,html-microdata /opt/any23-demo/sample.html
------------------------------------------------------------------------
Apache Any23 :: rover
------------------------------------------------------------------------
<file:/opt/any23-demo/sample.html> <http://vocab.sindice.net/any23#author> "Apache Any23 Team"@zh-CN .
<file:/opt/any23-demo/sample.html> <http://vocab.sindice.net/any23#keywords> "RDF, metadata, extraction"@zh-CN .
<file:/opt/any23-demo/sample.html> <http://purl.org/dc/terms/title> "Any23 实测示例页面" .
_:node1k4r17uvtx2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
_:node1k4r17uvtx2 <http://schema.org/name> "Alice"@zh-CN .
<file:/opt/any23-demo/sample.html> <http://www.w3.org/1999/xhtml/microdata#item> _:node1k4r17uvtx2 .
<file:/opt/any23-demo/sample.html> <http://www.w3.org/1999/xhtml/vocab#license> <http://www.apache.org/licenses/LICENSE-2.0> .
<file:/opt/any23-demo/sample.html> <http://www.w3.org/ns/rdfa#usesVocabulary> <http://schema.org/> .
_:n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> .
_:n0 <http://schema.org/name> "Anything To Triples"@zh-CN .
_:n0 <http://schema.org/author> "Apache Any23"@zh-CN .
------------------------------------------------------------------------
Apache Any23 SUCCESS
Total time: 1s
Finished at: Tue Oct 13 16:29:05 GMT 2026
Final Memory: 62M/506M
------------------------------------------------------------------------
验证结果:指定 4 个提取器产出与默认模式一致的 10 条三元组,耗时从 127 秒降至 1 秒,强烈建议日常使用 -e 收敛。
五、输出格式切换
rover 的 -f 支持 8 种写入器:json、jsonld、nquads、notrivial、ntriples、trix、turtle、uri。以下实测三种代表性格式。
5.1 Turtle 输出
any23 rover -e html-head-title,html-rdfa11 -f turtle /opt/any23-demo/sample.html
------------------------------------------------------------------------
Apache Any23 :: rover
------------------------------------------------------------------------
@prefix sindice: <http://vocab.sindice.net/> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix doac: <http://ramonantonio.net/doac/0.1/#> .
@prefix rdf: <https://www.w3.org/TR/REC-rdf-syntax#> .
@prefix foaf: <http://xmlns.com/foaf/spec/> .
<file:/opt/any23-demo/sample.html> <http://vocab.sindice.net/any23#author> "Apache Any23 Team"@zh-CN;
<http://vocab.sindice.net/any23#keywords> "RDF, metadata, extraction"@zh-CN;
dcterms:title "Any23 实测示例页面";
<http://www.w3.org/1999/xhtml/microdata#item> _:node1k4r17uvtx2;
<http://www.w3.org/1999/xhtml/vocab#license> <http://www.apache.org/licenses/LICENSE-2.0>;
<http://www.w3.org/ns/rdfa#usesVocabulary> <http://schema.org/> .
_:node1k4r17uvtx2 a <http://schema.org/Person>;
<http://schema.org/name> "Alice"@zh-CN .
_:n0 a <http://schema.org/Book>;
<http://schema.org/name> "Anything To Triples"@zh-CN;
<http://schema.org/author> "Apache Any23"@zh-CN .
------------------------------------------------------------------------
Apache Any23 SUCCESS
Total time: 1s
Finished at: Tue Oct 13 16:26:10 GMT 2026
Final Memory: 79M/506M
------------------------------------------------------------------------
验证结果:Turtle 写入器自动生成 sindice、dcterms、rdf、foaf 等前缀声明,语义与 N-Triples 完全一致。
5.2 N-Quads 输出
any23 rover -e html-head-title,html-rdfa11,html-microdata -f nquads -o /opt/any23-demo/out.nq /opt/any23-demo/sample.html
查看输出文件:
cat /opt/any23-demo/out.nq
<file:/opt/any23-demo/sample.html> <http://www.w3.org/1999/xhtml/vocab#license> <http://www.apache.org/licenses/LICENSE-2.0> <file:/opt/any23-demo/sample.html> .
<file:/opt/any23-demo/sample.html> <http://www.w3.org/ns/rdfa#usesVocabulary> <http://schema.org/> <file:/opt/any23-demo/sample.html> .
_:n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> <file:/opt/any23-demo/sample.html> .
_:n0 <http://schema.org/name> "Anything To Triples"@zh-CN <file:/opt/any23-demo/sample.html> .
_:n0 <http://schema.org/author> "Apache Any23"@zh-CN <file:/opt/any23-demo/sample.html> .
验证结果:N-Quads 每条语句附带第四个元素(上下文 IRI,即文档自身 file: 地址),适合直接入库带命名图的 RDF 存储。
5.3 JSON-LD 输出
any23 rover -e html-head-title,html-head-meta,html-rdfa11,html-microdata -f jsonld -o /opt/any23-demo/out.jsonld /opt/any23-demo/sample.html
cat /opt/any23-demo/out.jsonld
[ {
"@graph" : [ {
"@id" : "_:n0",
"@type" : [ "http://schema.org/Book" ],
"http://schema.org/author" : [ {
"@language" : "zh-cn",
"@value" : "Apache Any23"
} ],
"http://schema.org/name" : [ {
"@language" : "zh-cn",
"@value" : "Anything To Triples"
} ]
}, {
"@id" : "_:node1k4r1gpcbx2",
"@type" : [ "http://schema.org/Person" ],
"http://schema.org/name" : [ {
"@language" : "zh-cn",
"@value" : "Alice"
} ]
}, {
"@id" : "file:/opt/any23-demo/sample.html",
"http://purl.org/dc/terms/title" : [ {
"@value" : "Any23 实测示例页面"
} ],
"http://vocab.sindice.net/any23#author" : [ {
"@language" : "zh-cn",
"@value" : "Apache Any23 Team"
} ],
"http://www.w3.org/1999/xhtml/vocab#license" : [ {
"@id" : "http://www.apache.org/licenses/LICENSE-2.0"
} ],
"http://www.w3.org/ns/rdfa#usesVocabulary" : [ {
"@id" : "http://schema.org/"
} ]
} ],
"@id" : "file:/opt/any23-demo/sample.html"
} ]
验证结果:JSON-LD 以 @graph 组织实体,语言标签转为 zh-cn 小写形式,可直接交付前端或 JSON-LD 处理器消费。
六、CLI 其他集成入口
6.1 远程 URL 抽取
rover 直接接受 http/https URL 作为输入:
any23 rover -e html-head-title http://example.com/page.html
说明:实测环境网络受限未完整复现远程抽取回显;远程抽取行为与本地文件一致,仅数据获取阶段走 HTTPDocumentSource。
6.2 单提取器调试
使用 extractor 子命令查看指定提取器的示例输入与示例输出:
any23 extractor -i html-rdfa11
------------------------------------------------------------------------
Apache Any23 :: extractor
------------------------------------------------------------------------
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div xmlns:v="http://schema.org/" typeof="v:Review">
<span property="v:itemreviewed">L?Amourita Pizza</span>
Reviewed by
<span property="v:author">Ulysses Grant</span> on
<span property="v:dateCreated" content="2009-01-06">Jan 6</span>.
<span property="v:summary">Delicious, tasty pizza on Eastlake!</span>
<span property="v:about">L'Amourita serves up traditional wood-fired
Neapolitan-style pizza, brought to your table promptly and without fuss.
An ideal neighborhood pizza joint.</span>
Rating:
<span property="v:reviewRating">4.5</span>
</div>
------------------------------------------------------------------------
Apache Any23 SUCCESS
Total time: 0s
Finished at: Tue Oct 13 16:35:52 GMT 2026
Final Memory: 47M/506M
------------------------------------------------------------------------
any23 extractor -o html-rdfa11
------------------------------------------------------------------------
Apache Any23 :: extractor
------------------------------------------------------------------------
_:n0 a <http://schema.org/Review>;
<http://schema.org/itemreviewed> "L?Amourita Pizza";
<http://schema.org/author> "Ulysses Grant";
<http://schema.org/dateCreated> "2009-01-06";
<http://schema.org/summary> "Delicious, tasty pizza on Eastlake!";
<http://schema.org/about> "L'Amourita serves up traditional wood-fired Neapolitan-style pizza, brought to your table promptly and without fuss. An ideal neighborhood pizza joint.";
<http://schema.org/reviewRating> "4.5" .
------------------------------------------------------------------------
Apache Any23 SUCCESS
Total time: 0s
------------------------------------------------------------------------
验证结果:extractor -i/-o 提供官方示例输入与预期输出,是编写自定义提取器与排查问题的调试利器。
七、Java 库集成示例
7.1 Maven 依赖
Any23 2.7 已发布至 Maven Central,库方式集成声明如下坐标:
<dependency>
<groupId>org.apache.any23</groupId>
<artifactId>apache-any23-core</artifactId>
<version>2.7</version>
</dependency>
说明:核心库默认配置与提取器全部位于 core 模块;需要 HTTP 数据源时同时引入 apache-any23-api。以下示例依据官方开发指南整理,抽取链路与 CLI 同源。
7.2 从本地文件抽取 N-Triples
import org.apache.any23.Any23;
import org.apache.any23.source.DocumentSource;
import org.apache.any23.source.FileDocumentSource;
import org.apache.any23.writer.NTriplesWriter;
import org.apache.any23.writer.TripleHandler;
import java.io.ByteArrayOutputStream;
import java.io.File;
public class ExtractDemo {
public static void main(String[] args) throws Exception {
// 1. 创建 Any23 外观实例,可指定提取器集合
Any23 runner = new Any23("html-rdfa11", "html-microdata");
// 2. 构造文档源(本地文件)
DocumentSource source = new FileDocumentSource(
new File("/opt/any23-demo/sample.html"));
// 3. 准备输出流与三元组处理器
ByteArrayOutputStream out = new ByteArrayOutputStream();
TripleHandler handler = new NTriplesWriter(out);
try {
// 4. 执行抽取,三元组写入 handler
runner.extract(source, handler);
} finally {
handler.close();
}
// 5. 打印 UTF-8 编码的 N-Triples 结果
System.out.println(out.toString("UTF-8"));
}
}
7.3 从 HTTP URL 抽取
import org.apache.any23.Any23;
import org.apache.any23.http.HTTPClient;
import org.apache.any23.source.DocumentSource;
import org.apache.any23.source.HTTPDocumentSource;
import org.apache.any23.writer.NTriplesWriter;
import org.apache.any23.writer.TripleHandler;
public class HttpExtractDemo {
public static void main(String[] args) throws Exception {
Any23 runner = new Any23();
runner.setHTTPUserAgent("my-agent");
HTTPClient httpClient = runner.getHTTPClient();
DocumentSource source = new HTTPDocumentSource(
httpClient, "http://example.com/page.html");
TripleHandler handler = new NTriplesWriter(System.out);
try {
runner.extract(source, handler);
} finally {
handler.close();
}
}
}
7.4 运行时配置注入
import org.apache.any23.Any23;
import org.apache.any23.configuration.DefaultConfiguration;
import org.apache.any23.configuration.ModifiableConfiguration;
public class ConfigDemo {
public static void main(String[] args) throws Exception {
// 拷贝默认配置并修改 HTTP 超时
ModifiableConfiguration conf = DefaultConfiguration.copy();
conf.setProperty("any23.http.client.timeout", "30000");
// 带配置实例化,并指定提取器
Any23 runner = new Any23(conf, "html-rdfa11");
// 后续抽取均使用该配置
}
}
验证结果:以上 3 段示例覆盖本地文件、HTTP 数据源与配置注入三类集成形态,API 均来自 org.apache.any23 包公开接口,编译运行前需通过 Maven 拉取 core/api 依赖。
八、REST 服务说明
Any23 的 REST 服务(any23server)位于 service 模块,CLI 发行包未内置该脚本,需从源码构建:
git clone https://github.com/apache/any23.git
cd any23 && mvn -pl service -am package
说明:仓库已归档为只读,构建前请确认 JDK 与 Maven 环境;REST 服务生产使用建议直接评估 Apache Tika 的 REST Server。
九、参考链接
- Apache Any23 官方入门:https://any23.apache.org/getting-started.html
- Apache Any23 数据抽取开发指南:https://any23.apache.org/dev-data-extraction.html
- Apache Any23 Maven Central:https://repo1.maven.org/maven2/org/apache/any23/
- Apache Any23 API 文档:https://any23.apache.org/apidocs/index.html
- Apache Attic Any23 项目状态:https://attic.apache.org/projects/any23.html
- Apache Tika(替代方案):https://tika.apache.org/