[英]Generate Unique Identifier in Very Big Loop - Java
the question i am going to ask is very old and i think it asked on SO 5-10 times. 我要问的问题已经很老了,我认为它问了5-10次。
but mine has a different situation. 但是我的情况有所不同。
Read my Problem before making it duplicate by so many wise (over) SO users. 在让如此多的明智(过度)SO用户重复该问题之前,请阅读我的问题。
I am importing CSV sheet containing 10K records in my application. 我要在应用程序中导入包含1万条记录的CSV表。
My logic works in following manner, 我的逻辑按以下方式工作:
(1) Validate & Import Sheet (2) Save to the database if record does not exist (1)验证并导入表(2)如果记录不存在,则保存到数据库
Step 2 is done for each and every record of the sheet. 对工作表的每个记录执行步骤2。
in the step -2 i have to generate UUID to identify a particular record later , 在步骤-2中,我必须生成UUID以便以后标识特定记录,
in my first solution 在我的第一个解决方案中
// this might be unique in some cases
String id = UUID.randomUUID().toString();
but i checked that it does generate unique id in each case , for example if i import 10 sheet one by one with different records in it, all 10 times i am getting duplicate key error from database for at least 4000 times in each import and save operation, 但是我检查了它是否在每种情况下都会生成唯一的ID,例如,如果我一张一张地导入10张带有不同记录的表,那么所有10次我在每次导入和保存时都会从数据库中获取重复键错误至少4000次操作
that means that out of 10,000 key generation it generates only 6000 unique ids. 这意味着在10,000个密钥生成中,它仅生成6000个唯一ID。
so then i generate an alphanumeric code which length is 6 , some thing like 所以我生成了一个长度为6的字母数字代码,就像
eSv3h7 eSv3h7
and append it to previously generated id and hence get the following id 并将其附加到先前生成的ID中,从而获得以下ID
d545f2b2-63ab-4703-89b0-f2f8eca02154-eSv3h7 d545f2b2-63ab-4703-89b0-f2f8eca02154-eSv3h7
after testing still there is a problem of id duplication. 经过测试仍然存在id重复的问题。
I also tried several combination mentioned here and on other sites but still there is a same problem of id duplication, 我还尝试了此处和其他网站上提到的几种组合,但是仍然存在相同的ID重复问题,
Now i am wondering that this occurs only for 10k records saving in loop , actually i need to import sheet which is having 8 million records in it 现在我想知道,这种情况仅在循环保存10k条记录时才会发生,实际上我需要导入其中包含800万条记录的工作表
so how can i solve my problem of generating a Unique id in my particular case ? 那么如何解决我在特殊情况下生成唯一ID的问题?
Try this thing at you end. 尝试此操作。
loop through 1 to 10,000 generate uuid in the loop store it somewhere in simple text file 循环1到10,000在循环中生成uuid将其存储在简单文本文件中的某个位置
then make a simple program to find the duplicates from them , if you do not find any one duplicate in first attempt , repeat all above steps again and again and i am sure you will find duplicates. 然后制作一个简单的程序从中查找重复项,如果您第一次尝试找不到任何重复项,请一次又一次重复上述所有步骤,我相信您会找到重复项。
in the past i am also strong believer of the same thing that UUID will never generates duplicates, share me your result of above test. 过去,我也坚信UUID永远不会生成重复项,请与我分享您的上述测试结果。
This is the method which is called by each record of the sheet to be saved by caller method' loop. 这是要由调用方方法的循环保存的工作表的每个记录所调用的方法。
@Override @Override
public void preSynchronizedServiceExecution(ServiceData sData,
ValueObject valueObject) throws BlfException {
PropertyVO pVO = (PropertyVO) valueObject;
ArrayList<CountyAuctionPropertyVO> capList = pVO
.getCountyAuctionPropertyList();
for (CountyAuctionPropertyVO caVO : capList) {
TadFrameworkUtil.processValueObjectKeyProperty(caVO, true);
TadFrameworkUtil.processValueObjectKeyProperty(caVO
.getPropertyLastOwner(), true);
TadFrameworkUtil.processValueObjectKeyProperty(caVO
.getPropertyLastOwner().getAdd(), true);
}
ArrayList<PropertyAminitiesVO> amList = pVO.getPropertyAminitiesList();
for (PropertyAminitiesVO pamVO : amList) {
TadFrameworkUtil.processValueObjectKeyProperty(pamVO, true);
}
ArrayList<PropertyAttributesVO> atList = pVO
.getPropertyAttributesList();
for (PropertyAttributesVO patVO : atList) {
TadFrameworkUtil.processValueObjectKeyProperty(patVO, true);
}
TadFrameworkUtil.processValueObjectKeyProperty(pVO, true);
TadFrameworkUtil.processValueObjectKeyProperty(pVO.getSiteAdd(), true);
}
Following is id generation method 以下是id生成方法
public static String generateUUID() throws BlfException {
// this might be unique in some cases
String id = UUID.randomUUID().toString();
// introduce custom random string in mixing of upper and lower
// alphabets,
// which is 6 character long
// and append it to generated GUID.
String rs = randomString(6);
id = id.concat("-").concat(rs);
return id;
}
public static void processValueObjectKeyProperty(ValueObject valueObject,
boolean create) throws BlfException {
String key = (String) BlfConverter.getKey(valueObject);
if (!StringUtility.isStringNonEmpty(key)) {
throw new BlfException(valueObject.getObjectName()
+ "- key property does not exist.");
}
if (create) {
String id = generateUUID();
valueObject.setProperty(key, id);
} else {
String exisitingId = valueObject.getProperty(key);
if (!StringUtility.isStringNonEmpty(exisitingId)) {
String id = generateUUID();
valueObject.setProperty(key, id);
}
}
}
The random string method is just a simple methods of 2 lines which generates alpha numeric random string of length 6. 随机字符串方法只是2行的简单方法,它会生成长度为6的字母数字随机字符串。
please ask me if you need anything more so i can post here. 请问我是否还需要其他任何东西,所以我可以在这里发布。
d545f2b2-63ab-4703-89b0-f2f8eca02154-eSv3h7
6f06fa28-6f36-4ed4-926b-9fef86d002b3-DZ2LaE
20142d05-f456-4d72-b845-b6819443b480-xzypQr
67b2a353-e7b4-4245-90a0-e9fca8644713-AgSQZm
8213b275-2cb1-4d37-aff0-316a47e5b780-vMIwv9
and i am getting accurate result from database if i need to fetch it from there. 如果我需要从那里获取数据,我将从数据库中获得准确的结果。
Thanks 谢谢
Thanks for all your user who seriously study my question and spent some time to help me in solving it. 感谢所有认真研究我的问题并花一些时间帮助我解决问题的用户。
I found that the error was in Database layer of business logic foundation. 我发现错误出在业务逻辑基础的数据库层。
One of the Object needs to Updated but it was created using previous existing id so that i was getting the Duplicate Primary key Error.
对象之一需要更新,但是它是使用以前的现有ID创建的,因此我遇到了重复主键错误。
I develop a Unit Test for id generation and tested UUID for more than one billion key , it is guaranteed to be unique, it is true in all circumstances.
我开发了用于ID生成的单元测试,并测试了超过十亿个密钥的UUID,它保证是唯一的,在任何情况下都是如此。
Thanks again to everyone. 再次感谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.