HashRingFile hashRingFile = new HashRingFile("/home/HashRing.json");
ConnectionInfo c1 = new ConnectionInfo(); // mysql connection info
c1.port = 3306;
c1.host = "your host";
c1.dbName = "your bd name";
c1.password = "your password";
c1.userName = "your username";
ConnectionInfo c2 = new ConnectionInfo();
c2.port = 3306;
c2.host = "your host";
c2.dbName = "your bd name";
c2.password= "your password";
c2.userName = "your username";
hashRingFile.init(2, Arrays.asList(c1, c2));
// hashRingFile.addNode(connectionInfo);
// hashRingFile.remove();
// hashRingFile.remove(1);
// 尝试存储数据(或修改数据)
Data testData = new Data();
testData.setIdAndMd5("your bucketName", "your fileName");
testData.content = "your data".getBytes(StandardCharsets.UTF_8);
hashRingFile.saveData(testData);
// 尝试查询数据
Data qData = new Data();
qData.setIdAndMd5("your bucketName", "your fileName");
qData = hashRingFile.getData(qData); // qData为null,说明数据不存在,否则取qData.content就能得到内容
if (qData != null) {
System.out.println(new String(qData.content)); // 如果不是字符串,不用new String()... 这里仅仅示例
System.out.println("bucket name: " + qData.getBucketName());
System.out.println("file name: " + qData.getFileName());
} else {
System.out.println("数据不存在!");
}
// 尝试删除数据
Data deleteData = new Data();
deleteData.setIdAndMd5("your bucketName", "your fileName");
if (!hashRingFile.deleteData(deleteData)) { // 返回true说明数据存在并删除成功,false说明数据不存在,有时候也达到了目的!
System.out.println("数据删除失败,数据不存在!");
}
// 如果不再使用对象存储服务,可以调用destroy(),只是内存级别的关闭,持久化文件依旧保留,再次构造还可以正常使用
// hashRingFile.destroy();