88bf必发 14

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

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

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

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

 

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

 88bf必发 188bf必发 2

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

88bf必发 3

88bf必发 4

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

json在线校验URL:http://www.bejson.com/ 

json在线压缩URL:http://www.sojson.com/yasuo.html

88bf必发 588bf必发 6

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

88bf必发 7

主干精华:

@Test
public void testProCityArea(){
String fileName = "ProvCityArea.geojson";
String path = System.getProperty("user.dir") + "\\src\\main\\webapp\\static\\geojson\\" + fileName;
JSONObject jsonobject = JSONObject.parseObject(FileHelper.readFile(path));
JSONArray provArray = jsonobject.getJSONArray("provinces");
for (Object object : provArray) {
JSONObject provJson = (JSONObject) object;
Province province = new Province(GuidUtils.getInstance().getGuid(), provJson.getString("code"), provJson.getString("name"));
mongoTemplate.insert(province, "province");
}
JSONArray cityArray = jsonobject.getJSONArray("city");
for (Object object : cityArray) {
JSONObject cityJson = (JSONObject) object;
City city = new City(GuidUtils.getInstance().getGuid(), cityJson.getString("code"), cityJson.getString("name"), cityJson.getString("provinceCode"));
mongoTemplate.insert(city, "city");
}
JSONArray areaArray = jsonobject.getJSONArray("area");
for (Object object : areaArray) {
JSONObject areaJson = (JSONObject) object;
Area area = new Area(GuidUtils.getInstance().getGuid(), areaJson.getString("code"), areaJson.getString("name"), areaJson.getString("cityCode"));
mongoTemplate.insert(area, "area");
}
}

FileHelper:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileHelper {

public static String readFile(String path){
BufferedReader reader = null;
String laststr = "";
try{
FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
reader = new BufferedReader(inputStreamReader);
String tempString = null;
while((tempString = reader.readLine()) != null){
laststr += tempString;
}
reader.close();
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return laststr;
}

}

GuidUtils

public class GuidUtils {

private static final GuidUtils instance = new GuidUtils();

private GuidUtils(){

}

public static GuidUtils getInstance() {
return instance;
}

public String getGuid() {
UUID uuid = UUID.randomUUID();
String guid = uuid.toString();
guid = guid.replace("-", "");
return guid.toUpperCase();
}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

http://www.bkjia.com/Windowsjc/1180973.htmlwww.bkjia.comtruehttp://www.bkjia.com/Windowsjc/1180973.htmlTechArticlesqlyog导出json数据格式支持mysql数据转存mongodb,jsonmongodb
!————-知识的力量是无限的(当然肯定还有更简单的方法)———–!
当我考虑将省…

 

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

 88bf必发 888bf必发 9

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

88bf必发 10

88bf必发 11

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

json在线校验URL:http://www.bejson.com/ 

json在线压缩URL:http://www.sojson.com/yasuo.html

88bf必发 1288bf必发 13

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

88bf必发 14

主干精华:

 

 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 }

 

 

 

  

 

 

 

 

 

88bf必发,