How would I use HashTable or HashMap to count? - hashmap

For example, given the data
Name: 1234, Qty: 100
Name: 1234, Qty: 100
Name: 1234, Qty: 100
Name: 1234, Qty: 200
Name: 1234, Qty: 200
The expectation is:
Name: 1234, Qty: 700

The basic idea is that you repeatedly find the name-specific cumulative sum in the hashtable, adding the next value to it. In pseudo-code:
for name, qty in input
hashtable[name] += qty
Depending on your language, you may have to explicitly check whether there's an existing hashtable entry for "name" and create one with the initial "qty" value.

In a HashMap, a key can only be associated with one value, so you will need to use a HashMap<Integer, List<Integer>> or a MultiMap to achieve this.

Related

How can query for group by based on unique value in Django

I want to group by data based on unique value.
Eg. country: xyz, city: abc; country: xyz, city: efg; country: wxyz, city: abcde
Query Result: {'xyz':['abc', 'efg'], 'wxyz': ['abcde']}
I solved this using dictionary
query_res[country_val].append(city_val), but I want to know if there is a better solution.

How to update fields in cassandra frozen UDT column?

I am aware of fact that fields in frozen UDT column is not possible and entire records needs to update , in that case does it imply update on frozen UDT column is not possible and if there is scenario of field update of frozen UDT column , in that case one has to insert new record and delete older one ?
You are correct that you cannot update individual fields of a frozen UDT column but you can update the whole column value. You do not need to delete the previous record. It's fine to update the fields directly. Let me illustrate with an example I created on Astra.
Here is a user-defined type that stores a user's address:
CREATE TYPE address (
number int,
street text,
city text,
zip int
)
and here is the definition for the table of users:
CREATE TABLE users (
name text PRIMARY KEY,
address frozen<address>
)
In this table, there is one user with their address stored as:
cqlsh> SELECT * FROM users ;
name | address
-------+----------------------------------------------------------------
alice | {number: 100, street: 'Main Rd', city: 'Melbourne', zip: 3000}
Let's say that the street number is incorrect. If we try to update just the street number field with:
cqlsh> UPDATE users SET address = {number: 456} WHERE name = 'alice';
We'll end up with an address that only has the street number and nothing else:
cqlsh> SELECT * FROM users ;
name | address
-------+----------------------------------------------------
alice | {number: 456, street: null, city: null, zip: null}
This is because the whole value (not just the street number field) got overwritten by the update. The correct way to update the street number is to explicitly set a value for all the fields of the address with:
cqlsh> UPDATE users SET address = {number: 456, street: 'Main Rd', city: 'Melbourne', zip: 3000} WHERE name = 'alice';
so we end up with:
cqlsh> SELECT * FROM users ;
name | address
-------+----------------------------------------------------------------
alice | {number: 456, street: 'Main Rd', city: 'Melbourne', zip: 3000}
Cheers!
You can update column that is frozen UDT, but you'll need to insert all values for fields inside that UDT. So you can just do normal update of that column only
UPDATE table SET udt_col = new_value WHERE pk = ....
without need to delete something first, etc.
Basically, frozen value is just blob obtained by serializing UDT or collection, and stored as one cell inside row and having the single timestamp. That's different from the non-frozen value, where different pieces of UDT/collection could be stored in different places, and having different timestamps.

Cassandra-stress does not generate random values for every row

