sqlyog导出json数据格式支持mysql数据转存mongodb

 百家乐-操作     |      2019-11-14 20:10

<!-------------知识的力量是无限的(当然肯定还有更简单的方法)-----------!>

 

当我考虑将省市区三级联动数据从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

 图片 1图片 2

选取复制所有行到剪贴板:

图片 3

图片 4

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

json在线校验URL: 

json在线压缩URL:

图片 5图片 6

最后以压缩成一个json文件的形式在java代码中解析

图片 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 }

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

上一篇:没有了 下一篇:没有了