packagejizg.study.maven.hello;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.Map.Entry;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importorg.apache.commons.collections.map.HashedMap;importorg.springframework.jdbc.core.JdbcTemplate;/*** Created by lkpnotice on 2017/5/25.*/
public classDataGenerator {/*五种类型*/String[] typesArray= new String[]{"type1","type2","type3","type4","type5"};int assetValRange = 1000;int assetValLimit = 10;
String content= "lkpnotice@163.com";public static final int mainFlushLimit = 100;public static final int refFlushLimit = 200;publicDataGenerator(){
}publicDataGenerator(JdbcTemplate jdbc){this.jdbcTemplate =jdbc;
}public static voidmain(String[] args){
DataGenerator gen= newDataGenerator();
Map> unit =gen.genUnit();
gen.genUnitMain(1,unit);
gen.genUnitRef(1,unit);
System.out.println(gen.mainMap);
System.out.println(gen.refMap);
}/***
*@paramidBase*/
public void genLoopCase1(long idBase,longammount){for (long cc = 0 ; cc < ammount ; cc++){long idNow = idBase + 1 +cc;
genLoopOnce(idNow);
}
flushLoopLeft();
}/**/
public void genLoopOnce(long id){
/*gen new unit*/Map> unit =genUnit();/*transform to main and ref unit object*/genUnitMain(id,unit);
genUnitRef(id,unit);if (mainCount >= mainFlushLimit){/*main table db op and flush ,empty cache and counter*/handleDbMainBatchInsert();
}if (refCount >= refFlushLimit){/*maintable db op and flush ,empty cache and counter*/handlerDbRefBatchInsert();
}
}/*left batch commit*/
public voidflushLoopLeft(){if (mainCount >= 0){/*main table db op and flush ,empty cache and counter*/handleDbMainBatchInsert();
}if (refCount >= 0){/*maintable db op and flush ,empty cache and counter*/handlerDbRefBatchInsert();
}
}public voidclearMain(){
mainMap.clear();
}public voidclearRef(){
refMap.clear();
}
JdbcTemplate jdbcTemplate;
String mainInsertSql="insert into json_test_main_0001(id,content,json_str) values (?,?,?)";public voidhandleDbMainBatchInsert(){
List batchArgs = new ArrayList();for(MainTable main: mainMap.values()
) {
Long id=main.getId();
String content=main.getContent();
String jsonStr=main.getJsonStr();
batchArgs.add(newObject[]{id,content,jsonStr});
}int[] returns =jdbcTemplate.batchUpdate(mainInsertSql, batchArgs);
clearMain();
}
String refInsertSql="insert into json_test_ref_0001(ref_id,asset_type,asset_value) values (?,?,?)";public voidhandlerDbRefBatchInsert(){
List batchArgs = new ArrayList();for (Listrefs:refMap.values()
) {for(RefTable ref: refs
) {//ref.getId();
Long refId =ref.getRefId();
String assetType=ref.getAssetType();
String assetValue=ref.getAssetVal();
batchArgs.add(newObject[]{refId,assetType,assetValue});
}
}int[] returns =jdbcTemplate.batchUpdate(refInsertSql, batchArgs);
clearRef();
}/*RefTable*/Map> refMap = newHashMap();public int refCount=0;public void genUnitRef(long id,Map>unit){
List refList = newArrayList();
Iterator>> iter =unit.entrySet().iterator();while(iter.hasNext()){
Entry> en =iter.next();
String key=en.getKey();
List valList =en.getValue();for(String str:valList
) {
RefTable ref= newRefTable();
ref.setRefId(id);
ref.setAssetType(key);
ref.setAssetVal(str);
refList.add(ref);
}
}
refCount+=refList.size();
refMap.put(id,refList);
}/*mainTable*/Map mainMap = newHashedMap();public int mainCount = 0;public void genUnitMain(long id,Map>unit){
String jsonStr=JSON.toJSONString(unit);
MainTable main= newMainTable();
main.setId(id);
main.setContent(content);
main.setJsonStr(jsonStr);
mainCount+= 1;
mainMap.put(id,main);
}public Map>genUnit(){/*gen unit type*/
int len =typesArray.length;/*gen how many types of this id*/
int count = (int)Math.round(Math.max((Math.random() * len) ,1));
Map> unit = newHashedMap();/*gen each type of this id*/
for(int i = 0;i < count ; i++){int index = (int)Math.round(Math.max((Math.random() * len) -1,0));
String assetType=typesArray[index];
List tmpType =unit.get(assetType);if (null ==tmpType){/*gen how many val of this type*/
int cnt = (int)Math.round(Math.max((Math.random() * assetValLimit),1));
List valList = newArrayList();/*gen each val of this type*/
for (int j = 0; j< cnt ; j++){long assetVal = Math.round(Math.random() *assetValRange);
valList.add(newLong(assetVal).toString());
}
unit.put(assetType,valList);
}else{
i--;
}
}returnunit;
}classMainTable {longid;
String content;
String jsonStr;
JSONObject json;public longgetId() {returnid;
}public void setId(longid) {this.id =id;
}publicString getContent() {returncontent;
}public voidsetContent(String content) {this.content =content;
}publicString getJsonStr() {returnjsonStr;
}public voidsetJsonStr(String jsonStr) {this.jsonStr =jsonStr;
}publicJSONObject getJson() {returnjson;
}public voidsetJson(JSONObject json) {this.json =json;
}
@OverridepublicString toString() {return id + " : " + content + " : " +jsonStr;
}
}classRefTable {longid;longrefId;
String assetType;
String assetVal;public longgetId() {returnid;
}public void setId(longid) {this.id =id;
}public longgetRefId() {returnrefId;
}public void setRefId(longrefId) {this.refId =refId;
}publicString getAssetType() {returnassetType;
}public voidsetAssetType(String assetType) {this.assetType =assetType;
}publicString getAssetVal() {returnassetVal;
}public voidsetAssetVal(String assetVal) {this.assetVal =assetVal;
}
@OverridepublicString toString() {return id +" : " + refId + " : " + assetType + " : " +assetVal;
}
}
}