public class RedisSentinelCluster {
private static final Logger logger = LoggerFactory.getLogger(RedisSentinelCluster.class);
private static JedisSentinelPool pool;
private RedisSentinelCluster() {
}
static {
init();
}
public static Jedis bulider() {
return pool.getResource();
}
public static JedisSentinelPool buliderPool() {
return pool;
}
private static void init() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(Integer.parseInt(PropertiesUtil.getValue("redis.maxTotal")));// 最大连接数
poolConfig.setMaxIdle(Integer.parseInt(PropertiesUtil.getValue("redis.maxIdle")));// 最大空闲数
poolConfig.setMaxWaitMillis(Integer.parseInt(PropertiesUtil.getValue("redis.maxWait")));// 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:Could not get a resource from the pool
String[] hostAndPortArray = PropertiesUtil.getValue("redis.cluster").split(",");
Set<String> sentinels = new HashSet<>();
for (int i = 0; i < hostAndPortArray.length; i++) {
String host = hostAndPortArray[i].split(":")[0];
int port = Integer.parseInt(hostAndPortArray[i].split(":")[1]);
sentinels.add(new HostAndPort(host, port).toString());
}
String password = PropertiesUtil.getValue("redis.auth");
String masterName = PropertiesUtil.getValue("redis.master.name");
pool = new JedisSentinelPool(masterName, sentinels, poolConfig, password);
System.out.println("the redis cluster pool init success: " + PropertiesUtil.getValue("redis.cluster"));
}
public void close() {
if (pool != null) {
try {
pool.close();
} catch (Exception e) {
logger.error("close the redis pool error:", e);
}
}
}
public class PropertiesUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties prop = null;
private static String properPath = ConstantsDefine.CONFIG_PATH + "*.properties";
//静态块中的内容会在类被加载的时候先被执行
static {
try {
prop = new Properties();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources(properPath);
for (Resource resource : resources) {
prop.load(resource.getInputStream());
logger.info("{} load complete!", resource.getFilename());
}
} catch (Exception e) {
e.printStackTrace();
}
}
//静态方法可以被类名直接调用
public static String getValue(String key) {
return prop.getProperty(key);
}
//静态方法可以被类名直接调用
public static boolean getBooleanValue(String key) {
return BooleanUtils.toBoolean(prop.getProperty(key));
}
}
spark使用;
city_distance_df.foreachPartition(x=>{
val jedis = RedisSentinelCluster.bulider()
try {
val pipeline = jedis.pipelined()
while (x.hasNext) {
val row = x.next()
val key = "city_distance:"+row.getAs("from_to").toString
val value: String = row.getAs("distance").toString
pipeline.set(key, value)
pipeline.expire(key, 60 * 30 * 1)
}
pipeline.sync()
} finally {
if (jedis != null)
jedis.close()
}
})
博客主要提及了Spark的使用相关内容,Spark是大数据开发领域的重要技术,在数据处理等方面有广泛应用。
869

被折叠的 条评论
为什么被折叠?



