Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 17, 2024 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/e9f500aeea76d21bc2ea9149aae13cde to your computer and use it in GitHub Desktop.
Save dacr/e9f500aeea76d21bc2ea9149aae13cde to your computer and use it in GitHub Desktop.
playing with arangodb using java driver and genson serdes / published by https://github.com/dacr/code-examples-manager #d81dc0ba-ca3e-44a7-a1a9-0c95553ef915/18b7a8382843d31c20319510e0fd79bcb6a938a6
// summary : playing with arangodb using java driver and genson serdes
// keywords : arangodb, graphdb, javadriver, genson, @testable,
// publish : gist, corporate
// 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 : d81dc0ba-ca3e-44a7-a1a9-0c95553ef915
// created-on : 2021-03-05T09:25:00Z
// managed-by : https://github.com/dacr/code-examples-manager
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// ---------------------
//> using scala "3.3.1"
//> using dep "com.arangodb:arangodb-java-driver:6.9.1"
//> using dep "com.owlike:genson:1.6"
// ---------------------
import java.text.SimpleDateFormat
import com.arangodb._
import com.arangodb.entity._
import com.arangodb.model.AqlQueryOptions
import com.arangodb.util.MapBuilder
import com.owlike.genson._
val dateFormatPattern = "yyyy-MM-dd'T'HH:mm:ss'Z'"
val builder =
new GensonBuilder()
.useDateFormat(new SimpleDateFormat(dateFormatPattern))
.setSkipNull(true)
.useIndentation(true)
.useConstructorWithArguments(true) // Take care, with true you may encounter IllegalArgumentException within asm.ClassReader
.useRuntimeType(true)
.useDateAsTimestamp(true)
val genson = builder.create()
val arango = {
new ArangoDB
.Builder()
.host("127.0.0.1", 8529)
.user("root")
.password("root")
.build()
}
try {
arango.createDatabase("example")
} catch {
case _ =>
}
val db = arango.db("example")
try {
db.createCollection("sample")
} catch {
case _ =>
}
def cursorBaseDocument: ArangoCursor[BaseDocument] =
db.query(
"FOR i IN @@collection RETURN i",
new MapBuilder().put("@collection", "sample").get(),
new AqlQueryOptions(),
classOf[BaseDocument]
)
cursorBaseDocument.forEach { basedoc =>
println(s"${basedoc.getId} ${basedoc.getRevision} ${basedoc.getProperties}")
val doc = basedoc.getProperties
val jsonString = genson.serialize(doc)
println(jsonString)
}
arango.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment