生成测试数据

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();
        }
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值