With DDL and profile yaml below, I generate random data for my table using cassandra-stress. The results I get for the columns amount and status don't match expectation. The random values seem to be drawn once per partition, not for each row.
If, for example, cassandra-stress generates 5 rows with the same business_date (i.e. one partition) the amount and status values are repeated 5 times, the "next" random value comes when the business_date changes. How can I make this so I get a new draw of amount and status for every row?
Sample output, notice last two columns change value only once first column changes.
2018-09-26,y~8.>6MZ,00000000-0004-0a3c-0000-000000040a3c,5.133114565746717E10,3PR|I{3B
2018-09-26,y~8.>6MZ,00000000-004c-4e7e-0000-0000004c4e7e,5.133114565746717E10,3PR|I{3B
2018-09-26,y~8.>6MZ,00000000-003d-b97f-0000-0000003db97f,5.133114565746717E10,3PR|I{3B
2018-09-26,y~8.>6MZ,00000000-004f-db3f-0000-0000004fdb3f,5.133114565746717E10,3PR|I{3B
2018-09-26,y~8.>6MZ,00000000-008c-f0ea-0000-0000008cf0ea,5.133114565746717E10,3PR|I{3B
2018-10-14,Y ?R| |u,00000000-002b-5707-0000-0000002b5707,6.698617679577381E10,,fkb[cU~N!
.
.
.
Table structure:
CREATE TABLE IF NOT EXISTS record (
business_date date,
region text,
id uuid,
status text,
amount double,
PRIMARY KEY (business_date, region, id)
);
Profile YAML:
keyspace: dev
table: record
columnspec:
- name: business_date
population: uniform(17800..17845)
- name: region
size: fixed(10)
population: seq(10..16)
cluster: fixed(7)
- name: id
size: fixed(32)
population: seq(1..10M)
cluster: fixed(5)
- name: status
size: fixed(10)
population: uniform(1000..1010)
- name: amount
population: uniform(500000..10M)
insert:
partitions: fixed(1)
select: fixed(1)/35
queries:
selectall:
cql: select * from record where business_date = ? and region = ?
fields : samerow

Excel VBA How to convert sections of string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a File that has orders history that was downloaded from an old e-commerce website I am trying to convert the data to be uploaded onto a new site. It is a csv file so I am working in excel. There are 20 columns of data one of the columns has the Items Ordered (original text string) each row has data from one order. I need to do convert the data from the the original text string so it ends up with only the required fields in the order and format as shown in the converted text string example below.
original text string
Product ID: 1234, Product Qty: 2, Product SKU: Brush, Product Name: Brush, Product Weight: 80.0000, Product Variation Details: , Product Unit Price: 5.00, Product Total Price: 10.00|Product ID: 2345, Product Qty: 5, Product SKU: Comb, Product Name: Comb, Product Weight: 1.3000, Product Variation Details: , Product Unit Price: 1.00, Product Total Price: 5.00
converted text string
product_id:1234|quantity:2|subtotal:5.00|total:10.00|;product_id:2345|quantity:5|subtotal:1.00|total:5.00
below is not a complete solution for you (because currently it is hard to understand from OP what is real outcome should be), but an example which you can adopt to cover your requirements:
Sub test()
Dim S1$, k1, k2
S1 = "Product ID: 35, Product Qty: 2, Product SKU: Brush, Product Name: Brush, Product Weight: 80.0000, Product Variation Details: , Product Unit Price: 5.00, Product Total Price: 10.00|Product ID: 54, Product Qty: 5, Product SKU: Comb, Product Name: Comb, Product Weight: 1.3000, Product Variation Details: , Product Unit Price: 1.00, Product Total Price: 5.00"
For Each k1 In Split(S1, "|")
For Each k2 In Split(k1, ",")
If UCase(k2) Like "*ID*" Or _
UCase(k2) Like "*QTY*" Or _
UCase(k2) Like "*PRICE*" Then
Debug.Print k2
End If
Next k2, k1
End Sub
test:

Indexing and search by dynamic fields and values in solr

I need help indexing this data in solr.
Assume that i have this kind of a students database. I want to index this data in solr and want to be able to search by tested subjects and their percentile. The subjects that the student has taken might differ among students and its not a fixed schema. So, i cant have the subject name as a field name.
I tried exploring dynamic fields but cant seem to be figure out how will i query by the percentile scored.
E.g. i want to filter by all students who have been tested for Math, but only have fared 90 percentil or higher. I also independently want to search by City, name or a combined query between name, address and the tested subjects and their percentil.
{
Name: Max Junior,
Address: Twin peaks
City : ,San francisco,
Grade: 1,
Subjects: ["Math", "Science", "English"]
Tested Subjects:
[
{name: Math, percentile: 95},
{name: Science, percentile: 85}
]
},
{
Name: Alicia Alex,
Address: Nob Hill,
City: San Mateo,
Grade: 1,
Subjects: ["Math", "English", "Craft"]
Tested Subjects:
[
{name: Math, percentile: 65},
{name: Science, percentile: 78}
]
}
Any help is greatly appreciated.
Thanks a lot
I assume you have some unique ID to identify different subjects. For instance Maths subject as 1234, science as 2345. In that case you can index the Math percentile against subject1234_i (assuming percentile in integer value). You can then query for this field as
subject1234_i:[90 TO *]
Refer to Solr query syntax
Other fields like Name, City can be defined as static as those seems common for each student.

Resources