scala用于生成测试数据demo
生成数据格式为
1,test1,56,sanxing,7,2019-11-10
2,test87,18,huawei,12,2019-9-13
3,test50,23,huawei,0,2019-4-18
4,test22,24,apple,1,2019-2-19
5,test47,40,sanxing,11,2019-5-1
6,test26,37,huawei,1,2019-3-23
7,test80,17,sanxing,12,2019-3-10
8,test67,48,vivo,7,2019-4-18
9,test40,33,sanxing,8,2019-1-0
import java.io.FileWriter
import scala.util.Random
/**
* * 产生数据结构
* DataStructure("ID","Username","Userage","PhoneType,"Click","LoginTime")
*/
object MockData {
private val pathData = "input/scalatest"
private val maxRecord = 1000
private val age = 60
private val brand = Array("apple", "huawei", "sanxing", "vivo")
//定义方法产生数据
//define a method to make data
def Create(): Unit = {
val rand = new Random()
val writeData: FileWriter = new FileWriter(pathData, true)
for (i <- 1 to maxRecord) {
var dataage = rand.nextInt(age)
if (dataage < 15) { //如果年龄小于15需要对数据进处理
dataage + 15
}
//产生品牌类型
var phonePlus = brand(rand.nextInt(4))
var click = rand.nextInt(15) //点击次数
var name = "test" + rand.nextInt(100).toString
var month = rand.nextInt(12) + 1
var logintime = "2019-" + month + "-" + rand.nextInt(31)
writeData.write(i + "," + name + "," + dataage + "," + phonePlus + "," + click + "," + logintime)
println(i + "," + name + "," + dataage + "," + phonePlus + "," + click + "," + logintime)
//这也是换行符,功能和"\n"是一致的,但是此种写法屏蔽了 Windows和Linux的区别
writeData.write(System.getProperty("line.separator"))
}
writeData.flush()
writeData.close()
}
//测试方法
def main(args: Array[String]): Unit = {
Create()
System.exit(1)
}
}
2java版
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class CreateTestData {
public static void main(String[] args) {
writer();
}
public static void writer() {
PrintWriter PrintWriter;
String[] name = {"张三", "李四", "王五"};
String age; //年龄
String[] sex = {"男", "女", "其他"}; //性别
String[] creeer = {"程序员", "教师", "律师"}; //职业
Random rand = new Random();
String str = null;
try {
PrintWriter = new PrintWriter(new FileWriter("D:\\creereInfo.txt"));
for (int i = 0; i < 100; i++) {
int num = rand.nextInt(3);
str = name[num] + "," + rand.nextInt(100) + "," + sex[num] + "," + creeer[num];
PrintWriter.printf(i + "," + str);
PrintWriter.printf("\n");
PrintWriter.flush();
}
PrintWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}