//运用Java产生海量数据
package generate;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Random;
import java.io.*;
public class GenData {
static char[] allBase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
static char[] lowerBase = "abcdefghijklmnopqrstuvwxyz".toCharArray();
static char[] numberBase = new char[]{'0','1','2','3','4','5','6','7','8','9'};
static String[] sexBase = new String[] {"M","F"};
static String[] hospitalName =
new String[] {"Jingjiang Hospital","Qingyang Hospital","Jingniu Hospital","Wuhou Hospital",
"Chenghua Hospital","Gaoxin Hospital","Longquanyi Hospital","Wenjiang Hospital","Xindu Hospital",
"Qingbaijiang Hospital","Shuangliu Hospital","Pixian Hospital","Pujiang Hospital",
"Dayi Hospital","Jintang Hospital","Xinjin Hospital","Dujiangyan Hospital","Pengzhou Hospital",
"Qionglai Hospital","Chongzhou Hospital"};
static String[] hospitalID =
new String[]{"00","01","02","03","04","05","06","07","08","09","10","11","12",
"13","14","15","16","17","18","19"};
static String[] bodyPart = new String[]{"SKULL","CSPINE","TSPINE","LSPINE","SSPINE","COCCYX","CHEST",
"CLAVICLE","BREAST","ABDOMEN","PELVIS","HIP","SHOULDER","ELBOW","KNEE","ANKLE","HAND",
"FOOT","EXTREMITY"};
static String[] bodyPartID =
new String[]{"00","01","02","03","04","05","06","07","08","09","10","11","12",
"13","14","15","16","17","18"};
public static String[] getPatient(){
char[] id = new char[18];
Random rand = new Random();
String[] str = new String[6];
DecimalFormat df2 = new DecimalFormat("00");
//产生身份证除去 出生日期 外的其他部分
for(int i=0;i<18;i++){
int num = rand.nextInt(numberBase.length);
id[i]=numberBase[num];
if(i == 5) i = 13;
}
//产生身份在的出生日期
int year = 1900+rand.nextInt(2013-1900);
int month = 1+rand.nextInt(11);
String monthStr = df2.format(month);
int day = 1+rand.nextInt(28);
String dayStr = df2.format(day);
for (int i=0;i<4;i++){
id[i+6] = String.valueOf(year).toCharArray()[i];
}
for (int i=0;i<2;i++){
id[i+10] = monthStr.toCharArray()[i];
}
for (int i=0;i<2;i++){
id[i+12] = dayStr.toCharArray()[i];
}
str[0] = String.valueOf(id);
str[3] = Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(day); //Date of birth
// Generate study date
int syear = year + rand.nextInt(2014-year);
month = 1+rand.nextInt(11);
day = 1+rand.nextInt(28);
str[4] = Integer.toString(syear)+"-"+Integer.toString(month)+"-"+Integer.toString(day);
str[5] = Integer.toString(syear)+Integer.toString(month)+Integer.toString(day);
int xingByte = 2+rand.nextInt(7);
int minByte = 2+rand.nextInt(7);
char[] xing = new char[xingByte];
char[] min = new char[minByte];
xing[0]=(char)(lowerBase[Integer.parseInt(String.valueOf(id[0]+id[9]))%26]-32);
for(int i=1;i<xingByte;i++){
xing[i]=lowerBase[Integer.parseInt(String.valueOf(id[i]+id[9+i]))%26];
}
min[0] = (char)(lowerBase[Integer.parseInt(String.valueOf(id[xingByte]+id[1+xingByte]))%26]-32);
for(int i=1;i<minByte;i++){
min[i]=lowerBase[Integer.parseInt(String.valueOf(id[i+xingByte]+id[1+i+xingByte]))%26];
}
str[1] = String.valueOf(xing)+' '+String.valueOf(min);
str[2] = sexBase[id[1]%2];
return str;
}
public static String getDoctorName(){
Random d = new Random();
int xingByte = 2+d.nextInt(7);
int minByte = 2+d.nextInt(7);
char[] xing = new char[xingByte];
char[] min = new char[minByte];
int temp = d.nextInt(25);
xing[0] = (char)(lowerBase[temp]-32);
for(int i=1;i<xingByte;i++){
temp = d.nextInt(25);
xing[i] = lowerBase[temp];
}
temp = d.nextInt(25);
min[0] = (char)(lowerBase[temp]-32);
for(int i=1;i<minByte;i++){
temp = d.nextInt(25);
min[i] = lowerBase[temp];
}
return String.valueOf(xing)+" "+String.valueOf(min);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long start;
start = System.currentTimeMillis();
String[] line = new String[23];
Random r = new Random();
DecimalFormat df4 = new DecimalFormat("0000");
long count = 0;
FileWriter fw = new FileWriter("/home/hadoop/WorkHbase/test10wan.txt");
for(int hc=0;hc<20;hc++){
line[0] = hospitalID[hc];
line[1] = ", ";
line[2] = hospitalName[hc];
line[3] = ", ";
for(int bc=0; bc<19; bc++ ){
line[4] = bodyPart[bc];
line[5] = ", ";
for(int dc=0;dc<6;dc++){
line[6] = hospitalID[hc]+bodyPartID[bc]+df4.format(dc); //doctor id = 2 + 2 + 4 = 8
line[7] = ", ";
line[8] = getDoctorName(); //doctor name
line[9] = ", ";
for(int pc=0;pc<9;pc++){
String[] pat = new String[6];
pat = getPatient();
line[10] = pat[0]; //patient id
line[11] = ", ";
line[12] = pat[1]; //patient name
line[13] = ", ";
line[14] = pat[2]; //patient sex,
line[15] = ", ";
line[16] = pat[3]; //date of birth
line[17] = ", ";
for(int sc=0, m=1+r.nextInt(3);sc<m;sc++){
line[18] = pat[4]; //study date
line[19] = ", ";
line[20] = line[6]+line[10]+pat[5]+df4.format(sc); //study id
//=doctor id + patient id + study date + count number
//=8 +18 + 8 + 4 = 38
line[21] = ", ";
for(int urlc=0,n=1+r.nextInt(4);urlc<n;urlc++){
line[22] = "/"+hospitalName[hc]+"/"+bodyPart[bc]+"/"+line[20]+"/"+df4.format(urlc);
for(int s = 0; s<23; s++){
fw.write(line[s].toString());
}
count ++;
if (count == 100000) {
fw.close();
System.out.printf("Time used: %d\n",(System.currentTimeMillis()-start));
System.out.println(count);
System.out.printf("The hospital count is %d\n", hc+1);
System.out.printf("The body part count is %d\n", bc+1);
System.exit(0);
}
fw.write("\n");
}
}
}
}
}
}
fw.close();
System.out.printf("Time used: %d\n",(System.currentTimeMillis()-start));
System.out.println(count);
}
}
运用Java产生海量测试数据
最新推荐文章于 2024-08-06 09:23:19 发布