简体   繁体   English

如何在Apache Jena中保存记录?

[英]How do I save a record in Apache Jena?

I want to create a person with nick name jd and e-mail john.doe@provider.com in a persistent Apache Jena database. 我想在一个持久的Apache Jena数据库中创建一个昵称jd的人,并发送电子邮件john.doe@provider.com

I wrote following code: 我写了以下代码:

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))
}

When I run it, I get no errors. 运行它时,没有任何错误。

But when I try to read the data from the file (see code below), the query doesn't find anything. 但是,当我尝试从文件中读取数据时(请参见下面的代码),查询找不到任何内容。

    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()
    }

What's wrong with my code? 我的代码有什么问题?

You should get a warning at end because you have not committed the transaction. 由于尚未提交事务,因此应该在end收到警告。

dataSet.begin(ReadWrite.WRITE)
val model = dataSet.defaultModel
createPerson("john.doe@provider.com", model, "jd")
dataSet.commit()
dataSet.end()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM