How to query Grakn with Java?

arcticOak2 :

I went through the documentation of Java api to query Grakn database.

Grakn.Transaction readTransaction = session.transaction(GraknTxType.READ);
GetQuery query = Graql.match(var("p").isa("person")).limit(10).get();
Stream<ConceptMap> answers = query.withTx(readTransaction).stream();
answers.forEach(answer -> System.out.println(answer.get("p").id()));

It's printing id, but I want to see the data, the name associated with the person. I want to see the content inside the result. It's simply showing id.

Soroush Saffari :

The answers provided as the result of a Graql query, is a collection of the variables (and their values) as you have specified them in the query itself.

In this case, to get the name of instances of person, you'd include it in the query like so:

GetQuery query = Graql.match(var("p").isa("person").has("name", var("n"))).limit(10).get();

The Graql equivalent being match $p isa person, has name $n; get;.

Now you can use the methods available in the Concept API to retrieve information available on each variable.

In this case, variable n holds an attribute and you'd want to retrieve its value(), like so:

answers.forEach(answer -> System.out.println(answer.get("n").asAttribute().value()))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=94159&siteId=1