Sqlyog can export mysql Data to mongodb and jsonmongodb in json format.

Source: Internet
Author: User

Sqlyog can export mysql Data to mongodb and jsonmongodb in json format.

<! ------------- The power of knowledge is infinite (of course, there must be a simpler method) -----------!>

 

When I was considering transferring provincial, municipal, and municipal data from mysql to mongodb, I encountered an example of no direct mongodb insertion on the Internet (basically mysql insertion examples ). Therefore, it would be easier to import data directly to mongodb using json files (how to export SQLyog json ?)

In SQLyog, write a query statement in json format: (the province, city, and district example are as follows:) You can understand the rules at a Glance *

SELECT
'{"Code": "' AS,
Cities. 'cityid' AS B,
'"," Name ":"' AS c,
Cities. 'city' AS d,
'"," ProvinceCode ":"' AS e,
Cities. 'provinceid' AS f,
'"}' AS g
FROM cities

 

Copy all rows to the clipboard:

Next we will splice the exported json into a json file: (replace it with ^ search in notpad ++, splice the jsonarray string with the array name (remember to perform json validation and json compression ))

Json online validation URL: http://www.bejson.com/

Json online compressed URL: http://www.sojson.com/yasuo.html

Finally, parse the code in java code in the form of compressing it into a json File

Backbone essence:

@ 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 "));
Using template. 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 "));
Using template. 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 "));
Using template. 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 ();
}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.