Created
June 30, 2019 18:50
-
-
Save ramazanpolat/81700b06a575227c66242fd88e3178d7 to your computer and use it in GitHub Desktop.
Java Bean to JSON
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
package com.rambo; | |
import com.arangodb.ArangoDBException; | |
import com.arangodb.entity.BaseDocument; | |
import com.arangodb.entity.CollectionEntity; | |
import com.arangodb.velocypack.VPack; | |
import com.arangodb.velocypack.VPackSlice; | |
import org.msgpack.MessagePack; | |
import com.arangodb.ArangoDB; | |
import java.io.IOException; | |
import java.time.LocalDateTime; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.concurrent.atomic.AtomicInteger; | |
/** | |
* Hello world! | |
*/ | |
public class App { | |
private static final String ARANGO_USER = "root"; | |
private static final String ARANGO_PASSWORD = "closedSesame"; | |
private static String ARANGO_HOST = "134.209.83.172"; | |
private static int ARANGO_PORT = 8080; | |
private static ArangoDB arangoDB = new ArangoDB.Builder().host(ARANGO_HOST, ARANGO_PORT) | |
.user(ARANGO_USER).password(ARANGO_PASSWORD).build(); | |
private static String dbName = "cache"; | |
private static String collectionName = "cache1"; | |
public static void messagePack() throws IOException { | |
System.out.println("Hello World!"); | |
Person person = new Person("Ali", 21); | |
System.out.println(person); | |
MessagePack mp = new MessagePack(); | |
mp.register(Person.class); | |
byte[] raw = mp.write(person); | |
System.out.println(raw); | |
Person person2 = new Person("a", 1); | |
person2 = mp.read(raw, Person.class); | |
System.out.println(person2); | |
// Value dynamic = mp.read(raw); | |
// System.out.println(dynamic); | |
} | |
private static void velocyPack() { | |
Person entity = new Person("Veli", 34); | |
VPack vpack = new VPack.Builder().build(); | |
VPackSlice slice = vpack.serialize(entity); | |
System.out.println(slice.toString()); | |
Person entity2 = vpack.deserialize(slice, Person.class); | |
System.out.println(entity2); | |
} | |
// Create collection | |
private static void arango1() { | |
final ArangoDB arangoDB = new ArangoDB.Builder().host("127.0.0.1", 8529).build(); | |
String dbName = "cache"; | |
String collectionName = "firstCollection4"; | |
try { | |
CollectionEntity myArangoCollection = arangoDB.db(dbName).createCollection(collectionName); | |
System.out.println("Collection created: " + myArangoCollection.getName()); | |
} catch (ArangoDBException e) { | |
System.err.println("Failed to create collection: " + collectionName + "; " + e.getMessage()); | |
} finally { | |
arangoDB.shutdown(); | |
} | |
} | |
private static void arango3() { | |
BaseDocument myObject = new BaseDocument(); | |
myObject.addAttribute("a", "Foo"); | |
myObject.addAttribute("b", 42); | |
try { | |
arangoDB.db(dbName).collection(collectionName).insertDocument(myObject); | |
System.out.println("Document created"); | |
} catch (ArangoDBException e) { | |
System.err.println("Failed to create document. " + e.getMessage()); | |
} finally { | |
arangoDB.shutdown(); | |
} | |
} | |
private static void arangoDate() { | |
//Create formatter | |
long unixTime = System.currentTimeMillis() / 1000L; | |
fineTuneDateTime(ZonedDateTime.now()); | |
DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_INSTANT; | |
//Local date time instance | |
LocalDateTime localDateTime = LocalDateTime.now(); | |
System.out.println("localDateTime"); | |
System.out.println(localDateTime); | |
ZonedDateTime now = ZonedDateTime.now(); | |
System.out.println("ZonedDateTime"); | |
System.out.println(now); | |
System.out.println(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now)); | |
} | |
private static String fineTuneDateTime(ZonedDateTime zdt) { | |
System.out.println("--=== fineTuneDateTime ===---"); | |
System.out.print("zdt:"); | |
System.out.println(zdt); | |
String nowStr = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'H.m.s'Z'x").format(zdt); | |
System.out.print("nowStr:"); | |
System.out.println(nowStr+" ZONE: "+zdt.getOffset().toString()); | |
// nowStr = 2019-05-27T05:34:16.245+03:00 | |
String[] parts = nowStr.split("."); | |
// parts[0] = 2019-05-27T02:20:08 | |
// parts[1] = 225Z | |
System.out.print("parts:"); | |
System.out.println(String.valueOf(parts)); | |
System.out.println("-----------------------------"); | |
return "1"; | |
} | |
private static void insertDate1() { | |
/* | |
* * `2019-05-27`: May 27th 2019, 00:00:00 UTC time | |
* `2019-05-27T21:20:00`: May 27th 2019, 21:20:00 UTC time | |
* `2019-05-27T21:20:00Z`: May 27th 2019, 21:20:00 UTC time | |
* `2019-05-27T21:20:00.123Z`: May 27th 2019, 21:20:00.123 UTC time | |
* `2019-05-27T21:20:00.123+01:30`: May 27th 2019, 21:20:00.123, +01:30 offset from UTC time | |
* `2019-05-27T21:20:00.123-02:00`: May 27th 2019, 21:20:00.123, -02:00 offset from UTC time | |
* */ | |
String[] dates = new String[]{ | |
"2019-05-29T02:18:00", | |
"2019-05-29T02:18:00Z", | |
"2019-05-29T02:18:00+03:00", | |
"2019-05-29T02:18:00.123Z", | |
"2019-05-29T02:18:00.123+03:00", | |
"2019-05-29T05:18:00", //not removed | |
"2019-05-29T05:18:00Z", //not removed | |
"2019-05-29T05:18:00+03:00", | |
"2019-05-29T05:18:00.123Z",//not removed | |
"2019-05-29T05:18:00.123+03:00", | |
}; | |
try { | |
AtomicInteger index = new AtomicInteger(); | |
for (String date : dates) { | |
BaseDocument myObject = new BaseDocument(); | |
myObject.addAttribute("index", String.valueOf(index.get())); | |
myObject.addAttribute("expireAfter", date); | |
arangoDB.db(dbName).collection(collectionName).insertDocument(myObject); | |
System.out.println(myObject); | |
index.getAndIncrement(); | |
} | |
System.out.println("Finished"); | |
} catch (ArangoDBException e) { | |
System.err.println("Failed to create document. " + e.getMessage()); | |
} finally { | |
arangoDB.shutdown(); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
insertDate1(); | |
// arangoDate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment