首先需要搭建一个java环境
安装OpenJDK 1.8
解压到/usr/local/目录,并将安装目录重命名
tar -zxvf apache-maven-3.6.3-bin.tar.gz -C /usr/local/ &&
mv /usr/local/apache-maven-3.6.3/ /usr/local/maven
将Maven的可执行文件目录加入到系统环境变量中
echo "export PATH=$PATH:/usr/local/maven/bin" >> /etc/profile
source /etc/profile
打开镜像仓库配置文件
vim /usr/local/maven/conf/settings.xml
添加镜像地址
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
创建并进入工作空间
mkdir -p demo/src/main/java/test/ && cd demo
vim打开GameRankSample.java文件
vim src/main/java/test/GameRankSample.java
输入:set paste
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
public class GameRankSample {
static int TOTAL_SIZE = 20;
public static void main(String[] args)
{
//Redis数据库连接地址
String host = "xxxxxxxxxx.m.cnhz1.kvstore.aliyuncs.com";
//连接密码
String password = "password";
int port = 6379;
Jedis jedis = new Jedis(host, port);
try {
String authString = jedis.auth(password);
if (!authString.equals("OK"))
{
System.err.println("AUTH Failed: " + authString);
return;
}
//Key(键)
String key = "游戏名:奔跑吧,阿里!";
//清除可能的已有数据
jedis.del(key);
//模拟生成若干个游戏玩家
List<String> playerList = new ArrayList<String>();
for (int i = 0; i < TOTAL_SIZE; ++i)
{
//随机生成每个玩家的ID
playerList.add(UUID.randomUUID().toString());
}
System.out.println("输入所有玩家 ");
//记录每个玩家的得分
for (int i = 0; i < playerList.size(); i++)
{
//随机生成数字,模拟玩家的游戏得分
int score = (int)(Math.random()*5000);
String member = playerList.get(i);
System.out.println("玩家ID:" + member + ", 玩家得分: " + score);
//将玩家的ID和得分,都加到对应key的SortedSet中去
jedis.zadd(key, score, member);
}
//输出打印全部玩家排行榜
System.out.println();
System.out.println(" "+key);
System.out.println(" 全部玩家排行榜 ");
//从对应key的SortedSet中获取已经排好序的玩家列表
Set<Tuple> scoreList = jedis.zrevrangeWithScores(key, 0, -1);
for (Tuple item : scoreList) {
System.out.println("玩家ID:"+item.getElement()+", 玩家得分:"+Double.valueOf(item.getScore()).intValue());
}
//输出打印Top5玩家排行榜
System.out.println();
System.out.println(" "+key);
System.out.println(" Top 玩家");
scoreList = jedis.zrevrangeWithScores(key, 0, 4);
for (Tuple item : scoreList) {
System.out.println("玩家ID:"+item.getElement()+", 玩家得分:"+Double.valueOf(item.getScore()).intValue());
}
//输出打印特定玩家列表
System.out.println();
System.out.println(" "+key);
System.out.println(" 积分在1000至2000的玩家");
//从对应key的SortedSet中获取已经积分在1000至2000的玩家列表
scoreList = jedis.zrangeByScoreWithScores(key, 1000, 2000);
for (Tuple item : scoreList) {
System.out.println("玩家ID:"+item.getElement()+", 玩家得分:"+Double.valueOf(item.getScore()).intValue());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
jedis.quit();
jedis.close();
}
}
}
完成后输入:set nopaste 回车
vim打开pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--jar入口类,格式Package.ClassName -->
<mainClass>test.GameRankSample</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>