Can't read integers value in Cassandra - cassandra

I have a simple column family 'Users'
System.out.println(" read User");
long timestamp = System.currentTimeMillis();
clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"),
new Column(ByteBuffer.wrap("email".getBytes())).setValue(ByteBuffer.wrap("mike#gmail.com".getBytes())).setTimestamp(timestamp)
, ConsistencyLevel.ONE);
clientA.insert(ByteBuffer.wrap("mike".getBytes()), new ColumnParent("Users"),
new Column(ByteBuffer.wrap("totalPosts".getBytes())).setValue(ByteBuffer.allocate(4).putInt(27).array()).setTimestamp(timestamp)
, ConsistencyLevel.ONE);
SlicePredicate predicate = new SlicePredicate();
predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 10));
ColumnParent parent = new ColumnParent("Users");
List<ColumnOrSuperColumn> results = clientA.get_slice(ByteBuffer.wrap("mike".getBytes()),
parent,
predicate,
ConsistencyLevel.ONE);
for (ColumnOrSuperColumn result : results) {
Column column = result.column;
System.out.println(new String(column.getName()) + " -> "+ new String(column.getValue())+", "+column.getValue().length);
}
it returns
read User
email -> mike#gmail.com, 14
totalPosts ->
So totalPosts can't be read with the thrift client
with cassandra-cli
[default#test] get Users['mike']['totalPosts'];
=> (column=totalPosts, value=0000001b, timestamp=1336493080621)
Elapsed time: 10 msec(s).
How can I retrieve this Integer value with Java thrift client?
using cassandra 1.1
edit:
it seems due to this part
for (ColumnOrSuperColumn result : results) {
Column column = result.column;
System.out.println(new String(column.getName()) + " -> "+Arrays.toString(column.getValue()));
}
returns
email -> [109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109]
totalPosts -> [0, 0, 0, 27]

Your column values are being returned as bytes, the same way you're inserting them. So the value for the 'email' column is [109, 105, 107, 101, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109], which interpreted as ascii is mike#gmail.com. And the number 27 which you write into the ByteBuffer as [0, 0, 0, 27] (via the putint call) comes out the same way.
If you absolutely have to be using the raw thrift interface, you'll probably want to retrieve your totalPosts int using ByteBuffer.getInt(). But if at all possible, I recommend using a library to wrap around the ugliness of the thrift interface, which should take care of value serialization issues like this. Maybe look at Hector, or skip the old interface entirely and go straight to CQL with Cassandra-JDBC.

Related

cv2.groupRectangles() returning empty tuple - Python

I'm trying to group rectangles together using cv2.groupRectangles() but it returns empty tuple ()
I tried using 2 different versions of opencv-python (4.6.0.66 and 3.4.18.65)
‎This is the code:
# "do" object detection
rectangles = np.array([
[530, 119, 47, 47],
[641, 117, 53, 53],
[531, 89, 117, 117]])
print(rectangles)
print()
(rect, weights) = cv2.groupRectangles(rectangles, groupThreshold=1, eps=0.5)
print("rect after cv.groupRectangles:", rect)
And this is the output:
[[530 119 47 47]
[641 117 53 53]
[531 89 117 117]]
rect after cv.groupRectangles: ()
Here are your initial rectangles for reference. How were you wanting those to be merged?
You might have chosen groupThreshold badly.
If that is more than 0, the function will remove any rectangles that aren't "confirmed" by nearby neighbors. With 0, it's allowed to leave single rectangles alone...
>>> cv.groupRectangles(rectangles, groupThreshold=0, eps=0)
(array([[530, 119, 47, 47],
[641, 117, 53, 53],
[531, 89, 117, 117]]), array([1, 1, 1]))
But then it also doesn't seem to want to merge any of them. That eps doesn't seem to behave all that "relatively". I get effects when I increase it beyond 1.0.
Documentation is very poor about this. Perhaps this function is broken. If not broken, then it's at least not documented well enough for me to make sense of it and I think I'm proficient with OpenCV.

How to add and concate two variables in same list

array = [[1676, 196, 159, 29, 'invoice'], [1857, 198, 108, 28, 'date:']]
width = 159+108 = 267
height = 29+28 = 57
label = invoice date:
Required solution: [1676, 196, 267, 57, 'invoice date:']
Is there any solution to concatenate string and add numbers in same list
Assuming your other lines are for your own notes/testing, and your "required solution" is a typo, you can use zip like so:
array = [[1676, 196, 159, 29, 'invoice'], [1857, 198, 108, 28, 'date:']]
res = []
for x, y in zip(array[0], array[1]): # compare values at each index
if isinstance(x, str): # check to see if x is a string
res.append(x + ' ' + y) # add a space if x is a string
else:
res.append(x + y) # add the two integers if x is not a string
print(res)
[3533, 394, 267, 57, 'invoice date:']
Note that the concatenation above will only work if you are sure that the same indices will have strings vs. integers.

Sorting dictionary by key

I have a dictionary that have year-month combination as the key and value of it. I used OrderedDict to sort the dictionary and getting result like below. In my expected result, after "2021-1", it should be "2021-2". But "2021-10" is coming in between.
{
"2020-11": 25,
"2020-12": 861,
"2021-1": 935,
"2021-10": 1,
"2021-2": 4878,
"2021-3": 6058,
"2021-4": 3380,
"2021-5": 4017,
"2021-6": 1163,
"2021-7": 620,
"2021-8": 300,
"2021-9": 7
}
My expected result should be like below. I want the dictionary to be sorted by least date to the last date
{
"2020-11": 25,
"2020-12": 861,
"2021-1": 935,
"2021-2": 4878,
"2021-3": 6058,
"2021-4": 3380,
"2021-5": 4017,
"2021-6": 1163,
"2021-7": 620,
"2021-8": 300,
"2021-9": 7,
"2021-10": 1
}
Appreciate if you can help.
If you want to customize the way sorting is done, use sorted with parameter key:
from typing import OrderedDict
from decimal import Decimal
data = {
"2020-11": 25,
"2020-12": 861,
"2021-1": 935,
"2021-10": 1,
"2021-2": 4878,
"2021-3": 6058,
"2021-4": 3380,
"2021-5": 4017,
"2021-6": 1163,
"2021-7": 620,
"2021-8": 300,
"2021-9": 7
}
def year_plus_month(item):
key = item[0].replace("-", ".")
return Decimal(key)
data_ordered = OrderedDict(sorted(data.items(), key=year_plus_month))
print(data_ordered)
I used Decimal instead of float to avoid any wonky floating point precision.

How can one convert an environment variable to a string in groovy

In a jenkins file pipeline I have:
def task = readJSON(file: 'ecs/task-definition.json')
echo "Read Task Definition: $task"
task.containerDefinitions[0].image="${AWS_VERSION_IMAGE}"
echo "New Task Definition With Image Is: $task"
In the output value of the second echo statement i get:
New Task Definition With Image Is: [name:proxy, image:[bytes:[48, 48, 55, 49, 50, 54, 53, 56, 51, 55, 53, 55, 46, 100, 107, 114, 46]]
where AWS_VERSION_IMAGE is an environment variable defined as AWS_VERSION_IMAGE = "${AWS_DOCKER_REGISTRY}:${VERSION_TAG}" in an environment block.
Thanks for the replies, I ended up fixing the issue by using String instead of def like this:
String image = "${AWS_VERSION_IMAGE}"
task.containerDefinitions[0].image=image
Now it works.

node-postgres simple SELECT is becoming extremely slow (PostgreSQL)

I'm developing an server-side app for mobile game backed with postgresql and I'm using pg with Knex("pg": "6.1.2" and "knex": "0.12.6"). Not so long ago I faced a problem with select perfomance degradation. Latest Postgresql and v4.7.2 Node
I've benchmarked my app with 40 fictive ccu. So I measured time between Query constructor and Query.prototype.handleReadyForQuery and what i've got:
{"select count(*) from \"player_battles\" where (\"attacking_player\" = $1 or \"defending_player\" = $2) and attacking_player <> defending_player": [27, 74, 92, 156, 170, 203, 217, 230, 243, 251, 261, 269, 288, 303, 313, 328, 342, 352, 361, 384, 395, 407, 420, 428, 440, 448, 460, 471, 483, 494, 507, 515, 537, 538, 539, 30, 40, 60, 1564, 2273, 2287, 2291, 2320, 2327, 2346, 2354, 2370, 2380, 2388, 2402, 2411, 2419, 2429, 2436, 2444, 4014, 4412, 4421, 4421, 4422, 4423, 4423, 4424, 4425, 4426, 4427, 4427, 4428, 4429, 4429, 18, 35, 60, 78, 113, 125, 151, 161, 170, 178, 185, 197, 611, 1972, 1987, 1988, 1988, 1989, 1991, 1992, 1993, 1993, 1994, 1995, 1996, 1996, 1997, 1997, 1999, 1999, 2000, 2001, 2002, 2002, 2002]}
(Array of numbers are representing times to complete particular query ms)
4429ms, Carl! Unpleasant surprize.
Obviously the problem lies in unoptimized sql or bad indexing, but analyzing logs with pgBadger showed me that: https://gyazo.com/4855a0eceac8669ab5c21564a392b357
Any ideas?
Related Issue # Github: https://github.com/brianc/node-postgres/issues/1243
Previous question: Knex with PostgreSQL select query extremely performance degradation on multiple parallel requests
UPD Here is screenshot of "General activity": https://gyazo.com/b2781069c87f88a2d4034345d58f91a3
UPD2
Example query
SELECT COUNT(*)
FROM "player_battles"
WHERE ("attacking_player" = $1
OR "defending_player" = $2)
AND attacking_player <> defending_player
(where $1 and $2 are 1 and 2)
Target table ddl
CREATE TABLE public.player_battles
(
id integer NOT NULL DEFAULT nextval('player_battles_id_seq'::regclass),
attacking_player integer NOT NULL,
defending_player integer NOT NULL,
is_attacking_won boolean NOT NULL,
received_honor integer NOT NULL,
lost_honor integer NOT NULL,
gold_stolen bigint,
created_at bigint NOT NULL DEFAULT '0'::bigint,
attacking_level_atm integer NOT NULL DEFAULT 0,
defending_level_atm integer NOT NULL DEFAULT 0,
attacking_honor_atm integer NOT NULL DEFAULT 0,
defending_honor_atm integer NOT NULL DEFAULT 0,
no_winner boolean NOT NULL DEFAULT false,
auto_defeat boolean,
CONSTRAINT player_battles_pkey PRIMARY KEY (id),
CONSTRAINT player_battles_attacking_player_foreign FOREIGN KEY (attacking_player)
REFERENCES public.player_profile (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT player_battles_defending_player_foreign FOREIGN KEY (defending_player)
REFERENCES public.player_profile (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
And indexes
CREATE INDEX player_battles_attacking_player_index
ON public.player_battles USING btree
(attacking_player)
TABLESPACE pg_default;
CREATE INDEX player_battles_created_at_index
ON public.player_battles USING btree
(created_at DESC)
TABLESPACE pg_default;
CREATE INDEX player_battles_defending_player_index
ON public.player_battles USING btree
(defending_player)
TABLESPACE pg_default;
EXPLAIN ANALYZE
Aggregate (cost=19.40..19.41 rows=1 width=8) (actual time=0.053..0.053 rows=1 loops=1)
-> Bitmap Heap Scan on player_battles (cost=8.33..19.39 rows=4 width=0) (actual time=0.030..0.047 rows=4 loops=1)
Recheck Cond: ((attacking_player = 1) OR (defending_player = 2))
Filter: (attacking_player <> defending_player)
Heap Blocks: exact=4
-> BitmapOr (cost=8.33..8.33 rows=4 width=0) (actual time=0.021..0.021 rows=0 loops=1)
-> Bitmap Index Scan on player_battles_attacking_player_index (cost=0.00..4.16 rows=2 width=0) (actual time=0.016..0.016 rows=2 loops=1)
Index Cond: (attacking_player = 1)
-> Bitmap Index Scan on player_battles_defending_player_index (cost=0.00..4.16 rows=2 width=0) (actual time=0.003..0.003 rows=2 loops=1)
Index Cond: (defending_player = 2)
Planning time: 0.907 ms
Execution time: 0.160 ms
Extra data
Just about 160 players and 310 rows in player_battles, but num of rows does not correlate with the problem. System: DigitalOcean 2cpu/2gb ram. Output of pgBadger and response times from node-postgres: http://ge.tt/3DWk8Dj2
And my conf file may help: http://ge.tt/7cw69Dj2

Resources