viswa
viswa

Reputation: 93

querying data from particular node's database

I am doing some reporting kind of things using customer query. for that, i have to fetch the data from respective node's database.but,no clue how to do that.its normally fetching all data irrespective of node.

Upvotes: 1

Views: 154

Answers (1)

Joel
Joel

Reputation: 23140

Let's do an example based on the IOU CorDapp (https://github.com/corda/cordapp-example/). There are several ways you can do this:

1. Via an API endpoint

This endpoint will return any IOUs stored on the node with a value above minValue:

@GET
@Path("ious-above-value")
@Produces(MediaType.APPLICATION_JSON)
fun getIOUsAboveValue(@QueryParam("minValue") minValue: Int): List<IOUState> {
    val results = builder {
        val currencyIndex = IOUSchemaV1.PersistentIOU::value.greaterThan(minValue)
        val customCriteria = QueryCriteria.VaultCustomQueryCriteria(currencyIndex)
        rpcOps.vaultQueryBy<IOUState>(customCriteria)
    }

    val stateAndRefs = results.states
    return stateAndRefs.map { stateAndRef -> stateAndRef.state.data }
}

2. Via a client

This client will return any IOUs stored on the node with a value above minValue:

fun main(args: Array<String>) {
    require(args.size == 1) { "Usage: ExampleClientRPC <node address>" }
    val nodeAddress = NetworkHostAndPort.parse(args[0])
    val client = CordaRPCClient(nodeAddress)

    // Can be amended in the com.example.MainKt file.
    val proxy = client.start("user1", "test").proxy

    val results = builder {
        val currencyIndex = IOUSchemaV1.PersistentIOU::value.greaterThan(3)
        val customCriteria = QueryCriteria.VaultCustomQueryCriteria(currencyIndex)
        proxy.vaultQueryBy<IOUState>(customCriteria)
    }

    val stateAndRefs = results.states
    val states = stateAndRefs.map { stateAndRef -> stateAndRef.state.data }
}

3. Via the node's database directly

You can log into the node's database by following the instructions here: https://docs.corda.net/node-database.html. You will then be able to execute SQL queries against the node's database directly.

Upvotes: 1

Related Questions