import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.io.FileUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.IOException;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringdemoApplicationTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void contextLoads() {
String fileName = "C:\\wea.chn.hor\\2019\\03\\12\\wea.chn.hor.201903120500_UTC";
File f = new File(fileName);
if (!f.exists()) {
return;
}
// Query query=new Query(Criteria.where("Datetime").is("201903121300"));
// mongoTemplate.remove(query,"SURF_WEA_CHN_PHE_HOR");
try {
String content = FileUtils.readFileToString(f, "utf-8");
JSONObject obj = JSON.parseObject(content);
JSONArray ds = obj.getJSONArray("DS");
if (ds == null || ds.isEmpty()) {
return;
}
for(int i=0;i<ds.size();i++){
JSONObject jsonObject = ds.getJSONObject(i);
Update update = new Update();
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
if(entry.getKey().equals("Datetime")){
String datetime1 = DateUtils.getTimeString(entry.getValue().toString(), DateUtils.SECOND_FORMAT1, DateUtils.MINUTE_FORMAT);
String datetime = DateUtils.futureHour(datetime1, DateUtils.MINUTE_FORMAT, 8);
entry.setValue(datetime);
update.set("Datetime",datetime);
}else if(!entry.getKey().equals("Admin_Code_CHN")&&!entry.getKey().equals("Station_Id_C")){
// entry.setValue(Float.valueOf(entry.getValue().toString()));
update.set(entry.getKey(),Double.valueOf(entry.getValue().toString()));
}
// double[] loc = {Double.valueOf(jsonObject.get("Lon").toString()), Double.valueOf(jsonObject.get("Lat").toString())};
GeoJsonPoint loc = new GeoJsonPoint(Double.valueOf(jsonObject.get("Lon").toString()), Double.valueOf(jsonObject.get("Lat").toString()));
update.set("loc", loc);
}
Query query=new Query(Criteria.where("Datetime").is(jsonObject.get("Datetime")).and("Station_Id_C").is(jsonObject.get("Station_Id_C")));
mongoTemplate.upsert(query,update,"SURF_WEA_CHN_PHE_HOR");
}
// mongoTemplate.insert(ds, "SURF_WEA_CHN_PHE_HOR");
} catch (IOException e) {
e.printStackTrace();
}
}
//db.SURF_WEA_CHN_PHE_HOR.createIndex( { loc : "2dsphere" } )
@Test
public void contextFind() {
Criteria c = Criteria.where("Datetime").is("201903121300").and("loc").nearSphere(new Point(117,36.6));
String eleValueRanges = "CLO_Cov:[6,7)";
String[] eleValues = eleValueRanges.split(";");
for (int i = 0; i < eleValues.length; i++) {
String[] attrs = eleValues[i].split(":");
String sigbegin = "";
String sigend = "";
if (attrs[1].startsWith("(")) {
sigbegin = "$gt";
} else if (attrs[1].startsWith("[")) {
sigbegin = "$gte";
}
if (attrs[1].endsWith("]")) {
sigend = "$lte";
} else if (attrs[1].endsWith(")")) {
sigend = "$lt";
}
if (sigend.equals("")) {
String[] eqparam = attrs[1].split(",");
c.and(attrs[0]).in(stringArrayToDoubleArray(eqparam));
} else {
String num1 = attrs[1].substring(1, attrs[1].indexOf(","));
String num2 = attrs[1].substring(attrs[1].indexOf(",") + 1, attrs[1].length() - 1);
if (!num1.equals("")) {
if(sigbegin.equals("$gt")){
if (!num2.equals("")) {
if(sigend.equals("$lt")){
c.and(attrs[0]).gt(Double.parseDouble(num1)).lt(Double.parseDouble(num2));
}else{
c.and(attrs[0]).gt(Double.parseDouble(num1)).lte(Double.parseDouble(num2));
}
}else{
c.and(attrs[0]).gt(Double.parseDouble(num1));
}
}else{
if (!num2.equals("")) {
if(sigend.equals("$lt")){
c.and(attrs[0]).gte(Double.parseDouble(num1)).lt(Double.parseDouble(num2));
}else{
c.and(attrs[0]).gte(Double.parseDouble(num1)).lte(Double.parseDouble(num2));
}
}else{
c.and(attrs[0]).gte(Double.parseDouble(num1));
}
}
}
}
}
Query query=new Query(c);
System.out.println(query);
query.fields().include("Station_Id_C");
query.fields().include("Datetime");
query.fields().include("CLO_Cov");
query.fields().exclude("_id");
Object obj = mongoTemplate.find(query.limit(2),Object.class,"SURF_WEA_CHN_PHE_HOR");
System.out.println(obj);
}
@Test
public void contextFind2() {
String eleValueRanges = "CLO_Cov:[6,7)";
String[] eleValues = eleValueRanges.split(";");
DBObject dbObject = new BasicDBObject();
// dbObject.put("Datetime", "201903121300");
for (int i = 0; i < eleValues.length; i++) {
String[] attrs = eleValues[i].split(":");
String sigbegin = "";
String sigend = "";
if (attrs[1].startsWith("(")) {
sigbegin = "$gt";
} else if (attrs[1].startsWith("[")) {
sigbegin = "$gte";
}
if (attrs[1].endsWith("]")) {
sigend = "$lte";
} else if (attrs[1].endsWith(")")) {
sigend = "$lt";
}
if (sigend.equals("") && sigend.equals("")) {
String[] eqparam = attrs[1].split(",");
dbObject.put(attrs[0], new BasicDBObject("$in", stringArrayToDoubleArray(eqparam)));
} else {
BasicDBObject paramdb = new BasicDBObject();
String num1 = attrs[1].substring(1, attrs[1].indexOf(","));
String num2 = attrs[1].substring(attrs[1].indexOf(",") + 1, attrs[1].length() - 1);
if (!num1.equals("")) {
paramdb.append(sigbegin, Float.parseFloat(num1));
}
if (!num2.equals("")) {
paramdb.append(sigend, Float.parseFloat(num2));
}
dbObject.put(attrs[0], paramdb);
}
}
Query query = new BasicQuery(dbObject);
System.out.println(query);
query.addCriteria(Criteria.where("Datetime").is("201903121300").and("loc").nearSphere(new Point(117,36.6)));
query.fields().include("Station_Id_C");
// query.fields().include("Datetime");
query.fields().include("CLO_Cov");
query.fields().exclude("_id");
Object obj = mongoTemplate.find(query.limit(2),Object.class,"SURF_WEA_CHN_PHE_HOR");
System.out.println(obj);
}
public static Double[] stringArrayToDoubleArray(String[] strs) {
Double[] ds = new Double[strs.length];
for (int i = 0; i < strs.length; i++) {
ds[i] = Double.valueOf(strs[i]);
}
return ds;
}
}
json增删改到MongoDB
最新推荐文章于 2024-04-17 14:22:28 发布
本文介绍了一种使用Spring Boot框架和MongoDB数据库处理大量气象数据的方法。通过解析特定格式的数据文件,将数据转换并存储到MongoDB中,同时实现了基于地理位置和数值范围的查询功能,展示了如何在复杂条件下进行高效数据检索。
摘要由CSDN通过智能技术生成