键值存储接口
您好,有以下接口:
public interface KeyValueStore {
public void put(String key, byte[] value);
public byte[] get(String key);
public void putAll(Map<String,byte[]> pairs);
public Map<String,byte[]> getAll(Collection<String> keys);
}
我有以下实现先前接口的客户端:
public class Client implements KeyValueStore{
@Override
public void put(String key, byte[] value) {
}
@Override
public byte[] get(String key) {
}
@Override
public void putAll(Map<String, byte[]> pairs) {
}
@Override
public Map<String, byte[]> getAll(Collection<String> keys) {
}
}
如何实现客户端以便与给定接口进行交互?我想用客户端-服务器架构来做到这一点。
我想要的另一件事是一个知道数据存储位置的“名称服务器”(基本上它具有到“存储服务器”的键映射),以及实际保存数据的其他几个服务器。
Hi have the following interface:
public interface KeyValueStore {
public void put(String key, byte[] value);
public byte[] get(String key);
public void putAll(Map<String,byte[]> pairs);
public Map<String,byte[]> getAll(Collection<String> keys);
}
And I have the following client that implements the previous interface:
public class Client implements KeyValueStore{
@Override
public void put(String key, byte[] value) {
}
@Override
public byte[] get(String key) {
}
@Override
public void putAll(Map<String, byte[]> pairs) {
}
@Override
public Map<String, byte[]> getAll(Collection<String> keys) {
}
}
How can I implement the Client in order to interact with the given interface? I want to do this with a client-server architecture.
Another thing I want is a "name server" that knows where the data is stored (basically it has the mapping of keys to "storage servers"), and several other servers where the data is actually persisted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个相当开放式的问题。我会尝试一下。
我认为你需要记住三个不同的概念:
接口
执行
客户
让我们逐一查看。
您已经有了界面,所以这部分一切都很好。
我认为您所说的客户端是实现,意思是:它是实现接口中定义的方法的行为的实际代码。
客户端(按照我的解释)将是系统中的另一个部分,它通过您将编写的实现与您的接口进行交互。
解决你的问题:
第一步是弄清楚如何保存通过 put 和 putAll 引入的数据。如果您只想在内存中保留一些内容,那么 Map 可能就足够了。如果预计条目数量会大量增长,那么您可能希望使实现与数据库(或其他形式的持久性)进行交互。
这对你的问题有帮助吗?添加更多细节,如果可以的话我会提供更多见解。
希望有帮助。
编辑:
分布式存储系统有很多变量。
您是否有一个集中端点,然后可以选择以某种方式(循环或更复杂的算法)存储数据的位置?
客户将如何与您的系统交互?它是基于 Web 的系统、RMI 还是其他系统?
我认为在考虑实际实现之前您需要关注基本架构,因为这样的系统的主要问题是鲁棒性和处理缺陷。
一种可能性是拥有一个知道数据存储位置的“名称服务器”(基本上它具有到“存储服务器”的键映射),以及实际保存数据的其他几个服务器。您需要选择它们之间的通信方式,或者每个服务器是否既是“名称服务器”又是“存储服务器”……这里确实有很多可能性!
This is quite an open-ended question. I'll take a shot at it.
I think you need to keep three different concepts in mind:
Interface
Implementation
Client
Let's look at each one.
You have the interface already, so it's all good on that part.
What you're calling the client, I believe to be the implementation, meaning: it's the actual code that implements the behavior of the methods defined in your interface.
A client (in my interpretation), would be yet another piece in your system, that interacts with your Interface through the implementation that you'll write.
Addressing your question:
First step would be figuring out how you'll persist the data introduced through put and putAll. If you just want to keep some stuff in memory, then a Map will probably suffice. If the number of entries is expected to grow alot, then you probably want to make the implementation interact with a database (or other form of persistence).
Does this add some light on your question? Add some more details, and I'll give more insight if I can.
Hope it helps.
Edit:
A distributed storage system has quite a few variables brought to the table.
Do you have a centralized endpoint that then chooses where the data will be stored in some way (round-robin or some more complex algorithm)?
How will the clients interact with your system? Is it a web-based system, RMI, something else?
I think you need to focus on the basic architecture before thinking of the actual implementation, since the main problems with a system like this is robustness and handling flaws.
One possibility is having a "name server" that knows where the data is stored (basically it has the mapping of keys to "storage servers"), and several other servers where the data is actually persisted. You need to choose how these will communicate between them, or if each server will be both a "name server" AND a "storage server"... there's really a lot of possibilities here!
最基本的实现是使用内部
Map
,但这有点愚蠢,为什么不首先使用Map
呢?所以我不确定这就是你想要的。您想要客户端-服务器架构吗?如果是这样,请在您的问题中明确提及,并注明您想要的规格。
The most basic implementation would be to use an internal
Map
, but it's a bit silly, why don't you use aMap
in the first place?So I'm not sure that's what you want. Do you want a client-server architecture? If so, please mention it clearly in your question, with the specifications you want.
不确定您在这里要问什么,但我假设您希望客户端实现一个接口并将调用远程到也实现该接口的服务器。以下是我的想法:
您需要选择一种远程技术。 RMI 是一个简单的选择。另一种可能性是某种 HTTP 客户端/服务器。这是一个简单的 UDP 客户端/服务器。这里有一些更多的想法。
在服务器上,您必须对数据进行一些操作。一个明显的方法是只拥有一个本地内存中的
HashMap
。使用像 H2 这样的 SQL 数据库将是持久保存它的简单方法。 Berkeley DB JE 是另一个。问题中最困难的部分是弄清楚如何以某种方式分发请求。您可以在客户端获取
hashCode()
值并在服务器之间划分哈希空间。例如,您可以采用hashCode()
整数并按服务器数量对其进行 mod。该值将是将其发送到的服务器。如果服务器数量发生变化,那么不幸的是您将需要重新修改所有内容。您可以将put
请求发送到随机服务器,然后将所有get
请求发送到所有服务器并等待所有响应。但是,除非您串行执行,否则这需要一些线程。Not sure exactly what you are asking here but I'll assume you want the client to implement an interface and remote the calls to a server which also implements the interface. Here are my thoughts:
You need to choose a remoting technology. RMI is a easy choice. Some sort of HTTP client/server is another possibility. Here's a simple UDP client/server. Here are some more ideas.
On the server you will have to do something with the data. An obvious one would be to just have a local in-memory
HashMap
. Using a SQL database like H2 would be an easy way to persist it. Berkeley DB JE is another.The hardest part of the problem is figuring out how you are going to to distribute the requests somehow. You could take the
hashCode()
value on the client and divide up the hash space between servers. For example, you can take thehashCode()
integer and mod it by the number of servers. That value will be the server to send it to. If the number of servers changes then you would need to re-mod everything unfortunately. You could send theput
requests to a random server and then send all of theget
request to all of the servers and wait for all of the responses. That takes some thread fu however unless you do it serially.