本教程说明了如何使用 Grakn的使用数据Java Client 将CSV,JSON或XML格式的客户数据集迁移到Grakn知识图中 。 我们将在本文中讨论的端将到 phone_calls. 知识图 称为 此知识图的模式在 此处 的前一篇文章中定义 。 如果您已经熟悉Grakn,加载并且您需要的知识只是一个迁移示例,您会发现 这个Github存储库 很有用。图中 如果,使用数据另一方面,客户你不熟悉的端将到技术,一定要首先完成 定义模式 的加载 phone_calls 知识图,使用Java数据迁移到Grakn阅读进行了详细的知识指导。 在我们开始迁移之前,图中让我们快速提醒一下 phone_calls 知识图 的使用数据架构是如何形成的 。 我们将概述迁移的客户发生方式。 首先,端将到我们需要与我们的Grakn键空间进行对话。 为此,我们将使用 Grakn 的 Java客户端 。站群服务器 我们将遍历每个数据文件,提取每个数据项并将其解析为JSON对象。 我们将每个数据项(以JSON对象的形式)传递给其对应的模板。 模板返回的是用于将该项插入Grakn的Graql查询。 我们将执行每个查询以将数据加载到目标键空间 - phone_calls 。 在继续之前,请确保已安装Java 1.8并在 计算机上 运行 Grakn服务器 。 该项目使用SDK 1.8并命名 phone_calls 。 我将使用IntelliJ作为IDE。 修改 pom.xml 以包含最新版本的Grakn(1.4.2)作为依赖项。 我们希望能够配置Grakn注销的内容。 为此,请修改 pom.xml 以排除 slf4j 附带 grakn 并 logback 作为依赖 项添加 。 接下来,添加一个 logback.xml 使用以下内容 调用的新文件 并将其放在下面 src/main/resources 。 在 src/main 创建一个名为的新文件 Migration.java 。 这是我们要编写所有代码的地方。 选择以下数据格式之一并下载文件。 下载四个文件中的每个文件后,源码下载将它们放在 src/main/resources/data 目录下。 我们将使用它们将数据加载到我们的 phone_calls 知识图中。 随后的所有代码都将被写入 Migration.java 。 在此之前,我们需要一个结构来包含读取数据文件和构建Graql查询所需的详细信息。 这些细节包括: 数据文件的路径,和 接收JSON对象并生成Graql插入查询的模板函数。 为此,我们创建了一个名为的新子类 Input 。 在本文的后面,我们将看到如何 Input 创建类 的实例 ,但在我们开始之前,让我们将 mjson 依赖 项添加 到 文件中 的 dependencies 标记中 pom.xml 。 是时候初始化了 inputs 。 下面的代码调用 initialiseInputs() 返回集合的方法 inputs 。 然后,我们将使用 input 此集合中的 每个 元素将每个数据文件加载到Grakn中。云南idc服务商 input.getDataPath() 会回来的 data/companies 。 鉴于 company 是 input.template(company) 将返回 input.getDataPath() 会回来的 data/people 。 鉴于 person 是 input.template(person) 将返回 并给予 person 被 input.template(person) 将返回 input.getDataPath() 会回来的 data/contracts 。 鉴于 contract 是 input.template(contract) 将返回 input.getDataPath() 会回来的 data/calls 。 鉴于 call 是 input.template(call) 将返回 现在我们已经为每个数据文件定义了数据路径和模板,我们可以继续连接我们的 phone_calls 知识图并将数据加载到其中。 connectAndMigrate(Collection<Input> inputs) 是启动数据迁移到 phone_calls 知识图中 的唯一方法 。 此方法发生以下情况: grakn 创建 Grakn实例 ,连接到我们在本地运行的服务器 localhost:48555 。 session 创建 A ,连接到键空间 phone_calls 。 对于 集合中的 每个 input 对象 inputs ,我们称之为 loadDataIntoGrakn(input, session) 。 这将负责将 input 对象中 指定的数据加载 到我们的键空间中。 最后 session 关闭了。 现在我们已经 session 连接到 phone_calls 键空间,我们可以继续将数据实际加载到我们的知识图中。 为了将每个文件中的数据加载到Grakn中,我们需要: 检索一个 ArrayList JSON对象,每个对象代表一个数据项。 为此,我们呼吁 parseDataToJson(input) ,和 对于每个JSON对象 items :a)创建事务 tx ,b)构造 graqlInsertQuery 使用相应的 template ,c)运行 query ,d) commit 事务和e) close 事务。 现在我们已经完成了上述所有操作,我们已准备好读取每个文件并将每个数据项解析为JSON对象。 这些JSON对象将被传递给 template 每个 Input 对象 上 的 方法 。 我们要编写实现 parseDataToJson(input) 。 parseDataToJson(input) 根据数据文件的格式 ,实现会 有所不同。 但无论数据格式是什么,我们都需要正确的设置来逐行读取文件。 为此,我们将使用 InputStreamReader 。 我们将使用 Univocity CSV Parser 来解析我们的 .csv 文件。 让我们为它添加依赖项。 我们需要在 dependencies 标签中 添加以下内容 pom.xml 。 完成后,我们将编写 parseDataToJson(input) 解析 .csv 文件 的实现 。 除了这个实现,我们还需要进行一次更改。 鉴于CSV文件的性质,生成的JSON对象将把 .csv 文件的 所有列 作为其键,即使该值不存在,它也将被视为一个 null 。 出于这个原因,我们需要在 person template 的 input 实例 方法中 更改一行 。 if (! person.has("first_name")) { ...} 变 if (person.at("first_name").isNull()) { ...} 。 我们将使用 Gson的JsonReader 来读取我们的 .json 文件。 让我们为它添加依赖项。 我们需要在 dependencies 标签中 添加以下内容 pom.xml 。 完成后,我们将编写 parseDataToJson(input) 用于读取 .json 文件 的实现 。 我们将使用Java的内置 StAX 来解析我们的 .xml 文件。 要解析XML数据,我们需要知道目标标记的名称。 这需要在 Input 类中 声明 并在构造每个 input 对象 时指定 。 现在用于 parseDataToJson(input) 解析 .xml 文件 的实现 。 以下是我们 Migrate.java 将CSV数据加载到Grakn中的样子,并在这里找到 JSON 和 XML 文件的 样子 。 运行 main 方法,坐下来,放松并观察日志,同时数据开始涌入Grakn。 我们从设置项目和定位数据文件开始。 接下来,我们继续设置迁移机制,该机制独立于数据格式。 然后,我们了解了如何将具有不同数据格式的文件解析为JSON对象。 最后,我们运行了 使用给定 main 方法触发 connectAndMigrate 方法的方法 inputs 。 这将数据加载到我们的Grakn知识图中。 快速查看phone_calls架构
将数据迁移到Grakn
入门
创建一个新的Maven项目
将Grakn设置为依赖关系
配置日志记录
创建迁移类
包括数据文件
指定每个数据文件的详细信息
公司的输入实例
//进口 公共 类 迁移 { 抽象 静态 类 输入 { ...} public static void main(String [] args){ ...} static Collection < Input > initialiseInputs(){ 集合< Input > inputs = new ArrayList <>(); 输入。add(new Input(“data / companies”){ @覆盖 public String template(Json company){ 返回 “插入$ company isa company has name” + 公司。at(“name”)+ “;” ; } }); 回报 输入 ; } } 一个人的输入实例
//进口 公共 类 迁移 { 抽象 静态 类 输入 { ...} public static void main(String [] args){ ...} static Collection < Input > initialiseInputs(){ 集合< Input > inputs = new ArrayList <>(); 输入。add(new Input(“data / companies”){ ...}); 输入。add(new Input(“data / people”){ @覆盖 public String template(Json person){ //插入人 String graqlInsertQuery = “insert $ person isa person has phone-number” + person。at(“phone_number”); 如果(! 人。有(“FIRST_NAME” )){ //人不是客户 graqlInsertQuery + = “has is-customer false” ; } else { //人是顾客 graqlInsertQuery + = “has is-customer true” ; graqlInsertQuery + = “有名字” + 人。at(“first_name”); graqlInsertQuery + = “有姓氏” + 人。at(“last_name”); graqlInsertQuery + = “有城市” + 人。在(“城市”); graqlInsertQuery + = “有年龄” + 人。在(“年龄”)。asInteger(); } graqlInsertQuery + = “;” ; return graqlInsertQuery ; } }); 回报 输入 ; } } 合同的输入实例
//进口 公共 类 迁移 { 抽象 静态 类 输入 { ...} public static void main(String [] args){ ...} static Collection < Input > initialiseInputs(){ 集合< Input > inputs = new ArrayList <>(); 输入。add(new Input(“data / companies”){ ...}); 输入。add(new Input(“data / people”){ ...}); 输入。add(new Input(“data / contracts”){ @覆盖 public String template(Json contract){ //匹配公司 String graqlInsertQuery = “匹配$ company isa company has name” + contract。at(“company_name”)+ “;” ; //匹配人 graqlInsertQuery + = “$ customer isa person has phone-number” + contract。at(“person_id”)+ “;” ; //插入合同 graqlInsertQuery + = “insert(provider:$ company,customer:$ customer)isa contract;” ; return graqlInsertQuery ; } }); 回报 输入 ; } } 呼叫的输入实例
//进口 公共 类 迁移 { 抽象 静态 类 输入 { ...} public static void main(String [] args){ ...} static Collection < Input > initialiseInputs(){ 集合< Input > inputs = new ArrayList <>(); 输入。add(new Input(“data / companies”){ ...}); 输入。add(new Input(“data / people”){ ...}); 输入。add(new Input(“data / contracts”){ ...}); 输入。add(new Input(“data / calls”){ @覆盖 public String template(Json call){ //匹配来电者 String graqlInsertQuery = “match $ caller isa person has phone-number” + call。at(“caller_id”)+ “;” ; //匹配被叫者 graqlInsertQuery + = “$ callee isa person has phone-number” + call。at(“callee_id”)+ “;” ; //插入电话 graqlInsertQuery + = “insert $ call(caller:$ caller,callee:$ callee)isa call;” + “$ call已经开始” + 来电。at(“started_at”)。asString()+ “;” + “$ call has duration” + call。在(“持续时间”)。asInteger()+ “;” ; return graqlInsertQuery ; } }); 回报 输入 ; } } 连接和迁移
将数据加载到phone_calls
DataFormat特定实现
解析CSV
阅读JSON
解析XML
把它放在一起
加载时间
回顾一下