[英]How to upload multiple json files under same folder dynamically without knowing the attributes in the json file to MongoDB using Java code
I am getting error:我收到错误:
exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.
线程“main”org.bson.BsonInvalidOperationException 中的异常:readStartDocument 只能在 CurrentBSONType 为 DOCUMENT 时调用,而 CurrentBSONType 为 ARRAY 时不能调用。
With the below code I am trying to insert only one json file.使用下面的代码,我试图只插入一个 json 文件。 Could you please help me?
请你帮助我好吗?
public class hallo {
public static void main( String args[] ) {
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
// Accessing the database
MongoDatabase database = mongo.getDatabase("dumpJsonFiles");
//System.out.println("Credentials ::"+ credential);
//Creating a collection
//database.createCollection("sampleCollection1");
System.out.println("Collection created successfully");
MongoCollection<Document> collection = database.getCollection("new_name");
System.out.println("Collection myCollection selected successfully");
Document document = new Document("title", "MongoDB")
.append("description", "database")
.append("likes", 100)
.append("url", "http://www.tutorialspoint.com/mongodb/")
.append("by", "tutorials point");
//Inserting document into the collection
collection.insertOne(document);
System.out.println("Document inserted successfully");
List<InsertOneModel<Document>> docs = new ArrayList<>();
int count = 0;
int batch = 100;
try (BufferedReader br = new BufferedReader(new FileReader("sample.json"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println("line is "+line);
docs.add(new InsertOneModel<>(Document.parse(line)));
count++;
if (count == batch) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
docs.clear();
count = 0;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (count > 0) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
}
//DBObject dbObject = (DBObject)JSON.parse("sample.json");
System.out.println("Document inserted 2 successfully");
}
}
The input JSON file sample contents (copied from comments):输入 JSON 文件示例内容(从注释中复制):
{
"Details":{
"Name":"AC",
"Rollno":"5978",
"section":"",
"Maths":"10",
"Science":"20",
"social":"20",
"English":"30"
}
}
Another json file is:另一个 json 文件是:
[
{
"Name":"Table",
"Description":"Used",
"Marks":[
{
"Social":"10",
"Science":"20",
"Maths":"30",
"English":"40",
"Hindi":"50",
"Computers":[
{
"Lab":"10",
"Theory":"20"
}
]
}
]
}
]
I am getting error:我收到错误:
exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.
线程“main”org.bson.BsonInvalidOperationException 中的异常:readStartDocument 只能在 CurrentBSONType 为 DOCUMENT 时调用,而 CurrentBSONType 为 ARRAY 时不能调用。
With the below code I am trying to insert only one json file.使用下面的代码,我试图只插入一个 json 文件。 Could you please help me?
请你帮助我好吗?
public class hallo {
public static void main( String args[] ) {
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
// Accessing the database
MongoDatabase database = mongo.getDatabase("dumpJsonFiles");
//System.out.println("Credentials ::"+ credential);
//Creating a collection
//database.createCollection("sampleCollection1");
System.out.println("Collection created successfully");
MongoCollection<Document> collection = database.getCollection("new_name");
System.out.println("Collection myCollection selected successfully");
Document document = new Document("title", "MongoDB")
.append("description", "database")
.append("likes", 100)
.append("url", "http://www.tutorialspoint.com/mongodb/")
.append("by", "tutorials point");
//Inserting document into the collection
collection.insertOne(document);
System.out.println("Document inserted successfully");
List<InsertOneModel<Document>> docs = new ArrayList<>();
int count = 0;
int batch = 100;
try (BufferedReader br = new BufferedReader(new FileReader("sample.json"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println("line is "+line);
docs.add(new InsertOneModel<>(Document.parse(line)));
count++;
if (count == batch) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
docs.clear();
count = 0;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (count > 0) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
}
//DBObject dbObject = (DBObject)JSON.parse("sample.json");
System.out.println("Document inserted 2 successfully");
}
}
The input JSON file sample contents (copied from comments):输入 JSON 文件示例内容(从注释中复制):
{
"Details":{
"Name":"AC",
"Rollno":"5978",
"section":"",
"Maths":"10",
"Science":"20",
"social":"20",
"English":"30"
}
}
Another json file is:另一个 json 文件是:
[
{
"Name":"Table",
"Description":"Used",
"Marks":[
{
"Social":"10",
"Science":"20",
"Maths":"30",
"English":"40",
"Hindi":"50",
"Computers":[
{
"Lab":"10",
"Theory":"20"
}
]
}
]
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.