<!————-知识的力量是极端的(当然确定还大概有更轻巧的办法)———–!>

 

当笔者思考将省市区三级联合浮动数据从mysql转入mongodb时遇见了互连网无直接插入mongodb的示范(基本均是mysql插入示例)。于是想到利用json文件直接导入mongodb会相比easy(SQLyog如何导出json?)

在SQLyog中写二个json格式查询语句:(省市区example如下:)看一下就能够懂在那之中法则*

SELECT
‘{"code":"’ AS a,
cities.`cityid` AS b,
‘","name":"’ AS c,
cities.`city` AS d,
‘","provinceCode":"’ AS e,
cities.`provinceid` AS f,
‘"}’ AS g
FROM cities

 www.512.net 1www.512.net 2

慎选复制全部行到剪贴板:

www.512.net 3

www.512.net 4

接下去是将导出的json拼接成一个json文件:(在notpad++中以^查找来替换,拼接jsonarray串加array名(记得进行json校验与json压缩管理))

json在线校验U科雷傲L: 

json在线压缩U凯雷德L:

www.512.net 5www.512.net 6

最终以压缩成四个json文件的样式在java代码中解析

www.512.net 7

骨干杰出:

 

 1 @Test
 2 public void testProCityArea(){
 3 String fileName = "ProvCityArea.geojson";
 4 String path = System.getProperty("user.dir") + "\src\main\webapp\static\geojson\" + fileName;
 5 JSONObject jsonobject = JSONObject.parseObject(FileHelper.readFile(path));
 6 JSONArray provArray = jsonobject.getJSONArray("provinces"); 
 7 for (Object object : provArray) {
 8 JSONObject provJson = (JSONObject) object;
 9 Province province = new Province(GuidUtils.getInstance().getGuid(), provJson.getString("code"), provJson.getString("name"));
10 mongoTemplate.insert(province, "province");
11 }
12 JSONArray cityArray = jsonobject.getJSONArray("city");
13 for (Object object : cityArray) {
14 JSONObject cityJson = (JSONObject) object;
15 City city = new City(GuidUtils.getInstance().getGuid(), cityJson.getString("code"), cityJson.getString("name"), cityJson.getString("provinceCode"));
16 mongoTemplate.insert(city, "city");
17 }
18 JSONArray areaArray = jsonobject.getJSONArray("area");
19 for (Object object : areaArray) {
20 JSONObject areaJson = (JSONObject) object;
21 Area area = new Area(GuidUtils.getInstance().getGuid(), areaJson.getString("code"), areaJson.getString("name"), areaJson.getString("cityCode"));
22 mongoTemplate.insert(area, "area");
23 }
24 }

FileHelper:

 

 1 import java.io.BufferedReader;
 2 import java.io.FileInputStream;
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 
 6 public class FileHelper {
 7 
 8 public static String readFile(String path){
 9 BufferedReader reader = null;
10 String laststr = "";
11 try{
12 FileInputStream fileInputStream = new FileInputStream(path);
13 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
14 reader = new BufferedReader(inputStreamReader);
15 String tempString = null;
16 while((tempString = reader.readLine()) != null){
17 laststr += tempString;
18 }
19 reader.close();
20 }catch(IOException e){
21 e.printStackTrace();
22 }finally{
23 if(reader != null){
24 try {
25 reader.close();
26 } catch (IOException e) {
27 e.printStackTrace();
28 }
29 }
30 }
31 return laststr;    
32 }
33 
34 }

 

GuidUtils

 1 public class GuidUtils {
 2 
 3 private static final GuidUtils instance = new GuidUtils();
 4 
 5 private GuidUtils(){
 6 
 7 }
 8 
 9 public static GuidUtils getInstance() {
10 return instance;
11 }
12 
13 public String getGuid() {
14 UUID uuid = UUID.randomUUID();
15 String guid = uuid.toString();
16 guid = guid.replace("-", "");
17 return guid.toUpperCase();
18 }
19 
20 }

 

 

www.512.net,