Java로 Redis를 사용하고자하므로 간단한 예제 (아래 코드 참조)로 this으로 시작했습니다.JRedis 클라이언트를 사용한 Hello World 예제
첫 번째 실행은 키, 값 쌍을 데이터베이스에 삽입하고 두 번째 실행은 값을 가져 와서 화면에 인쇄해야합니다. 내가 이해하지 못하는 두 가지 문제가 있습니다
나는 그것이 값이 명령 아니라고 오류를 출력 키를 삽입하면 "PING에 대한 오류 응답 => 알 수없는 명령을 ERR"안녕하세요 다시를! "이것은 나에게 이해가되지 않는다. 왜 문자열 값이 명령이라고 생각 하는가?
두 번째로 키 값을 인쇄 할 때 키의 값만 인쇄하면 가치가 있지만 "Hello Again!"이 인쇄 될 것으로 예상했습니다.
감사합니다. LP! 내가 어떤 대답을 얻을 수 있고이 예제의 저자을 시도했지만 행운하지 않았다
import org.jredis.ClientRuntimeException;
import org.jredis.JRedis;
import org.jredis.ProviderException;
import org.jredis.RedisException;
import org.jredis.protocol.Command;
import org.jredis.ri.alphazero.JRedisClient;
import static org.jredis.ri.alphazero.support.DefaultCodec.*;
/**
* [TODO: document me!]
*
* @author Joubin Houshyar ([email protected])
* @version alpha.0, Apr 15, 2009
* @since alpha.0
*
*/
public class HelloAgain {
public static final String key = "jredis::examples::HelloAgain::message";
public static void main(String[] args) {
String password = "";
if(args.length > 0) password = args[0];
new HelloAgain().run(password);
}
private void run(String password) {
try {
JRedis jredis = new JRedisClient("localhost", 6379, "jredis", 0);
jredis.ping();
if(!jredis.exists(key)) {
jredis.set(key, "Hello Again!");
System.out.format("Hello! You should run me again!\n");
}
else {
String msg = toStr (jredis.get(key));
System.out.format("%s\n", msg);
}
jredis.quit();
}
catch (RedisException e){
if (e.getCommand()==Command.PING){
System.out.format("I'll need that password! Try again with password as command line arg for this program.\n");
}
}
catch (ProviderException e){
System.out.format("Oh no, an 'un-documented feature': %s\nKindly report it.", e.getMessage());
}
catch (ClientRuntimeException e){
System.out.format("%s\n", e.getMessage());
}
}
}