欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

操作手册-安全切分List,redis分布式锁,hive翻页

程序员文章站 2022-05-29 16:01:18
...

切分list

public Integer size=10;//每个list的大小
public Integer getBatchNum(List list){
	int num = list.size()/size;
	if(list.size()%size != 0){
		num+=1;
	}
	return num;
}
public List<List>  splitList(List list){
	int num = getBatchNum(list);
	List resultList = new ArrayList();
	for(int i=0;i<num;i++){
		int beg = i*size;
		int end = (i+1) *size<list.size()?(i+1)*size:list.size();
		List subList = list.subList(beg,end);
		resultList.add(subList);
		
	}
	return resultList;
}

hive 分页

select  * from ( select *,row_number() over (order by xxx_ID ) rm from 库名.表名 ) a where rm between 1 and 1000

redis 分布式锁(redis 集群不可用)

@Autowired
StringRedisTemplate stringRedisTemplate;
private static final String RELEASE_LOCK_SCRIPT=" if redis.call ('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";
private static final LOCK_SCRIPT=" if redis.call('setnx',KEYS[1],ARGV[1]) ==1 then return redis.call('expire',KEYS[1],ARGV[2]) else return o end";
public Boolean tryLock(String lockKey, String clientId,long seconds){
 DefaultRedisScript<Boolean> redisScript = new org.springframework,data,redis.core.script.DefaultRedisScript<>(LOCK_SCRIPT,Boolean.class);
 Boolean isLock = stringRedisTemplate.execute(redisScript,Collections.singletonList(lockkey),clientId,String.valueof(seconds));
 return isLock;
}