org.neo4j.graphdb.Result.columnAs()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(94)

本文整理了Java中org.neo4j.graphdb.Result.columnAs方法的一些代码示例,展示了Result.columnAs的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Result.columnAs方法的具体详情如下:
包路径:org.neo4j.graphdb.Result
类名称:Result
方法名:columnAs

Result.columnAs介绍

[英]Returns an iterator with the result objects from a single column of the result set. This method is best used for single column results.

To ensure that any resources, including transactions bound to it, are properly closed, the iterator must either be fully exhausted, or the org.neo4j.graphdb.ResourceIterator#close() method must be called.

As an example, if we are only interested in a column n that contains node values, we can extract it like this:

try ( ResourceIterator<Node> nodes = result.columnAs( "n" ) ) 
{ 
while ( nodes.hasNext() ) 
{ 
Node node = nodes.next(); 
// Do some work with the 'node' instance. 
} 
}

[中]返回一个迭代器,其中包含结果集中单个列的结果对象。这种方法最适用于单列结果。
为了确保任何资源(包括绑定到它的事务)都被正确关闭,迭代器必须完全耗尽,或者组织必须关闭。neo4j。graphdb。必须调用ResourceIterator#close()方法。
例如,如果我们只对包含节点值的列n感兴趣,我们可以这样提取它:

try ( ResourceIterator<Node> nodes = result.columnAs( "n" ) ) 
{ 
while ( nodes.hasNext() ) 
{ 
Node node = nodes.next(); 
// Do some work with the 'node' instance. 
} 
}

代码示例

代码示例来源:origin: neo4j/neo4j

private Number countStaff()
{
  try ( Result countResult = databaseRule.execute( "MATCH (n:staff) return count(n.name) as count" ) )
  {
    return (Number) countResult.columnAs( "count" ).next();
  }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldHandleColumnAsWithNull()
{
  assertThat( db.execute( "RETURN toLower(null) AS lower" ).<String>columnAs( "lower" ).next(), nullValue() );
}

代码示例来源:origin: neo4j/neo4j

@Override
  public void evaluate()
  {
    // Then the database is not empty
    Result result = ruleWithDirectory.getGraphDatabaseService().execute( "MATCH (n) RETURN count(n) AS " + "count" );
    List<Object> column = Iterators.asList( result.columnAs( "count" ) );
    assertEquals( 1, column.size() );
    assertEquals( 1L, column.get( 0 ) );
  }
}, null );

代码示例来源:origin: neo4j/neo4j

