简体   繁体   English

不同JVM之间共享的映射

[英]Map shared between different JVMs

I need an implementation of java.util.Map that can be shared between different JVMs on different hosts. 我需要一个java.util.Map的实现,该实现可以在不同主机上的不同JVM之间共享。 All the hosts (and thereby the JVMs) have access to the same shared filesystem, so I need something like this: 所有主机(以及JVM)都可以访问相同的共享文件系统,因此我需要这样的东西:

Map<String,String> sharedMap = new SharedMapInFile("/shared/mymap.map");

I have taken a look at MapDB ( http://www.mapdb.org/ ), but there is no support for sharing between different JVMs (see: https://groups.google.com/forum/#!topic/mapdb/VKVy84MZJko ). 我看过MapDB( http://www.mapdb.org/ ),但是不支持在不同的JVM之间共享(请参阅: https ://groups.google.com/forum/#!topic/mapdb / VKVy84MZJko )。

Any ideas? 有任何想法吗? TIA! TIA!

Chronicle Map could do that, if your shared filesystem could leverage mapping files into memory on the OS level. 如果您的共享文件系统可以在操作系统级别将映射文件映射到内存中,那么Chronicle Map可以做到这一点。 E. g: 例如:

Map<String, String> sharedMap = ChronicleMap
    .of(String.class, String.class)
    .averageKey(...).averageValue(...)
    .entries(...)
    .createPersistedTo(new File("/shared/mymap.map"));

It might be convenient but pretty inefficient, however, because 3 pages, 12k bytes needs to be send over network on each Map.put() operation. 但是,这可能很方便,但是效率很低,因为在每个Map.put()操作上需要通过网络发送3页,12k字节。 Also, beware that the latency between putting an entry into the map on one host and being able to read that entry on another host could be high, if OS doesn't write the mapped memory back to file system on the writer host for long time. 另外,请注意,如果OS长时间不将映射的内存写回到写入器主机上的文件系统,则将一个条目放入一个主机上的映射与能够在另一主机上读取该条目之间的延迟可能会很高。 。 See /proc/sys/vm/dirty_expire_centisecs in Linux. 请参阅Linux中的/proc/sys/vm/dirty_expire_centisecs

So setting up direct network connection between the hosts (eg using Netty ) and sending just keys and values could be much more efficient, though a less convenient solution. 因此,在主机之间建立直接的网络连接(例如使用Netty )并仅发送键和值可能会更加高效,尽管这种解决方案不太方便。

You can achieve this by using a database as your 'map'. 您可以通过使用数据库作为“地图”来实现。 It performs the functionality of a shared map and does so very efficiently. 它执行共享地图的功能,并且非常有效。 If needed you can easily create a wrapper that implements the 'Map' interface so that it can used just like a HashMap (if something like that is needed). 如果需要,您可以轻松地创建一个实现“ Map”接口的包装器,使其可以像HashMap一样使用(如果需要类似的东西)。

Sqlite is nice for this because it creates a simple file that you can access if you are on a shared file system (but be aware that it doesn't handle concurrency issues so well). Sqlite很好用,因为它创建了一个简单文件,如果您使用的是共享文件系统,则可以访问该文件(但请注意,它不能很好地处理并发问题)。 Any other database such as MySQL or Postgres can accomplish this as well (and with better concurrency). 任何其他数据库(例如MySQL或Postgres)也可以做到这一点(并发性更好)。 Here is an example using Sqlite: 这是使用Sqlite的示例:

Class.forName("org.sqlite.JDBC");
String connectTo = "jdbc:sqlite:"+myDbName; //filename goes here
java.sql.Connection conn = DriverManager.getConnection(connectTo);

Statement st = conn.createStatement()
ResultSet result = st.executeQuery("select value from tablename where key='myKey'"); //ie some sql statement that gets what you need
result.getString(1);
and so on...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM