I created var object then put in key and values.
I can call key and object[key] however there are some keys that are repeated such as
key = A,
key = B,
key = A,
key = B
I want to separate the first two objects in array[0][0] and array[0][1]
and the other two in array[1][0] and array[1][1]. Is this possible?
Thanks in advance!
Deending on your needs, you can make an array of arrays as such.
var arr = new Array();
arr.push([{key:"A"}, {key:"B"}]);
arr.push([{key:"A"}, {key:"B"}]);
console logs =>
arr[0][0]
Object {key: "A"}
arr[0][1]
Object {key: "B"}
arr[1][0]
Object {key: "A"}
arr[1][1]
Object {key: "B"}
Related
I just started to study the clickhouse! I use python and library clickhouse_connect. Can't get to add a new string to the Array(String)
I try to create new String to Array
My code:
import clickhouse_connect
ch_client = clickhouse_connect.get_client(host=ch_host, user=ch_user, password=ch_pass, database=ch_datebase)
ch_client.command(f'CREATE TABLE IF NOT EXISTS {ch_table} (key String, strings Array(String)) ENGINE MergeTree ORDER BY key')
insert_data = [['123', ['string1']]]
ch_client.insert(ch_table, insert_data, column_names=['key', 'strings'])
insert_data = [['123', ['string2']]]
ch_client.insert(ch_table, insert_data, column_names=['key', 'strings'])
Is there an easy way to insert a new row into the list if there is already such a key, and if there is no such key, then create a new row?
You could just insert your rows, then write a query that gives you what you want:
SELECT
key,
groupArrayArray(strings)
FROM ch_table
GROUP BY key;
If that works, you could create a materialized view from this query:
CREATE MATERIALIZED VIEW ch_table_view
ENGINE = AggregatingMergeTree
ORDER BY key
POPULATE AS
SELECT
key,
groupArrayArrayState(strings) AS strings_merged
FROM ch_table
GROUP BY key;
Notice the -State aggregate combinator was used, which keeps a "running total" of the array of strings. To read this column, you need to use the corresponding -Merge combinator:
SELECT
key,
groupArrayArrayMerge(strings_merged)
FROM ch_table_view
GROUP BY key;
I have an html table that is filled from a DynamoDB table. Clicking a row pops up an edit form in a modal. The data inputted is sent to a flask server to update the item - using AWS DynamoDB - that was edited in the modal form. Upon reading the AWS documentation for this, the correct method is to use update_item. However, when doing so the item is added again instead of updating the item. I used the AWS here to script the below. In my DynamoDB table, the primary partition key is KEY1 and the primary sort key is KEY2 in the below reference.
table = dynamodb.Table('table_name') #define DynamoDB table
key1 = account_id #string value of account id
key2 = request.form["KEY2"] #this is a read only field in the form, so the key does not get updated here
form_val1 = request.form["input1"]
form_val2 = request.form["input2"]
form_val3 = request.form["input3"]
form_val4 = request.form["input4"]
form_val5 = request.form["input5"]
form_val6 = request.form["input6"]
form_val7 = request.form["input7"]
form_val8 = request.form["input8"]
form_val9 = request.form["input9"]
#update item in dynamo
table.update_item(
Key={
'KEY1': key1, #partition key
'KEY2': key2 #sort key
},
UpdateExpression='SET dbField1 = :val1, dbField2 = :val2, dbField3 = :val3, dbField4 = :val4, dbField5 = :val5, dbField6 = :val6, dbField7 = :val7, dbField8 = :val8, dbField9 = :val9',
ExpressionAttributeValues={
':val1': form_val1,
':val2': form_val2,
':val3': form_val3,
':val4': form_val4,
':val5': form_val5,
':val6': form_val6,
':val7': form_val7,
':val8': form_val8,
':val9': form_val9
}
)
You can't and I will explain to you for what that not is possible.
When you create a table on dynamo DB with key and a order key you automatically create an index between key and sort key. We know an index is inmutable, that means you can't update the keys. Is for that reason that when you update dynamo create a new element.
It's a problem of the definition of your table because you never need to change the key or the sort key. Recreate your table only with the index and not with the sort index (because if your app can change the sort index that make not sense).
Is this the full query? the update_item docs say that TableName is required, which I don't see in your snippet.
From the updateitem docs:
Edits an existing item's attributes, or adds a new item to the table
if it does not already exist.
Make sure that the primary key (partition key and sort key) are unique in your table. If they are not, updateitem will create a new item in the database.
Are you absolutely certain that the primary key for the item already exists in the database?
When I try to assign a key to in a multi-path .update() like this:
key1 = 'bar';
key2 = 'baz';
db.ref('path').update({
key1: 'value1',
key2: 'value2'});
It literally writes the key as a string to the DB:
/path/key1/value1
/path/key2/value2
QUESTION: how can I get the expected output with dynamic keys?
/path/bar/value1
/path/baz/value2
To get the expected output, you need to use brackets around your [keys]:
key1 = 'bar';
key2 = 'baz';
db.ref('path').update({
[key1]: 'value1',
[key2]: 'value2'});
This will give you:
/path/bar/value1
/path/baz/value2
way to map the following to a Dictionary??
Sql sql = new Sql()
.Append("SELECT Count(*) ")
.Append("FROM Custumers")
.Append("WHERE CustomerId = #0", Id)
var result = database.Fetch<Dictionary<int,DateTime>>(sql);
I cannot use List as DateTime is also thr.
Petapoco always return a List<T>, but you can convert the List to a Dictionary afterwards:
var result = database
.Fetch<Pair<int,DateTime>>(sql)
.ToDictionary(i => i.ID, i => i.Date);
With NPoco you can write this: it will use the first 2 columns
var result = database.Dictionary<int, DateTime>(sql);
or use what #Eduardo said.
In red is using hash, I need to store hash key with multiple fields and values.
I tried as below:
client.hmset("Table1", "Id", "9324324", "ReqNo", "23432", redis.print);
client.hmset("Table1", "Id", "9324325", "ReqNo", "23432", redis.print);
var arrrep = new Array();
client.hgetall("Table1", function(err, rep){
console.log(rep);
});
Output is: { Id: '9324325', ReqNo: '23432' }
I am getting only one value. How to get all fields and values in the hash key? Kindly help me if I am wrong and let me get the code. Thanks.
You are getting one value because you override the previous value.
client.hmset("Table1", "Id", "9324324", "ReqNo", "23432", redis.print);
This adds Id, ReqNo to the Table1 hash object.
client.hmset("Table1", "Id", "9324325", "ReqNo", "23432", redis.print);
This overrides Id and ReqNo for the Table1 hash object. At this point, you only have two fields in the hash.
Actually, your problem comes form the fact you are trying to map a relational database model to Redis. You should not. With Redis, it is better to think in term of data structures and access paths.
You need to store one hash object per record. For instance:
HMSET Id:9324324 ReqNo 23432 ... and some other properties ...
HMSET Id:9324325 ReqNo 23432 ... and some other properties ...
Then, you can use a set to store the IDs:
SADD Table1 9324324 9324325
Finally to retrieve the ReqNo data associated to the Table1 collection:
SORT Table1 BY NOSORT GET # GET Id:*->ReqNo
If you want to also search for all the IDs which are associated to a given ReqNo, then you need another structure to support this access path:
SADD ReqNo:23432 9324324 9324325
So you can get the list of IDs for record 23432 by using:
SMEMBERS ReqNo:23432
In other words, do not try to transpose a relational model: just create your own data structures supporting your use cases.