如何使用Theo和Neo4j数据库从Swift中检索Node属性?

时间:2018-01-12 15:04:42

标签: swift xcode neo4j cypher

以下是我的Swift Xcode项目的代码片段。我目前正在使用Swift在Xcode中开发一个应用程序,我正在尝试使用Neo4j作为存储所有数据等的后端。

在使用Swift和Neo4j时,唯一的选择是使用Theo连接并与Neo4j数据库通信。您可以直接进行Cypher查询,但是当我想从Neo4j数据库中获取数据并将其本地存储在Swift中的变量或数组中时,它已被证明是非常困难的。

我的代码,请记住这是直接来自Neo4j Theo Swift示例包,您可以在这里找到 - https://github.com/GraphStory/neo4j-ios

import UIKit
import Theo
import PackStream

class QueryViewController: UIViewController {

    var connectionConfig: ConnectionConfig?
    @IBOutlet weak var outputTextView: UITextView?

    //All of the User Interface Outlets, Buttons etc.
    @IBOutlet weak var createNodeButton: UIButton?
    @IBOutlet weak var fetchNodeButton: UIButton?
    @IBOutlet weak var runCypherButton: UIButton?
    @IBOutlet weak var runTransactionButton: UIButton?
    @IBOutlet weak var clientNameField: UITextField!
    @IBOutlet weak var clientYearBorn: UITextField!
    @IBOutlet weak var nodeInfo: UILabel!

    var query: String!
    var clientArray: [String] = []

    var clientID : Int = 0
    var clientName: String!
    var yearBorn: String!

    private var theo: BoltClient?
    private var lastNodeId: UInt64 = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        disableButtons()
        if let config = connectionConfig {
            do {
                self.theo = try BoltClient(
                    hostname: config.host,
                    port: config.port,
                    username: config.username,
                    password: config.password,
                    encrypted: true)
            } catch {
                DispatchQueue.main.async { [weak self] in
                    self?.outputTextView?.text = "Failed during connection configuration"
                }
                return
            }

            guard let theo = self.theo else { return }

            log("Connecting...")

            let result = theo.connectSync()
            switch result {
            case .failure(_):
                log("Error while connecting")
            case .success(_):
                let result = theo.executeCypherSync("MATCH (n:ImpossibleNode) RETURN count(n) AS n")
                switch result {
                case let .failure(error):
                    log("Error while connecting: \(error)")
                case .success(_):
                    log("Connected")
                    DispatchQueue.main.async { [weak self] in
                        self?.enableButtons()
                    }
                }
            }

        } else {
            outputTextView?.text = "Missing connection configuration"
        }
    }

    private func enableButtons() {
        createNodeButton?.isEnabled = true
        fetchNodeButton?.isEnabled = true
        runCypherButton?.isEnabled = true
        runTransactionButton?.isEnabled = true
    }

    private func disableButtons() {
        createNodeButton?.isEnabled = false
        fetchNodeButton?.isEnabled = false
        runCypherButton?.isEnabled = false
        runTransactionButton?.isEnabled = false
    }

    //Here we can create a Node to add to the Neo4j Database.
    @IBAction func createNodeTapped(_ sender: UIButton) {

        guard let theo = self.theo else {
            log("Client not initialized yet")
            return
        }

    //Here we define some variables we wish to store within the node as Properties.
         clientName = clientNameField?.text
         yearBorn = clientYearBorn.text
         clientID = Int(lastNodeId)

         self.lastNodeId += 1

        let node = Node(label: "Client", properties:["id": clientID, "name": clientName, "yearBorn": yearBorn])

        let result = theo.createAndReturnNodeSync(node: node)
        switch result {
        case let .failure(error):
            log("Error while creating node: \(error)")
        case let .success(responseNode):
            log("Successfully created node: \(responseNode)")
            lastNodeId = responseNode.id!

            clientName = clientNameField?.text
            print("Added", clientName!, "to the Nexus")
            print(clientID)

        }
    }

    @IBAction func fetchNodeTapped(_ sender: UIButton) {

        guard let theo = self.theo else {
            log("Client not initialized yet")
            return
        }

        theo.nodeBy(id: lastNodeId) { result in
            switch result {
            case let .failure(error):
                self.log("Error while reading fetched node with ID '\(self.lastNodeId)': \(error)")
            case let .success(responseNode):
                if let responseNode = responseNode {
                    self.log("Fetched node with ID \(self.lastNodeId): \(responseNode)")

                    //Here I am attempting to grab all the nodes labeled "Client" with the property name "Mr Davies"

                    let results = theo.executeCypherSync("MATCH (c:Client{name: 'Mr Davies'}) RETURN (c)")

                    //Here I am appending the results from the Cypher Query above to an array - clientArray
                    self.clientArray.append((results.value?.nodes.description)!)

                    //Finally here I am printing to the console the client array.
                    print(self.clientArray)
                } else {
                    self.log("Could not find node with ID \(self.lastNodeId)")
                }
            }
        }
    }

    func log(_ string: String) {
        print(string)
        DispatchQueue.main.async {
            let text = self.outputTextView?.text ?? ""
            if text == "" {
                self.outputTextView?.text = string
            } else {
                self.outputTextView?.text = "\(string)\n\n\(text)"

            }
        }
    }

    @IBAction func runCypherTapped(_ sender: UIButton) {

        guard let theo = self.theo else {
            log("Client not initialized yet")
            return
        }

        let result = theo.executeCypherSync("MATCH (c:Client) RETURN count(c) AS num")
        switch result {
        case let .failure(error):
            log("Error while getting cypher results: \(error)")
        case let .success(queryResult):
            if let intNum = queryResult.rows[0]["num"] as? UInt64 {
                log("Asked via Cypher how many nodes there are with label Client. Answer: \(intNum)")

            } else {
                log("Got unexpected answer back")

            }
        }
    }

    @IBAction func runTransactionTapped(_ sender: UIButton) {

        do {
            try theo?.executeAsTransaction(transactionBlock: { (tx) in
                let query = "CREATE (C:Client { name: {name} } )"
                self.theo?.executeCypherSync(query, params: ["prop": "name"])
                self.theo?.executeCypherSync(query, params: ["prop": "yearborn"])

            })
        } catch {
            log("Error while executing transaction: \(error)")
            return
        }

        log("Transaction completed successfully")
    }
}

