Delete similar patern keys from redis using JAVA - java

I am using jedis for redis connect in java.
I want to delete similar pattern keys from redis server using jedis.
e.g.
1. 1_pattern
2. 2_pattern
3. 3_pattern
4. 4_pattern
5. 5_pattern
We can use del(key), but it will delete only one key.
I want something like del("*_pattern")

It should use regex in redis. In your code:
String keyPattern = "*"+"pattern";
// or String keyPattern = "*_"+"pattern";
Set<String> keyList = jedis.keys(keyPattern);
for(String key:keyList){
jedis.del(key);
}
// free redis resource
I think above solution work well.

One of the most efficient way is to reduce the redis calls.
String keyPattern = "*"+"pattern";
Set<String> keys = redis.keys(keyPattern);
if (null != keys && keys.size() > 0) {
redis.del(keys.toArray(new String[keys.size()]));
}

You could combine the DEL key [key ...] command with the KEYS pattern command to get what you want.
For example, you can do this with Jedis like so (pseudocode):
// or use "?_pattern"
jedis.del(jedis.keys("*_pattern"));
But be aware that this operation could take a long time since KEYS is O(N) where N is the number of keys in the database, DEL is O(M) where M is the number of keys, and for each key being deleted that is a list/set/etc, its O(P), where P is the length of the list/set/etc.

See my answer here.
In your case, it's a simple call to deleteKeys("*_pattern");

Related

How to acess value of a particular key present in redis database using java

I am trying to access redis database through jedis in java. I am able to get connected to redis. I am not able to get the value of a particular key present in the database. The code that I am using now is
Jedis jedis = new Jedis(host, port);
jedis.auth(username, password);
Set<String> keys = jedis.hkeys(Id);
List<String> values = jedis.hvals(Id);
It is returning keys and values in an incorrect order. Is there any way to get the keys and values in correct order.
You cannot rely on specific field order for fields in a Redis hash
From jedis, you can try hgetall. That command retrieves each key with its associated value. So you will not face that order issue again.
https://redis.io/commands/HGETALL
https://redis.io/topics/data-types#hashes
https://redis.io/topics/data-types-intro#redis-hashes

How get random key-value from hazelcast map

I tried to get a random element from the Map like this
IMap<Integer, Integer> workWaitTasks = hazelcastInstance.getMap(WORK_WAIT_TASKS);
collectionTask = Collections.singleton(workWaitTasks.values().stream()
.skip(workWaitTasks.isEmpty() ? 0 : new Random().nextInt(workWaitTasks.size()))
.findFirst()
.get());
int taskId = collectionTask.iterator().next();
but the best way I think is using predicates
I read this https://docs.hazelcast.com/imdg/4.2/query/how-distributed-query-works.html#querying-with-sql-like-predicates
Unfortunately this didn't help me
I couldn't find a way to do this
in sql it is like this
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
how to make correct predicate in hazelcast?
Can you give an example please? Help me please
I think there's no straightforward and effective way to do this using public API. One option is using Jet:
IMap<Object, Object> sourceMap = instance.getMap("table");
IList<Object> targetList = instance.getList("result");
int samplingFactor = sourceMap.size() / 10;
Pipeline p = Pipeline.create();
p.readFrom(Sources.map(sourceMap))
.filter(item -> item.getKey().hashCode() % samplingFactor == ThreadLocalRandom.current().nextInt(samplingFactor))
.writeTo(Sinks.list(targetList));
instance.newJob(p).join();
The code above should add approximately 10 elements to the result list, from which it's easy to get a random entry, but it can also end up with an empty list - you can experiment with increasing the multiplier in samplingFactor to get satisfactory probability of getting some result without having to re-run the job.
The same might also be possible using aggregation (IMap.aggregate) with a custom aggregator, some colleague might provide answer :-).

get Keys from IgniteCache

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();

Query all items in DynamoDB from a given hash key with a hash-range schema using java sdk

