mysql json效率_Mysql Json性能测试数据构建

该博客展示了如何使用Java生成用于Mysql JSON性能测试的数据。通过DataGenerator类,作者创建了一个数据生成器,用于构造JSON字符串并将其插入到Mysql的主表和引用表中。内容包括数据单元生成、主表和引用表的处理以及批量插入操作。
摘要由CSDN通过智能技术生成

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值