先看看在MongoDB中的原始数据的样子:
Step1:导出csv
通过运行在cmd 中 运行mongoexport命令导出需要的数据:
PS:可能会有乱码,可以改一下编码格式
mongoexport -h localhost -d test -c orderList -f _id,createTime,shopName,userAddress,content.products.0.name,content.products.1.name,content.products.2.name,content.products.3.name,content.products.4.name,aoi --type=csv -o C:\Users\LK\Desktop\orderlist3.csv
Step1.1:将一些数据转换成自己需要的
导出的csv文件的内容,由于日期那一列是数字,需要转化为日期格式(我用java),如果本身没啥问题当然不用转。。。:
Long 转 date:
/**
* 读取csv文件并将里面的数字转化为date,再输出到csv文件
* @author LK
*
*/
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CSV_Num2Date {
/**
* CSV导出
* @throws IOException
*
* @throws Exception
*/
public static void main(String[] args) throws IOException {
exportCsv();
// System.out.println(num2date("yyyy/MM/dd HH:mm:ss","1483141509"));
}
public static void exportCsv() throws IOException {
String srcCSV = "F://JavaForOffer//CSV//data//orderlist.csv";
String targetFile = "C://Users/LK//Desktop//orederlist_time.csv";
CsvReader reader = new CsvReader(srcCSV, ',', Charset.forName("UTF-8"));
CsvWriter write =new CsvWriter(targetFile,',',Charset.forName("UTF-8"));
// 各字段以引号标记
write.setForceQualifier(false);
// 路过表头
reader.readHeaders();
// 逐条读取记录,直至读完
String[] header = {};
while (reader.readRecord()) {
// 把头保存起来
if (reader.getCurrentRecord()==0){
header = reader.getValues();
}
// 获取当前记录位置
System.out.print(reader.getCurrentRecord() + ":");
// 读取一条记录
System.out.println(reader.getRawRecord());
String[] tmp = {reader.getValues()[0],reader.getValues()[1]};
// 修改记录,并只写入第一个字段和第二字段
if (!header[1].equals(tmp[1]) && ("".equals(tmp[1])||tmp==null)){
tmp[1]="空";
write.writeRecord(tmp);
}else{
write.writeRecord(new String[]{reader.getValues()[0],num2date("yyyy/MM/dd HH:mm:ss",reader.getValues()[1])
,reader.getValues()[2],reader.getValues()[3],reader.getValues()[4],reader.getValues()[5]
,reader.getValues()[6],reader.getValues()[7],reader.getValues()[8],reader.getValues()[9]});
}
}
reader.close();
write.close();
}
public static String num2date(String dateFormat, String millSec_str){
long millSec = Long.parseLong(millSec_str);
// System.out.println("millSec:"+millSec);
long millSec_long = millSec * 1000;
// System.out.println("millSec_long"+millSec_long);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date = new Date(millSec_long);
return sdf.format(date);
}
}
转完以后:
Step2:将csv导入MySQL
天啊,这里遇到个问题,就是倒不进去!!!
问题1:
需要在mysql中先创建table
问题2:
一般csv第一行是各个列的名称,所以需要忽略第一行,或者直接在源文件删除第一行
问题3:
csv文件的存放 位置
Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
这是因为需要导入的文件不再MySQL认为的安全文件夹内,因此MySQL拒绝了导入操作。
使用如下语句可看到MySQL当前认为的安全文件夹,将文件放入文件夹内即可
SHOW VARIABLES LIKE 'secure_file_priv';
我这里的安全文件夹是C:\ProgramData\MySQL\MySQL Server 5.7\Uploads
load data infile 'C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\orderlist_time7.csv' into table orderlist fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';