Created
June 28, 2024 23:14
-
-
Save dacr/71b71b55c65d7c9dd4f05693d7640b7e to your computer and use it in GitHub Desktop.
neo4j cypher - many inserts / published by https://github.com/dacr/code-examples-manager #43425fdc-b629-48de-9f3c-2b52835ee43a/1984df79d3b068e9ab6c33af6dfd18026fa0bf9c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : neo4j cypher - many inserts | |
// keywords : scala, scalatest, neo4j, neotypes, cypher, dsl, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 43425fdc-b629-48de-9f3c-2b52835ee43a | |
// created-on : 2024-06-26T14:49:20+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep io.github.neotypes::neotypes-core:1.1.0 | |
//> using dep org.neo4j.test:neo4j-harness:5.20.0 | |
// --------------------- | |
import neotypes.GraphDatabase | |
import neotypes.mappers.ResultMapper | |
import neotypes.syntax.all.* | |
import org.neo4j.driver.AuthTokens | |
import scala.util.Using | |
import scala.concurrent.{Await, Future} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
case class Record(name: String, age: Int) | |
val builder = | |
org.neo4j.harness.Neo4jBuilders | |
.newInProcessBuilder() | |
Using(builder.build()) { embedded => | |
val driver = GraphDatabase.asyncDriver[Future](embedded.boltURI(), AuthTokens.none()) | |
val insertsQueries = for { | |
num <- LazyList.from(1).take(1000) | |
name = s"joe$num" | |
age = scala.util.Random.between(1, 130) | |
qry = c"CREATE (:Person {name: $name, age: $age})" | |
} yield qry | |
val insertsQueriesBatchFuture = for { | |
batch <- insertsQueries.grouped(100) | |
batchFuture = batch.reduce( _ + _).execute.void(driver) | |
} yield batchFuture | |
Await.ready(Future.sequence(insertsQueriesBatchFuture), 20.seconds) | |
val countFuture = | |
c"MATCH (p: Person) return count(p)" | |
.query(ResultMapper.fromFunction(Record.apply)) | |
.single(driver) | |
val dump = for { | |
count <- countFuture | |
} yield println(s"Found $count records") | |
Await.result(dump, 5.seconds) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment