[英]Creating proper json from java objects (@Documents) for mongoimport
尝试创建一个文件 (json) 供 mongoimport 实用程序使用以填充 mongodb 中的 collections 以测试我们应用程序的性能。
我有一个现有的 spring 引导应用程序与 mongodb 交互。
这就是我想要做的:
然后使用 mongoimport 将生成的文件加载到 mongodb。
我看到我们可以使用:
Map<String, Object> map =
mapper.convertValue(SOME_JAVA_OBJECT, new TypeReference<Map<String, Object>>() {
});
org.bson.Document d = new Document();
d.putAll(map);
String json = d.toJson();
System.out.println(json);
但是,这个 JSON 的 LocaleDate 为(这不是我想要的):
"serviceDate": {"dayOfWeek": "THURSDAY", "dayOfYear": 231, "month": "AUGUST", "nano": 818930000, "year": 2021, "monthValue": 8, "dayOfMonth": 19, "hour": 11, "minute": 57, "second": 5, "chronology": {"id": "ISO", "calendarType": "iso8601"}},
我需要的是将其插入到 Mongo 中,日期值为:
serviceDate: ISODate("2021-08-14T06:37:15.722Z"
本质上是使用 Java 从现有的 POJO(@Document 实例)创建一个 json 文件,以便使用 mongoimport 实用程序进行批量上传。
这不是 spring 引导程序,而只是原始驱动程序访问程序,但这似乎为日期提供 eJSON 而不是 LocalDate ...
文件 pom.xml
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.barry</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
<finalName>Test</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.barry.Main</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
文件 Main.java
package test.barry;
public class Main {
public static void main(String[] args) {
com.mongodb.MongoClientSettings mongoClientSettings = com.mongodb.MongoClientSettings.builder()
.applyConnectionString(new com.mongodb.ConnectionString("mongodb://barry:barry@localhost:42011,localhost:42012,localhost:42013/?replicaSet=replSet&retryWrites=true&w=majority"))
.build();
com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongoClientSettings);
com.mongodb.client.MongoDatabase db = client.getDatabase("javatest");
com.mongodb.client.MongoCollection<org.bson.Document> collection = db.getCollection("test");
// START WITH A CLEARED OUT EMPTY COLLECTION
collection.deleteMany(new org.bson.BsonDocument());
// INSERT A DOCUMENT
org.bson.Document insertedDocument = new org.bson.Document();
insertedDocument.append("createdDate", new java.util.Date());
insertedDocument.append("firstName", "John");
insertedDocument.append("lastName", "Doe");
insertedDocument.append("age", 38);
insertedDocument.append("happinessScore", 95.6);
java.util.List<org.bson.Document> children = new java.util.ArrayList<org.bson.Document>();
org.bson.Document child1 = new org.bson.Document();
child1.append("firstName", "Timmy");
child1.append("dateOfBirth", new java.util.Date("January 1, 2011"));
children.add(child1);
org.bson.Document child2 = new org.bson.Document();
child2.append("firstName", "Tommy");
child2.append("dateOfBirth", new java.util.Date("July 1, 2012"));
children.add(child2);
org.bson.Document child3 = new org.bson.Document();
child3.append("firstName", "Tammy");
child3.append("dateOfBirth", new java.util.Date("November 1, 2013"));
children.add(child3);
insertedDocument.append("children", children);
org.bson.Document spouse = new org.bson.Document();
spouse.append("firstName", "Jane");
spouse.append("lastName", "Doe");
spouse.append("age", 34);
insertedDocument.append("spouse", spouse);
System.out.println(String.format("Before insert ObjectId: %s", insertedDocument.get("_id")));
collection.insertOne(insertedDocument);
System.out.println(String.format("After insert ObjectId: %s", insertedDocument.get("_id")));
System.out.println("");
System.out.println("JSON value is:");
System.out.println(insertedDocument.toJson());
// --------------------------------------------------------------------------
// QUERY DOCUMENT
org.bson.conversions.Bson filter = com.mongodb.client.model.Filters.eq("_id", insertedDocument.get("_id"));
org.bson.conversions.Bson sort = com.mongodb.client.model.Sorts.ascending("lastName");
org.bson.conversions.Bson projection = com.mongodb.client.model.Projections.exclude("lastName");
com.mongodb.client.FindIterable<org.bson.Document> iterable1 = collection.find(filter).sort(sort).skip(0).limit(1).projection(projection);
// AUTO-CLOSABLE TRY
try(com.mongodb.client.MongoCursor<org.bson.Document> cursor1 = iterable1.iterator())
{
while (cursor1.hasNext())
{
org.bson.Document queriedDocument1 = cursor1.next();
System.out.println(String.format("queriedDocument1: %s", queriedDocument1));
System.out.println("");
System.out.println("JSON value is:");
System.out.println(insertedDocument.toJson());
}
}
System.out.println("");
}
}
运行时的结果
Before insert ObjectId: null
15:19:13.672 [main] DEBUG org.mongodb.driver.protocol.command - Sending command '{"insert": "test", "ordered": true, "writeConcern": {"w": "majority"}, "txnNumber": 1, "$db": "javatest", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1629411546, "i": 1}}, "signature": {"hash": {"$binary": {"base64": "P0qNdQj1rWGa9efu4STXEW4ObCU=", "subType": "00"}}, "keyId": 6966701957889130499}}, "lsid": {"id": {"$binary": {"base64": "kiLR686WT/qOe2yThNehWg==", "subType": "04"}}}, "documents": [{"_id": {"$oid": "611ed8e1a563bc5abdd7957c"}, "createdDate": {"$date": "2021-08-19T22:19:13.628Z"}, "firstName": "John", "lastName": "Doe", "age": 38, "happinessScore": 95.6, "children": [{"firstName": "Timmy", "dateOfBirth": {"$date": "2011-01-01T08:00:00Z"}}, {"firstName": "Tommy", "dateOfBirth": {"$date": "2012-07-01T07:00:00Z"}}, {"firstName": "Tammy", "dateOfBirth": {"$date": "2013-11-01T07:00:00Z"}}], "spouse": {"firstName": "Jane", "lastName": "Doe", "age": 34}}]}' with request id 12 to database javatest on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
15:19:13.984 [main] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 12 completed successfully in 346.59 ms on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
After insert ObjectId: 611ed8e1a563bc5abdd7957c
JSON value is:
{"createdDate": {"$date": "2021-08-19T22:19:13.628Z"}, "firstName": "John", "lastName": "Doe", "age": 38, "happinessScore": 95.6, "children": [{"firstName": "Timmy", "dateOfBirth": {"$date": "2011-01-01T08:00:00Z"}}, {"firstName": "Tommy", "dateOfBirth": {"$date": "2012-07-01T07:00:00Z"}}, {"firstName": "Tammy", "dateOfBirth": {"$date": "2013-11-01T07:00:00Z"}}], "spouse": {"firstName": "Jane", "lastName": "Doe", "age": 34}, "_id": {"$oid": "611ed8e1a563bc5abdd7957c"}}
15:19:14.005 [main] DEBUG org.mongodb.driver.protocol.command - Sending command '{"find": "test", "filter": {"_id": {"$oid": "611ed8e1a563bc5abdd7957c"}}, "sort": {"lastName": 1}, "projection": {"lastName": 0}, "limit": 1, "$db": "javatest", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1629411553, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "if1LAHJtpGkJyNjBGcwbpUWXvho=", "subType": "00"}}, "keyId": 6966701957889130499}}, "lsid": {"id": {"$binary": {"base64": "kiLR686WT/qOe2yThNehWg==", "subType": "04"}}}}' with request id 13 to database javatest on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
15:19:14.006 [main] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 13 completed successfully in 4.11 ms on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
queriedDocument1: Document{{_id=611ed8e1a563bc5abdd7957c, createdDate=Thu Aug 19 15:19:13 PDT 2021, firstName=John, age=38, happinessScore=95.6, children=[Document{{firstName=Timmy, dateOfBirth=Sat Jan 01 00:00:00 PST 2011}}, Document{{firstName=Tommy, dateOfBirth=Sun Jul 01 00:00:00 PDT 2012}}, Document{{firstName=Tammy, dateOfBirth=Fri Nov 01 00:00:00 PDT 2013}}], spouse=Document{{firstName=Jane, lastName=Doe, age=34}}}}
JSON value is:
{"createdDate": {"$date": "2021-08-19T22:19:13.628Z"}, "firstName": "John", "lastName": "Doe", "age": 38, "happinessScore": 95.6, "children": [{"firstName": "Timmy", "dateOfBirth": {"$date": "2011-01-01T08:00:00Z"}}, {"firstName": "Tommy", "dateOfBirth": {"$date": "2012-07-01T07:00:00Z"}}, {"firstName": "Tammy", "dateOfBirth": {"$date": "2013-11-01T07:00:00Z"}}], "spouse": {"firstName": "Jane", "lastName": "Doe", "age": 34}, "_id": {"$oid": "611ed8e1a563bc5abdd7957c"}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.