Kratos

A responsible theme for WordPress

使用Java代码创建Allegrograph客户端连接AG服务

 Java客户端发行版包含Java Sesame API。

Java客户端通过HTTP端口10035与AllegroGraph服务器通信。Java和AllegroGraph可以安装在同一台计算机上,但实际上一台服务器由运行在不同机器上的多个客户端共享。

通过Java Sesame API,可以了从基于Java的应用程序访问AllegroGraph服务器。这个API提供了创建、查询和维护RDF数据的方法,以及管理存储的三元组的方法。

概念

在使用Java操作图数据库时,需要了解一下概念:

  • “RDF”是由万维网联盟(W3C)定义的资源描述框架。它提供了一种非常简单的方法来描述多面资源对象,并将它们链接到复杂关系图中。AllegroGraph Server创建、搜索和管理这样的RDF图。
  • “URI”是一个统一的资源标识符。它是一个标签,用于惟一地标识RDF图中各种类型的实体。一个典型的URI看起来很像一个web地址:http://example.org/project/class#number。尽管有相似之处,URI并不是web地址。它只是一个独特的标签。
  • “三元组”是一个数据语句,一个以RDF格式存储的“事实”。它声明资源具有值的属性。它包括三个领域:
    • 主语:第一个字段包含唯一标识此三元组描述的资源的URI。
    • 谓语:第二个字段包含标识此资源的属性的URI,比如它的颜色或大小,或者该资源与另一个资源之间的关系,比如父类或所有权。
    • 宾语:第三个字段是属性的值。它可以是一个文字值,比如“red”,或者链接资源的URI。
  • “quad”是一个添加了“context”字段的三元组,用于将存储库划分为“子图”。这个上下文或子图只是出现在相关三元组的第四个字段中的URI标签。
  • “quint”是一个包含第五个字段的quad,用于“tripleID”。AllegroGraph 服务器实现所有的三元组作为五等分在幕后。然而,第四个和第五个字段常常被忽略,所以我们随意地说“三元组”,有时也说“四元组”,而把它们都称为“五元组”会更严格一些。
  • “资源描述”定义为在subject字段中具有相同URI的三元组的集合。换句话说,三元组都描述同一事物的属性。
  • “语句”是描述三元组(quad、quint)的Java客户端对象。

数据在Allegrograph服务器中怎么存储?

《使用Java代码创建Allegrograph客户端连接AG服务》

在寓言服务器的上下文中:

  • “catalog”是寓言图服务器拥有的存储库列表。
  • “repository”是目录中的三元组集合,存储在硬盘上并建立索引。
  • “context”是存储库中三元组的子图。
  • 如果上下文不使用,则将三元组存储在后台(默认)图中。

Java如何创建Allegrograph客户端连接?

1、Java代码所需依赖的Allegrograph的Maven依赖

		<!-- https://mvnrepository.com/artifact/com.franz/agraph-java-client -->
		<dependency>
			<groupId>com.franz</groupId>
			<artifactId>agraph-java-client</artifactId>
			<version>3.0.0</version>
		</dependency>

2、Allegrograph服务器配置

private static final String SERVER_URL = "http://000.000.000.000:10035";
private static final String USERNAME = "admin";
private static final String PASSWORD = "*************";

需要知道服务器的ip和安装ag时设定的访问端口号以及登录的账号和密码。

3、获取连接及相应操作代码

public class TutorialExamples {
	private static final String SERVER_URL = "http://000.000.000.000:10035";
	// private static final String CATALOG_ID = "scratch";
	private static final String REPOSITORY_ID = "javatutorial";
	// private static final String REPOSITORY_ID = "TestPerson";
	private static final String USERNAME = "admin";
	private static final String PASSWORD = "***********.";
	// private static final File DATA_DIR = new File(".");

	// private static final String FOAF_NS = "http://xmlns.com/foaf/0.1/";

	public static void main(String[] args) throws Exception {
		AGRepositoryConnection conn = getAGRepositoryConnection(false);
		/**
		 * 字母的顺序表示索引是如何组织的。例如,spogi索引包含存储中的所有三元组,
		 * 首先按主题排序,然后按谓词排序,然后按对象排序,最后按图排序。三重id号作为索引的第五列出现。如果您知道所需资源的URI(
		 * 查询模式的subject值), 那么spogi索引允许您以单个块的形式检索该主题的所有三元组。
		 * 其思想是提供查询所需的索引,并避免维护永远不需要的索引。
		 * 我们可以使用连接对象的listValidIndices()方法来检查所有可能的寓言图三重索引的列表:
		 */
		List<String> indices = conn.listValidIndices();
		System.out.println("All valid triple indices: " + indices);

		/**
		 * 如果需要,寓言图可以生成这些索引中的任何一个,但默认情况下它只创建七个索引。我们可以使用连接对象的listIndices()
		 * 方法来查看当前索引:
		 */
		indices = conn.listIndices();
		System.out.println("Current triple indices: " + indices);

		/**
		 * 以“g”开头的索引主要根据子图(或“上下文”)进行排序。 如果应用程序不使用子图,则应该考虑从存储库中删除这些索引。
		 * 您不希望构建和维护应用程序永远不会使用的三重索引。这会浪费CPU时间和磁盘空间。 连接对象有一个方便的dropIndex()方法:
		 */
		System.out.println("Removing graph indices...");
		conn.dropIndex("gospi");
		conn.dropIndex("gposi");
		conn.dropIndex("gspoi");
		indices = conn.listIndices();
		System.out.println("Current triple indices: " + indices);

		/**
		 * i索引用于使用三元组id号删除三元组。ospgi索引主要根据对象值排序,这使得从索引中获取对象值的范围成为一个三元组块成为可能。
		 * 类似地,posgi索引让我们可以找到一个由所有三元组组成的块,这些三元组都具有相同的谓词。
		 * 我们前面提到过,spogi索引允许我们检索具有相同subject URI的三元组块。
		 * 碰巧,我们可能过于匆忙地消除了所有的图索引。只要存在一个索引,寓言图就可以找到正确的匹配,但是使用“正确”索引要快得多。
		 * 我们把其中一个指标放回去,以防我们需要它。我们将使用连接对象的addIndex()方法:
		 */
		System.out.println("Adding one graph index back in...");
		conn.addIndex("gspoi");
		indices = conn.listIndices();
		System.out.println("Current triple indices: " + indices);

	}

	/**
	 * 获取AGRepository连接 Creating a Repository
	 */
	public static AGRepositoryConnection getAGRepositoryConnection(boolean close) throws Exception {
		// 连接一个已有的ag服务
		System.out.println("\nStarting Connect AG.");
		AGServer server = new AGServer(SERVER_URL, USERNAME, PASSWORD);
		System.out.println("Available catalogs: " + server.listCatalogs());

		// 获取catalog
		AGCatalog catalog = server.getRootCatalog();
		System.out.println(
				"Available repositories in catalog " + (catalog.getCatalogName()) + ": " + catalog.listRepositories());

		// 删除某个库的访问记录
		// catalog.deleteRepository(REPOSITORY_ID);

		// 创建一个ag库
		/*
		 * AGRepository myRepository = catalog.createRepository(REPOSITORY_ID);
		 * System.out.println("Got a repository."); myRepository.initialize();
		 * System.out.println("Initialized repository."); System.out.println(
		 * "Repository is writable? " + myRepository.isWritable());
		 */

		// 获取一个ag库
		AGRepository myRepository = catalog.openRepository(REPOSITORY_ID);
		System.out.println("Got a repository.");
		System.out.println("Repository is writable? " + myRepository.isWritable());

		AGRepositoryConnection conn = myRepository.getConnection();
		// closeBeforeExit(conn);
		System.out.println("Got a connection.");
		System.out.println("Repository " + (myRepository.getRepositoryID()) + " is up! It contains " + (conn.size())
				+ " statements.");
		
		// if close = true 关闭连接服务
		if (close) {
			conn.close();
			myRepository.shutDown();
			return null;
		}
		return conn;
	}
}

 

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注