如何在 Apache Jena 中保存记录?
How do I save a record in Apache Jena?
我想在持久的 Apache Jena 数据库中创建一个昵称 jd
和电子邮件 john.doe@provider.com
的人。
我写了下面的代码:
var dataSet:Dataset? = null
val Dir = "data/MyDataSet"
dataSet = TDBFactory.createDataset(Dir)
dataSet.begin(ReadWrite.WRITE)
val model = dataSet.defaultModel
createPerson("john.doe@provider.com", model, "jd")
dataSet.end()
dataSet.close()
private fun createPerson(email: String, model: Model, nick: String) {
val uuid = UUID.randomUUID()
val uri = "http://mycompany.com/data/p-${uuid}"
val person = model.createResource(uri)
person.addProperty(VCARD.EMAIL, email)
person.addProperty(VCARD.N,
model.createResource()
.addProperty(VCARD.NICKNAME, nick))
}
当我 运行 它时,我没有收到任何错误。
但是当我尝试从文件中读取数据时(参见下面的代码),查询没有找到任何内容。
ds.begin(ReadWrite.READ)
val query = QueryFactory.create("""SELECT ?x
WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#EMAIL> "john.doe@provider.com" }""")
val qexec: QueryExecution
try {
qexec = QueryExecutionFactory.create(query, ds.defaultModel)
val rs = qexec.execSelect()
while (rs.hasNext()) {
val solution = rs.nextSolution()
System.out.println("")
}
}
catch (throwable:Throwable) {
logger.error("", throwable)
}
finally {
ds.end()
}
我的代码有什么问题?
您应该在 end
收到警告,因为您尚未提交交易。
dataSet.begin(ReadWrite.WRITE)
val model = dataSet.defaultModel
createPerson("john.doe@provider.com", model, "jd")
dataSet.commit()
dataSet.end()
我想在持久的 Apache Jena 数据库中创建一个昵称 jd
和电子邮件 john.doe@provider.com
的人。
我写了下面的代码:
var dataSet:Dataset? = null
val Dir = "data/MyDataSet"
dataSet = TDBFactory.createDataset(Dir)
dataSet.begin(ReadWrite.WRITE)
val model = dataSet.defaultModel
createPerson("john.doe@provider.com", model, "jd")
dataSet.end()
dataSet.close()
private fun createPerson(email: String, model: Model, nick: String) {
val uuid = UUID.randomUUID()
val uri = "http://mycompany.com/data/p-${uuid}"
val person = model.createResource(uri)
person.addProperty(VCARD.EMAIL, email)
person.addProperty(VCARD.N,
model.createResource()
.addProperty(VCARD.NICKNAME, nick))
}
当我 运行 它时,我没有收到任何错误。
但是当我尝试从文件中读取数据时(参见下面的代码),查询没有找到任何内容。
ds.begin(ReadWrite.READ)
val query = QueryFactory.create("""SELECT ?x
WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#EMAIL> "john.doe@provider.com" }""")
val qexec: QueryExecution
try {
qexec = QueryExecutionFactory.create(query, ds.defaultModel)
val rs = qexec.execSelect()
while (rs.hasNext()) {
val solution = rs.nextSolution()
System.out.println("")
}
}
catch (throwable:Throwable) {
logger.error("", throwable)
}
finally {
ds.end()
}
我的代码有什么问题?
您应该在 end
收到警告,因为您尚未提交交易。
dataSet.begin(ReadWrite.WRITE)
val model = dataSet.defaultModel
createPerson("john.doe@provider.com", model, "jd")
dataSet.commit()
dataSet.end()