目前上司让我写1000万条测试数据,
主键是90000000开始的逐一递增数值,其余的数据为a,b,c,d,e,f的数据的反复排列。
文件的单位为10万件一个,请问该如何实现?
回答的好加钱。
需要a-f循环的话,就像下面这样就可以了:
package test;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Test {
private static final int FILE_SIZE = 100;
private static final int FILE_COUNT = 100000;
private static final String[] FILE_CONENT = {"a","b","c","d","e","f"};
private static int INDEX = 90000000;
public static void main(String[] args) throws Exception {
int tmp = 100000000;
System.out.println(tmp);
for (int i = 1; i <= Test.FILE_SIZE; i++) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("testcase" + i + ".csv"))) {
for (int j = 0; j < Test.FILE_COUNT; j++) {
bw.write(++INDEX + "," + Test.FILE_CONENT[j%6]);
bw.newLine();
}
}
}
}
}
测试数据应该有表结构吧,知道字段就可以了,用循环生成主键。
我给你写个demo吧,最近帮人写了这类的数据。
这个可以用算法来处理
生成 100 份,每份 10 万条数据
import util.IO;
import java.io.File;
import java.nio.charset.StandardCharsets;
/**
* @Author xiaosheng
* @Date 2021/7/8
* @Description
*/
public class MakeTest {
/**
* 生成 test-num.csv 文件
*
* @param start 主键开始值
* @param end 主键结束值
* @param num 文件后缀
*/
public static void generateTestDate(int start, int end, int num) {
File file = new File("test");
if (!file.exists()) {
file.mkdir();
}
Character[] characters = {'a', 'b', 'c', 'd', 'e', 'f'};
StringBuilder sb = new StringBuilder();
for (int i = start, j = 0; i < end; i++) {
sb.append(i + "," + characters[j % 6] + "\n");
j++;
}
IO.stringToFile(file + File.separator + "test-" + num + ".csv", StandardCharsets.UTF_8, sb.toString());
}
public static void main(String[] args) {
// 生成 100 份,每份 10 万条数据
for (int i = 1; i <= 100; i++) {
generateTestDate(90000000 + ((i - 1) * 100000), 90000000 + (i * 100000), i);
}
}
}
package util;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
/**
* IO类是对Java11版本的部分IO功能的封装,以降低开发难度 <br>
* IO类提供了3个静态方法,并在主函数中提供了使用样例 <br>
*
* 参考资料:<br>
* https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html
* <br>
* https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html
*
*/
public class IO {
static OpenOption[] options;
static {
options = new OpenOption[] { StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE };
}
/**
* 读取文件到字符串列表
*
* @param file
* @param cs
* @return
*/
public static List<String> fileToStringList(String file, Charset cs) {
List<String> stringList = null;
try {
Path p = Paths.get(file);
stringList = Files.readAllLines(p, cs);
} catch (IOException e) {
System.err.println(e);
}
return stringList;
}
/**
* 将字符串写入文件
*
* @param file
* @param cs
* @param content
*/
public static void stringToFile(String file, Charset cs, String content) {
try {
Path p = Paths.get(file);
Files.writeString(p, content, cs, options);
} catch (IOException e) {
System.err.println(e);
}
}
/**
* 创建一个目录,默认递归创建
*
* @param dir
*/
public static void mkdir(String dir) {
try {
Path p = Paths.get(dir);
Files.createDirectories(p);
} catch (Exception e) {
System.err.println(e);
}
}
public static void main(String[] args) {
// 测试
// javac -cp src/ -d dist/ src/util/util.IO.java
// java -cp dist/ util.util.IO tmp/ name-list.csv tmp/name-list-copy.csv
String tmpDir = args[0];
String nameListFile = args[1];
String nameListCopyFile = args[2];
// 创建一个目录
IO.mkdir(tmpDir);
// 读取文件到字符串列表
List<String> lineList = IO.fileToStringList(nameListFile, StandardCharsets.UTF_8);
System.out.println(lineList);
// 将字符串写入文件
String content = String.join("\n", lineList);
IO.stringToFile(nameListCopyFile, StandardCharsets.UTF_8, content);
}
}
用 jdk8 给你写了一版,我觉得得加钱啊
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* @Author xiaosheng
* @Date 2021/7/8
* @Description
*/
public class MakeTest {
/**
* 生成 test-num.csv 文件
*
* @param start 主键开始值
* @param end 主键结束值
* @param num 文件后缀
*/
public static void generateTestDate(int start, int end, int num){
File file = new File("test");
if (!file.exists()) {
file.mkdir();
}
Character[] characters = {'a', 'b', 'c', 'd', 'e', 'f'};
StringBuilder sb = new StringBuilder();
for (int i = start, j = 0; i < end; i++) {
sb.append(i + "," + characters[j % 6] + "\n");
j++;
}
try(BufferedOutputStream ops = new BufferedOutputStream(new FileOutputStream(file + File.separator + "test-" + num + ".csv"))) {
ops.write(sb.toString().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 生成 100 份,每份 10 万条数据
for (int i = 1; i <= 100; i++) {
generateTestDate(90000000 + ((i - 1) * 100000), 90000000 + (i * 100000), i);
}
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Properties;
/**
* @Author xiaosheng
* @Date 2021/7/8
* @Description
*/
public class MakeTest {
/**
* 生成 test-num.csv 文件
*
* @param start 主键开始值
* @param end 主键结束值
* @param num 文件后缀
*/
public static void generateTestDate(int start, int end, int num){
File file = new File("test");
if (!file.exists()) {
file.mkdir();
}
Character[] characters = {'a', 'b', 'c', 'd', 'e', 'f'};
StringBuilder sb = new StringBuilder();
for (int i = start, j = 0; i < end; i++) {
sb.append("07,"+ "SB" + i + "," + i + "," + characters[j % 6] + "," + "c,d" +"\n");
j++;
}
String numStr = "";
if (num < 10) {
numStr = "0" + String.valueOf(num);
} else {
numStr = String.valueOf(num);
}
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
try(BufferedOutputStream ops = new BufferedOutputStream(new FileOutputStream(file + File.separator + "AS012-2021" + sdf.format(System.currentTimeMillis()) +"-" + numStr + ".csv"))) {
ops.write(sb.toString().getBytes("Shift-JIS"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Properties properties = new Properties();
InputStream resourceAsStream = MakeTest.class.getClassLoader().getResourceAsStream("resource/test.properties");
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
String singleNum = properties.getProperty("singleNum");
String sumNum = properties.getProperty("sumNum");
int fileNum = Integer.parseInt(sumNum)/Integer.parseInt(singleNum);
for (int i = 1; i <= fileNum; i++) {
generateTestDate(90000000 + ((i - 1) * 100000), 90000000 + (i * 100000), i);
}
}
}
配置文件 test.properties 放在 src 下的 resource 包中:
singleNum=100000
sumNum=10000000