get Keys from IgniteCache - java

I have to get all keys stored in IgniteCache, unfortunately this method is not implemented in Ignite. I'm using java client.
I thought it is a common method, what is the reason Ignite team didn't implement it?
Is there any efficient solution for getting keys?

Thanks to #alexfedotov I created a solution for my problem, I'm positng it here, since someone may find it useful.
List<K> keys = new ArrayList<>();
cache.query(new ScanQuery<>(null)).forEach(entry -> keys.add((K) entry.getKey()));
After running this code you will receive a list with keyset.

You can get all the keys using ScanQuery with a null predicate. It will return all the entries (key-value pairs).
As well you can use an SqlFieldsQuery like select _key from Entity

The easiest way for getting all keys from cache :
ICache<string, object> cache = ignite.GetCache<string, object>(cacheName);
List<string> cacheKeys = cache.Select(e => e.Key).ToList();

Related

Compare Set<String> with Map<Object> to get Map value in Java 8

I have Set<String> requests = new HashSet<>() and added dynamically some string values in it.
I have Map<TargetSystems> targetSystems = new HashMap<>();, targetSystems with name key and targetSystems object in value.
So I need those targetSystems from Map which are in in Set.
Like Map.put("LMS", TargetSystems) and many more from Database.
Set<String> requests has same target systems name.
How can I achieve this in Java 8.
I don't want to execute forEach loop into Set because I have already created thread list and looping on that. So I need solution in Java 8 with Stream.filter kind of.
Thanks in advance.
I am going to assume that you meant Map<String, TargetSystems>.
You can make a copy of the Map, and use Set.retainAll to limit which entries are in it:
Map<String, TargetSystems> copy = new HashMap<>(targetSystems);
copy.keySet().retainAll(requests);
Collection<TargetSystems> systemsForRequests = copy.values();
If you want to filter your targetSystems map:
Map<String, TargetSystems> targetSystemsFiltered = targetSystems.entrySet().stream()
.filter(entry -> requests.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
If you want just to get filtered targetSystem objects:
Set<TargetSystems> targetSystemsFromSet = targetSystems.entrySet().stream()
.filter(entry -> requests.contains(entry.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toSet());
I have already got an answer and code below please see it's working fine.
Case was that, if we have Map<Object(Its entity class type)> targetSystems and put targetSystems entity objects by the name of it's entity key. Now I have multiple request by the same name exists in targetSystems Map, so I need to equalized that hashset value with hashmap.
I achieved as under:
Object[] objects = Map<String, Object>.values().stream().filter(ts -> Set.contains(ts.getName())).toArray();
I get Object[] type, so I can get my targetSystems value from Map as easy as I like.
Thanks all of you for your kind answers and thanks to stack Overflow.

How to create a map that takes values from other map and then maps them to its field?

So I have a map of ids to systemUsers and now I want to create a map of systemUser keys and login values. Login is a field inside systemUser class. I have a problem with how to write the mapper functions or even if it's the right way to go about it
Map<Long, PHSystemUser> systemUserMap = getPersistenceLogic()
.getSystemUsersMap(serviceClientMap.values());
Map<PHSystemUser, String> loginMap = systemUserMap.values().stream()
.map(PHSystemUser::getLogin)
.collect(Collectors.toMap(, ));
All you need is to collect directly using two functions:
systemUserMap.values().stream()
.collect(Collectors.toMap(Function.identity(), PHSystemUser::getLogin));
The problem with .map(PHSystemUser::getLogin) is that it changes the stream to Stream<String>, leaving you no chance to have the entire PHSystemUser object downstream.

Jedis HMSET map insertion order

Jedis has a hmset method which allows you to set a map of fields and their values at a specific key.
I use the method this way:
Map<String, String> map = new LinkedHashMap<String, String>();
// General player data
map.put("name", player.getName());
map.put("ip", player.getAddress().getAddress().getHostAddress());
map.put("rank", "none");
map.put("tokens", "0");
map.put("coins", "0");
// Arsenal player statistics
map.put("ar_score", "0");
map.put("ar_gameswon", "0");
map.put("ar_gameslost", "0");
map.put("ar_kills", "0");
map.put("ar_deaths", "0");
pipeline.hmset("uuid:" + uuid, map);
pipeline.sync();
core.redis.getJedisPool().returnResourceObject(jedis);
I decided to use a LinkedHashMap to retain the insertion order — however, when looking at the database, it still messes up the order.
Does anyone know how to insert it into the database without messing up the order?
Thank you.
The String-based Jedis client transcodes the strings using a temporary HashMap, so it kills your order. The BinaryClient iterates over the Map directly and retains the order.
The lettuce client keeps the order of your map in any case.
Alternatively, set the values one by one using HSET hash key value
HTH, Mark
Give a try to Redis based framework for Java - Redisson. It keeps map entries in insertion order during iteration using any codec (Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization).

Set up Accumulo table through api

new to Accumulo, and this may sound silly, but I was wondering how to setup a table through the api? The documentation is definitely lacking. I have been able to find
conn.tableOperations().createTable("myTable");
as well as like setting up locality groups:
HashSet<Text> metadataColumns = new HashSet<Text>();
metadataColumns.add(new Text("domain"));
metadataColumns.add(new Text("link"));
HashSet<Text> contentColumns = new HashSet<Text>();
contentColumns.add(new Text("body"));
contentColumns.add(new Text("images"));
localityGroups.put("metadata", metadataColumns);
localityGroups.put("content", contentColumns);
conn.tableOperations().setLocalityGroups("mytable", localityGroups);
Map<String, Set<Text>> groups =
conn.tableOperations().getLocalityGroups("mytable");
From the documentation, but I want to know how to take the first approach and build the table. Then build the columns.
Thanks in advance!
There is no inherent schema for a table to set up. Once it is created using the API you found, you can insert whatever key-value pairs you wish in it.

how to filter following map?

I have got these values in my HashMap ,
"ver":"a"
"ver":"b"
"ver":"c"
"os":"d"
"os":"e"
i need only:
"os":"d"
"os":"e"
My code is:
String[] eachPair = myString.split(",");
Map<String,String> pairs = new HashMap<String,String>();
for(String pair: eachPair) {
pairs.put(pair.substring(0, pair.indexOf(":")).trim(), pair.substring(pair.indexOf(":")+1));
}
pairs.get("os");
but its not working. please help
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Map should have unique keys only, If you add a value with the same key which is already exist in the map, value for that key will be override and you will lost the old value.
HahMap doesn't allows duplicate keys.So I recommand to use Guava's MultiMap or apache's MultiValueMap
Please look into
the sample code reference of MultiMap: http://tomjefferys.blogspot.in/2011/09/multimaps-google-guava.html
the sample code reference of MultiValueMap: http://java.dzone.com/articles/allowing-duplicate-keys-java

Categories

Resources