我使用此应用程序的目的是获取标记为"客户端"的所有节点。然后将所述节点的属性存储到Swift中的一个或多个本地数组中。然后我可以在" fetchNodeTapped"中操作数据。函数我已经评论了代码。您可以看到我将数组内容打印到控制台,这是我得到的:

  

获取ID为260的节点:Theo.Node [" [244:Theo.Node,229:Theo.Node,   154:Theo.Node,230:Theo.Node,215:Theo.Node,226:Theo.Node,249:   Theo.Node,177:Theo.Node,277:Theo.Node,214:Theo.Node,278:   Theo.Node,222:Theo.Node,285:Theo.Node,206:Theo.Node,158:   Theo.Node,258:Theo.Node,217:Theo.Node,167:Theo.Node,176:   Theo.Node,223:Theo.Node,153:Theo.Node,168:Theo.Node,266:   Theo.Node,228:Theo.Node,169:Theo.Node,220:Theo.Node,247:   Theo.Node,150:Theo.Node,284:Theo.Node,216:Theo.Node,156:   Theo.Node,272:Theo.Node,243:Theo.Node,255:Theo.Node,264:   Theo.Node,268:Theo.Node,205:Theo.Node,273:Theo.Node,320:   Theo.Node,175:Theo.Node,151:Theo.Node,257:Theo.Node,225:   Theo.Node,172:Theo.Node,270:Theo.Node,274:Theo.Node,246:   Theo.Node,224:Theo.Node,245:Theo.Node,283:Theo.Node,248:   Theo.Node,170:Theo.Node,318:Theo.Node,160:Theo.Node,218:   Theo.Node,152:Theo.Node,184:Theo.Node,256:Theo.Node,260:   Theo.Node,231:Theo.Node,173:Theo.Node,171:Theo.Node,149:   Theo.Node,157:Theo.Node,161:Theo.Node,221:Theo.Node,271:   Theo.Node,227:Theo.Node,275:Theo.Node,276:Theo.Node,279:   Theo.Node,155:Theo.Node,183:Theo.Node,219:Theo.Node,265:   Theo.Node,267:Theo.Node,162:Theo.Node,250:Theo.Node,263:   Theo.Node,269:Theo.Node,319:Theo.Node,159:Theo.Node]"]

在这里你可以看到我的功能是存储每一个"戴维斯先生"节点,当我将其打印到控制台时,它打印出每个节点ID" Theo.Node,319:例如。我想访问每个节点的特定属性,以便我可以从数据库中检索所有属性(name,yearBorn等),然后在本地存储这些属性,如上所述。

如果有人可以提供帮助,我们将不胜感激,如果您对我的问题有任何进一步的疑问,请随时解雇。

1 个答案:

答案 0 :(得分:0)

我不知道我是否完全理解了您的问题,但您可以使用keys()函数返回节点或关系的属性名称。例如:

创建示例节点:

CREATE (:Node {firstname : "Jon", lastname : "Doe", age : 20})

然后:

MATCH (node:Node)
RETURN keys(node)

输出将是:

╒══════════════════════════════╕
│"keys(node)"                  │
╞══════════════════════════════╡
│["firstname","age","lastname"]│
└──────────────────────────────┘