Last active
May 25, 2024 10:20
-
-
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/97c3ac9b8ea336ff92d4590929050f2882cf7212
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 : 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.4.2" | |
//> 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