Introduction
该工具可以根据数据库表的描述文件模拟数据库表记录。
Github:https://github.com/chinaeagle001/DataEmulator
Get Started
1 编写描述文件
使用时仅需编写一个数据库表的描述文件,即可生成相应行数的数据库表记录。数据库表描述文件的格式如下:
table:tablename1 rows
fieldname1 fieldtype1 [min,max)|{val1,...,valn} pk
...
fieldnamen fieldtypen [min,max)|{val1,...,valn}
table:tablename2
fieldname1 fieldtype1 [min,max)|{val1,...,valn}
...
fieldnamen fieldtypen [min,max)|{val1,...,valn}
每个数据库表描述文件可以包含单个或多个表的描述信息,每个表的第一行以table:
开头,其后接表名,rows
表示将要生成的数据库表记录的行数,为可选信息。其后n行为表字段的描述信息,fieldname
为字段名,fieldtype
为字段类型,目前支持int
、long
、float
、double
、string
和varchar
,string
和varchar
使用时的效果相同。[min,max)|{val1,...,valn}
为对该字段取值的约束,[min,max)
表示一个取值范围,表示可以取到符合数据类型的从min
(包含)到max
(不包含)的所有值,{val1,...,valn}
表示一个取值集合,即该字段只能取该集合内的值,不能取其他值,取值范围和取值集合只能设置一个,不能二者同时设置,但是可以都不设置。最后还可以设置字段为pk
,即表示该字段为主键,不可插入重复值。
NOTE:
- 字段的每一项描述信息之间以单个空格隔开
- 取值范围和取值集合信息内部不能包含空格
- 取值范围和取值集合不可同时设置
- 取值范围、取值集合和主键约束为可选信息,可以不填
- 数据库表记录的生成行数要么用表名后面的
rows
进行设置,要么在执行EmulateService.emulate(rows)
时进行设置,至少要设置一处,若两处都设置,则以EmulateService.emulate(rows)
为准。
一个描述文件的实例:
table:person 20
id int [1,1000) pk
age int [1,120)
gender int {0,1}
money double [0,1000000)
coef double
count long
2 根据描述文件生成数据库插入语句
- 方式1:
EmulateService service = new EmulateService("/Users/Admin/Documents/tableinfo");
System.out.println(service.emulate());
- 方式2
EmulateService service = new EmulateService("/Users/Admin/Documents/tableinfo");
System.out.println(service.emulate(5)); //为emulator()函数中传入rows参数
3 插入语句生成结果
- 生成方式1(20行记录):
insert into person(id,age,gender,money,coef,count) values
(291,94,0,53412.21630881065,0.5779976127815049,7692545845379467444),
(239,24,0,261361.0344283964,0.0508673570556899,-3620809042008399078),
(684,101,0,157092.75796406597,0.3966187758324009,4607446640046108149),
(920,47,0,624595.0526048058,0.986777681078574,8927642045409361120),
(911,93,1,786998.2163966655,0.45354157926164607,-4313673043051624580),
(20,92,1,664855.2384607359,0.8735817151239903,-7508906734973982553),
(11,28,1,124671.13618049053,0.9734051779586359,5351526109491550133),
(867,81,0,909606.4988407127,0.21212965912771842,897966445284003410),
(991,29,0,600485.2315198382,0.8919574955689352,5936093823740762813),
(720,81,0,537669.789256303,0.13056074959413433,6167679450490020352),
(872,37,0,230361.65505933613,0.6349515457561012,-8273762954102208134),
(613,105,1,173744.61354678206,0.24459185391975935,4334141001737976751),
(892,16,1,93626.73601215122,0.38629133067688814,-4814762211230589829),
(740,69,1,877803.2932818531,0.5004153012631574,3841626164676692156),
(164,46,0,16203.3794649159,0.786204377521101,-3540351928265827338),
(413,114,1,898904.6100799685,0.7137705656016263,-1021667364372863749),
(179,51,0,361814.34572503634,0.451531569768709,233900225619619757),
(230,69,0,969495.3066159218,0.6184311865576196,-843724423055990433),
(310,78,1,908526.264743164,0.30687587651597414,8955454434489590540),
(63,44,0,658074.8661432414,0.22715526857430424,-2933386510045183448)
- 生成方式2(5行记录):
insert into person(id,age,gender,money,coef,count) values
(291,94,0,53412.21630881065,0.5779976127815049,7692545845379467444),
(239,24,0,261361.0344283964,0.0508673570556899,-3620809042008399078),
(684,101,0,157092.75796406597,0.3966187758324009,4607446640046108149),
(920,47,0,624595.0526048058,0.986777681078574,8927642045409361120),
(911,93,1,786998.2163966655,0.45354157926164607,-4313673043051624580)
转载:http://blog.csdn.net/foreverling/article/details/51395588