远程从Mongodb 数据库导出数据为json文件
2017-10-10 21:09
441 查看
Java 连接Mongodb 数据库并且从数据库导出数据为Json文件。
需要导入三个包:
bson-3.4.3.jar
mongo-java-driver-3.4.3.jar
mongodb-driver-core-3.4.3.jar
当然版本可以改变。可以从http://mongodb.github.io/mongo-java-driver/ 这里下载
mongodb.properties:
url=localhost
port=27017
将数据导出为Json文件
public static void main(String[] args) throws IOException {
String url = null;
int port=27017;
//加载
Properties p = new Properties();
InputStream input = MongoDbConn.class.getClassLoader().getResourceAsStream("mongodb.properties");
try {
p.load(input);
url = p.getProperty("url");
port = Integer.valueOf(p.getProperty("port"));
} catch (IOException e) {
e.printStackTrace();
}finally{
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MongoClient mongoClient = new MongoClient(url, port);
MongoDatabase md =mongoClient.getDatabase("local");
System.out.println(md.getName());
MongoCollection<Document> mo=md.getCollection("student");
FindIterable<Document> f=mo.find();
MongoCursor<Document> cm=f.iterator();
String Json = "";
while(cm.hasNext()){
Json = Json + cm.next().toJson();
}
System.out.println(Json);
//将Json文件 存入文件
String Path = "F:/" + "1.txt";
File file = new File(Path);
if(file.exists()){
System.out.println("文件存在");
}
else{
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(Json.getBytes());
fileOutputStream.close();
System.out.println("文件创建成功");
}
}
view
pl
需要导入三个包:
bson-3.4.3.jar
mongo-java-driver-3.4.3.jar
mongodb-driver-core-3.4.3.jar
当然版本可以改变。可以从http://mongodb.github.io/mongo-java-driver/ 这里下载
mongodb.properties:
url=localhost
port=27017
将数据导出为Json文件
public static void main(String[] args) throws IOException {
String url = null;
int port=27017;
//加载
Properties p = new Properties();
InputStream input = MongoDbConn.class.getClassLoader().getResourceAsStream("mongodb.properties");
try {
p.load(input);
url = p.getProperty("url");
port = Integer.valueOf(p.getProperty("port"));
} catch (IOException e) {
e.printStackTrace();
}finally{
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MongoClient mongoClient = new MongoClient(url, port);
MongoDatabase md =mongoClient.getDatabase("local");
System.out.println(md.getName());
MongoCollection<Document> mo=md.getCollection("student");
FindIterable<Document> f=mo.find();
MongoCursor<Document> cm=f.iterator();
String Json = "";
while(cm.hasNext()){
Json = Json + cm.next().toJson();
}
System.out.println(Json);
//将Json文件 存入文件
String Path = "F:/" + "1.txt";
File file = new File(Path);
if(file.exists()){
System.out.println("文件存在");
}
else{
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(Json.getBytes());
fileOutputStream.close();
System.out.println("文件创建成功");
}
}
view
pl
相关文章推荐
- 远程从Mongodb 数据库中 导出数据为Excel 文件
- Oracle impdp 远程导出数据到本地数据库,不生成备份文件
- 远程从Mongodb 数据库中导出为CSV文件
- django 用 json 文件 导入和导出 数据库的数据
- mongoDB导出数据库所有集合内容到json文件
- mongodb-导出数据到csv文件或json文件
- MongoDB中的数据导出为JSON文件时报错“Failed: not authorized on XXX to...”
- SQL 数据的导入导出,对远程(MSsql,OracleAccess,)数据库的操作以及读取Excel,txt文件中的数据
- mongoDB导出数据库所有集合内容到json文件
- sql server 小技巧(7) 导出完整sql server 数据库成一个sql文件,包含表结构及数据
- Java实现从数据库导出大量数据记录并保存到文件的方法
- 将一个sql文件在远程的数据库执行、导入其数据
- MongoDB中的数据导出为excel CSV 文件
- Linux下 将Oracle 数据库数据导出为dump文件
- Oracle数据泵远程导入文件到本地数据库
- mysql 数据库、数据表文件导入导出
- 使用JDBC获取数据库数据,并生成json格式文件(省市区三级联动)
- Mongodb导出 Csv 或者 Json 文件
- MongoDB数据库的文件备份恢复以及文件导入导出
- MongoDB中的数据导出为excel CSV 文件