private long nodesInDatabase()
{
  Result r = graphdb().execute( "MATCH (n) RETURN count(n) AS c" );
  Long nodes = (Long) r.columnAs( "c" ).next();
  r.close();
  return nodes;
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldCloseTransactionsWhenIteratingOverSingleColumn()
{
  // Given an execution result that has been started but not exhausted
  createNode();
  createNode();
  Result executionResult = db.execute( "CYPHER runtime=interpreted MATCH (n) RETURN n" );
  ResourceIterator<Node> resultIterator = executionResult.columnAs( "n" );
  resultIterator.next();
  assertThat( activeTransaction(), is( notNullValue() ) );
  // When
  resultIterator.close();
  // Then
  assertThat( activeTransaction(), is( nullValue() ) );
}

代码示例来源:origin: neo4j/neo4j

try ( ResourceIterator<String> iterator = db.execute( LIST_AVAILABLE_ANALYZERS ).columnAs( "analyzer" ) )

代码示例来源:origin: neo4j/neo4j

@Test
public void useColumnAsOnEagerResult()
{
  Result result = database.execute( "MATCH (n) RETURN n.c as c, n.b as b" );
  assertEquals( 1, testCursorContext.getAdditionalAttempts() );
  ResourceIterator<Object> cValues = result.columnAs( "c" );
  int rows = 0;
  while ( cValues.hasNext() )
  {
    cValues.next();
    rows++;
  }
  assertEquals( 2, rows );
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testRegexGroups() {
  testResult(db, "RETURN apoc.text.regexGroups('abc <link xxx1>yyy1</link> def <link xxx2>yyy2</link>','<link (\\\\w+)>(\\\\w+)</link>') AS result",
      result -> {
        final List<Object> r = Iterators.single(result.columnAs("result"));
        List<List<String>> expected = new ArrayList<>(asList(
            new ArrayList<String>(asList("<link xxx1>yyy1</link>", "xxx1", "yyy1")),
            new ArrayList<String>(asList("<link xxx2>yyy2</link>", "xxx2", "yyy2"))
        ));
        assertTrue(r.containsAll(expected));
      });
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testLoadXmlFromZipByUrl() {
  testResult(db, "call apoc.load.xml('https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/3.4/src/test/resources/testload.zip?raw=true!xml/books.xml') yield value as catalog\n" +
      "UNWIND catalog._children as book\n" +
      "RETURN book.id as id\n", result -> {
    List<Object> ids = Iterators.asList(result.columnAs("id"));
    assertTrue(IntStream.rangeClosed(1,12).allMatch(value -> ids.contains(String.format("bk1%02d",value))));
  });
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testLoadXmlFromTarGzByUrl() {
  testResult(db, "call apoc.load.xml('https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/3.4/src/test/resources/testload.tar.gz?raw=true!xml/books.xml') yield value as catalog\n" +
      "UNWIND catalog._children as book\n" +
      "RETURN book.id as id\n", result -> {
    List<Object> ids = Iterators.asList(result.columnAs("id"));
    assertTrue(IntStream.rangeClosed(1,12).allMatch(value -> ids.contains(String.format("bk1%02d",value))));
  });
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testBookIds() {
  testResult(db, "call apoc.load.xml('file:src/test/resources/xml/books.xml') yield value as catalog\n" +
      "UNWIND catalog._children as book\n" +
      "RETURN book.id as id\n", result -> {
    List<Object> ids = Iterators.asList(result.columnAs("id"));
    assertTrue(IntStream.rangeClosed(1,12).allMatch(value -> ids.contains(String.format("bk1%02d",value))));
  });
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testRunFirstColumnBugCompiled() throws Exception {
  ResourceIterator<Node> it = db.execute("CREATE (m:Movie  {title:'MovieA'})<-[:ACTED_IN]-(p:Person {name:'PersonA'})-[:ACTED_IN]->(m2:Movie {title:'MovieB'}) RETURN m").columnAs("m");
  Node movie = it.next();
  it.close();
  String query = "WITH {m} AS m MATCH (m)<-[:ACTED_IN]-(:Person)-[:ACTED_IN]->(rec:Movie) RETURN rec LIMIT 10";
  System.out.println(db.execute("EXPLAIN "+query).getExecutionPlanDescription().toString());
  ResourceIterator<Node> rec = db.execute(query, map("m",movie)).columnAs("rec");
  assertEquals(1, rec.stream().count());
}
@Test

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testRelType() {
  assertEquals("REL", db.execute("CREATE (f)-[rel:REL {foo:'bar'}]->(f) RETURN apoc.rel.type(rel) AS type").columnAs("type").next());
  assertEquals("REL", db.execute("CREATE (f) WITH f CALL apoc.create.vRelationship(f,'REL',{foo:'bar'},f) YIELD rel RETURN apoc.rel.type(rel) AS type").columnAs("type").next());
  assertNull(db.execute("RETURN apoc.rel.type(null) AS type").columnAs("type").next());
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testNodes() throws Exception {
  Collection<Object> ids = Iterators.asSet(db.execute("UNWIND range(0,2) as id CREATE (n:Node {id:id}) return id(n) as id").columnAs("id"));
  TestUtil.testResult(db, "CALL apoc.get.nodes({ids})", map("ids",ids), r -> {
    assertEquals(true, ids.contains(((Node) r.next().get("node")).getId()));
    assertEquals(true, ids.contains(((Node) r.next().get("node")).getId()));
    assertEquals(true, ids.contains(((Node) r.next().get("node")).getId()));
  });
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void shouldSearchInNumericRange() throws Exception {
  // given
  execute("UNWIND range(1, 10000) AS num CREATE (:Number{name:'The ' + num + 'th',number:num})");
  execute("CALL apoc.index.addAllNodes('numbers', {Number:['name','number']})");
  // when
  ResourceIterator<Object> names = db.execute(
      "CALL apoc.index.search('numbers', 'Number.number:{100 TO 105]') YIELD node\n" +
          "RETURN node.name").columnAs("node.name");
  // then
  assertThat(Iterators.asList(names), Matchers.containsInAnyOrder("The 101th", "The 102th", "The 103th", "The 104th", "The 105th"));
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testCombinePropertiesTargetSingleValueSourceArrayValuesDifferentType(){
  long id = db.execute("Create (d:Person {name:'Daniele'})\n" + "Create (p:Country {name:'USA'})\n" + "Create (d)-[:TRAVELS_TO {year:1995, reason:\"work\"}]->(p)\n"
      + "Create (d)-[:GOES_TO {year:[\"2010\",\"2015\"], reason:\"fun\"}]->(p)\n" + "Create (d)-[:FLIGHTS_TO {company:\"Air America\"}]->(p) RETURN id(p) as id ").<Long>columnAs("id").next();
  testCall(db, QUERY , (r) -> {
    Relationship rel1 = (Relationship) r.get("rel1");
    Relationship rel2 = (Relationship) r.get("rel2");
    PropertiesManager.mergeProperties(rel2.getProperties("year"), rel1, new RefactorConfig(map("properties","combine")));
    assertEquals(asList("1995","2010", "2015").toArray(), new ArrayBackedList(rel1.getProperty("year")).toArray());
  });
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void link() throws Exception {
  db.execute("UNWIND range(1,10) as id CREATE (n:Foo {id:id}) WITH collect(n) as nodes call apoc.nodes.link(nodes,'BAR') RETURN size(nodes) as len").close();
  ResourceIterator<Long> it = db.execute("MATCH (n:Foo {id:1})-[r:BAR*9]->() RETURN size(r) as len").columnAs("len");
  assertEquals(9L,(long)it.next());
  it.close();
}
@Test

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testFilterMax() throws Exception {
  db.execute("CREATE (:User {name:'Joe',gender:'male'}), (:User {gender:'female',name:'Jane'}), (:User {gender:'female',name:'Jenny'})").close();
  assertEquals("male",((Node)db.execute("CALL apoc.nodes.group(['User'],['gender'],null,{filter:{`User.count_*.max`:1}})").columnAs("node").next()).getProperty("gender"));
  assertFalse(db.execute("CALL apoc.nodes.group(['User'],['gender'],null,{filter:{`User.count_*.max`:0}})").hasNext());
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void shouldGetPageRankWithCypherExpectedResult() throws IOException
{
  db.execute( COMPANIES_QUERY ).close();
  Result result = db.execute("CALL apoc.algo.pageRankWithCypher({iterations:20, write:true}) ");
  ResourceIterator<Double> it = db.execute("MATCH (n) RETURN n.name as name, n.pagerank as score ORDER BY score DESC LIMIT 1").columnAs("score");
  assertTrue( it.hasNext() );
  assertEquals(PageRankAlgoTest.EXPECTED, it.next(), 0.1D);
  assertFalse( it.hasNext() );
  it.close();
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void shouldGetPageRankWithCypherExpectedResultWithoutNodes() throws IOException
{
  db.execute( COMPANIES_QUERY_LABEL ).close();
  Result result = db.execute("CALL apoc.algo.pageRankWithCypher({iterations:20, write:true, node_cypher:'none', rel_cypher:'MATCH (n:Company)-->(o) RETURN id(n) as source, id(o) as target', weight:null}) ");
  ResourceIterator<Double> it = db.execute("MATCH (n) WHERE exists(n.pagerank) RETURN n.name as name, n.pagerank as score ORDER BY score DESC LIMIT 1").columnAs("score");
  assertTrue( it.hasNext() );
  assertEquals( PageRankAlgoTest.EXPECTED, it.next(), 0.1D );
  assertFalse( it.hasNext() );
  it.close();
}
@Test

相关文章