EDIT:
I was actually incorrect. I was querying the table when I meant to query an index which explains my error. Vikdor's solution is a valid one though.
ORIGINAL:
I have a table with a Hash-Range key schema in DynamoDB. I need to be able to get all items associated with a specific hash key but it seems to require a range key condition. My issue is I want EVERY range key but there is no wildcard option. As of right now my range key is a string and the only way I could think to do this is by querying all range keys greater or equal to the smallest ascii characters I can use since the documentation says it sorts based on ascii character values.
I looked into scanning but it appears that simply will read the entire table which is NOT an option.
Is there any better way to query for all values of a hash key or can anyone confirm that using the method with the ascii character will work?
but it seems to require a range key condition.
This doesn't sound to be true.
I use DynamoDBMapper and use DynamoDBQueryExpression to query all the records with a given HashKey as follows:
DynamoDBQueryExpression<DomainObject> query =
new DynamoDBQueryExpression<DomainObject>();
DomainObject hashKeyValues = new DomainObject();
hashKeyValues.setHashKey(hashKeyValue);
query.setHashKeyValues(hashKeyValues);
// getMapper() returns a DynamoDBMapper object with the appropriate
// AmazonDynamoDBClient object.
List<DomainObject> results = getMapper().query(query);
HTH.
You can use DynamoDB's query API, which allows you to query the database based conditional expressions using the hash/range keys. You can see examples of the API here. Here is a relevant example:
ItemCollection<QueryOutcome> items = table.query("theHashFieldName", "theHashFieldToQuery");
You can also query using more complex expressions. E.g.:
DynamoDB dynamoDB = new DynamoDB(
new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
Table table = dynamoDB.getTable("TableName");
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("Id = :v_id")
.withValueMap(new ValueMap()
.withString(":v_id", "TheId"));
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.toJSONPretty());
}

How to get features record for plan estimate change using lookback API

I am using rally lookback api with java. I am trying to fetch historical data features, sample code that i am using is as shown below.
LookbackApi lookbackApi = new LookbackApi();
lookbackApi.setCredentials("username", "password");
lookbackApi.setWorkspace(47903209423);
lookbackApi.setServer("https://rally1.rallydev.com");
//lookbackApi.setWorkspace("90432948");
LookbackQuery query = lookbackApi.newSnapshotQuery();
query.addFindClause("_TypeHierarchy", "PortfolioItem/Feature");
query.setPagesize(200) // set pagesize to 200 instead of the default 20k
.setStart(200) // ask for the second page of data
.requireFields("ScheduleState", // A useful set of fields for defects, add any others you may want
"ObjectID",
"State",
"Project",
"PlanEstimate",
"_ValidFrom",
"_ValidTo")
.sortBy("_UnformattedID")
.hydrateFields("ScheduleState","State", "PlanEstimate","Project"); // ScheduleState will come back as an OID if it doesn't get hydrated
LookbackResult resultSet = query.execute();
int resultCount = resultSet.Results.size();
Map<String,Object> firstSnapshot = resultSet.Results.get(0);
Iterator<Map<String,Object>> iterator = resultSet.getResultsIterator();
while (iterator.hasNext()) {
Map<String, Object> snapshot = iterator.next();
}
I need a way to put a condition so that it will fetch all the records from history which will have plan estimate changed,but will ignore other history for any feature and underlying user story. I need it this way so that we can track plan estimate change but, will be able to avoid fetching un-necessary data and reduce the time to do this.
I'm not familiar with the java toolkit, but using the raw Lookback API, you would accomplish this with a filter clause like {"_PreviousValues.PlanEstimate": {"$exists": true}}.
Map ifExist = new HashMap();
ifExist.put("$exists", true);
// Note:- true is java boolean, be careful with this as string "true" will not work.
query.addFindClause("_PreviousValues.PlanEstimate",ifExist);
Additinally one need to consider adding "_PreviousValues.PlanEstimate" into
.requireFields() in case only "PlanEstimate" is required to hydrated

Categories

Resources