Relation API usage on Foreign key fields - google-app-maker

I have code as below
var ds = widget.datasource;
ds.item.InvoiceID_fk = 10;
ds.saveChanges();
InvoiceID_fk is a foreign key field.
It says "Foreign key usage is deprecated. Please Relation API instead."
How do we use Relation API for above code?
Thank you

Related

How to avoid read before write for updates in google datastore?

KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity != null) {
System.out.println("Updating access_time for " + entity.getString("name"));
entity = Entity.newBuilder(entity)
.set("access_time", DateTime.now())
.build();
datastore.update(entity);
}
https://github.com/googleapis/java-datastore#updating-data , as per this there has to be a read before write.
Can an update happen without read in google datastore? As in certain cases, I would not care if certain attributes are overwritten.
If you don't need to know if an entity exists and can overwrite the data, then consider using upsert

DynamoDB: Get All Sort Keys from Primary Key

I have a table with a primary key and a sort key; since this is a composite key, I have multiple primary keys mapped with different sort keys.
How can I get all of the sort keys associated with a particular primary key?
I tried using the "Get" operation, but that seems to expect the sort key as well (even though these are what I'm looking for). I also looked at the "BatchGet" operation, but this is for multiple different keys, not for a single primary key with multiple different sort keys.
I tried to do "query" as well and wasn't successful, but I understand this less, so it's possible this is the solution -- is that the case? I am also aware that I could "scan" the entire database and specifically find all items with that particular primary key, but I'm looking to avoid this if possible.
I am working with JS and using this as a reference: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html.
Thank you!
Query() is what you want...
Basically, you just query the table (or index) with a keycondition of HashKey = :hkey and leave off any AND of sort key conditions...
In the docs you linked to, there's a section for query modifying that example...
var params = {
TableName: 'Table',
KeyConditionExpression: 'HashKey = :hkey',
ExpressionAttributeValues: {
':hkey': 'key'
}
};
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.query(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});

Ignite SQL error 50000 when inserting on refence table

I'm currently researching on Ignite, and I used web-console's automatic RDBMS integration feature for my MariaDB persistent store.
This made ignite configure a cache for one of my reference table which has a many-to-many relationship, with 2 fields, both primary-keys.
Example Structure in the persistent store:
CREATE TABLE `user_category` (
`USER_ID` bigint(20) NOT NULL,
`CATEGORY` bigint(20) NOT NULL,
PRIMARY KEY (`USER_ID`,`CATEGORY`),
KEY `FK48520EF2B4BDA303` (`USER_ID`),
KEY `FK48520EF2C941D634` (`CATEGORY`),
CONSTRAINT `FK48520EF2B4BDA303` FOREIGN KEY (`USER_ID`) REFERENCES `ctrl_app_user` (`USER_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK48520EF2C941D634` FOREIGN KEY (`CATEGORY`) REFERENCES `request_category` (`CATEGORY_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
This made web-console configure the cache like this:
ArrayList<QueryEntity> qryEntities = new ArrayList<>();
QueryEntity qryEntity = new QueryEntity();
qryEntity.setKeyType("model.UserCategoryKey");
qryEntity.setValueType("model.UserCategory");
qryEntity.setTableName("user_category");
HashSet<String> keyFields = new HashSet<>();
keyFields.add("userId");
keyFields.add("category");
qryEntity.setKeyFields(keyFields);
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("userId", "java.lang.Long");
fields.put("category", "java.lang.Long");
qryEntity.setFields(fields);
HashMap<String, String> aliases = new HashMap<>();
aliases.put("userId", "USER_ID");
qryEntity.setAliases(aliases);
ArrayList<QueryIndex> indexes = new ArrayList<>();
QueryIndex index = new QueryIndex();
index.setName("FK48520EF2B4BDA303");
index.setIndexType(QueryIndexType.SORTED);
LinkedHashMap<String, Boolean> indFlds = new LinkedHashMap<>();
indFlds.put("userId", false);
index.setFields(indFlds);
indexes.add(index);
index = new QueryIndex();
index.setName("FK48520EF2C941D634");
index.setIndexType(QueryIndexType.SORTED);
indFlds = new LinkedHashMap<>();
indFlds.put("category", false);
index.setFields(indFlds);
indexes.add(index);
qryEntity.setIndexes(indexes);
qryEntities.add(qryEntity);
ccfg.setQueryEntities(qryEntities);
return ccfg;
I am able to retrieve data from ignite properly using its standard SQL.
However, when trying to insert data to that cache, I am getting error 50000 which according to Ignite documentation, is a query that is unsupported by ANSI-99.
Documentation also mentioned to take a look into the SQLException message but the message only mentioned the error 50000.
sample insert statement:
insert into USER_CATEGORY (USER_ID, CATEGORY) values (1, 1);
Thanks in Advance.
Most likely you need to specify a schema name (cache name) for the query:
insert into "YourCacheName".USER_CATEGORY (USER_ID, CATEGORY) values (1, 1);
So, for everyone who will experience this issue in the future, I've resolved the issue by removing the query entity keys.
keyFields.add("userId");
keyFields.add("category");
Ignite treats keys in a reference table/cache as unique, so both columns needed to be unique, this is not applicable for reference tables with many-to-many relationship since this design is bound to have duplicates for each column.
Thanks for those who took a look at this issue!~

Updating Cassandra Map Value through querybuilder

Cassandra support updating specific value in Collection by syntax
ALTER TABLE users ADD todo map<timestamp, text>
UPDATE users SET todo['2012-10-2 12:00'] = 'throw my precious into mount doom'
WHERE user_id = 'frodo';
http://www.datastax.com/documentation/cql/3.0/cql/cql_using/use_map_t.html
Did not see any example of using QueryBuilder to update specific row in Map. How it can be done?
I think you have several options.
1/ Build your own query based on the CQL one.
Example: Consider that you have a table named Interactions and in your schema a column of type named 'attributes'.
String update ="UPDATE demo.Interactions SET attributes=attributes + {'i':'j'} where id='ff';
SimpleStatement statement = new SimpleStatement(update);
session.execute(statement);
2/ Use Java API.
Java API is not that documented indeed.
Let's take an example
a- Create an update object using queryBuilder
Update update = QueryBuilder.update(keyspace, tableName);
b- Then populate with 'put' or 'putAll' functions. put/putAll will add/update content
update.with(QueryBuilder.putAll(key, map));
To remove a key, set the content of a key to null, like:
for (Object item : map.keySet()) {
update.with(
QueryBuilder.put(columName, item, null)
);
}
Then execute the query.
Following methods are available for different types:
LIST:
QueryBuilder.appendAll(...)
QueryBuilder.discardAll(...)
SET:
QueryBuilder.addAll(...)
QueryBuilder.removeAll(...)
MAP:
QueryBuilder.putAll(...)
QueryBuilder.put(...)
The list is not exhautive.
Have a look in QueryBuilder class if you do not find what you are looking for.
Best regards and best luck with Cassandra.

How to query range key programmatically in DynamoDB

How to query range key programmatically in DynamoDB, I am using .Net AWSSDK ,I am able to query on Hash key with below code :
GetItemRequest request = new GetItemRequest
{
TableName = tableName
};
request.Key = new Dictionary<string,AttributeValue>();
request.Key.Add("ID",new AttributeValue { S = PKValue });
GetItemResponse response = client.GetItem(request);
Please suggest,
Thanks in advance.
There are two kinds of primary key in DynamoDB: Hash-only or Hash-Range.
In the above code I guess your table is Hash-only and you use the hash key to retrieve an element with hashkey equals to PKValue.
If your table is in H-R schema and you want to retrieve a specific element with a hashKey and rangeKey, you can reuse the above code and in addition, add the {"RangeKey", new AttributeValue } into your your request.KEY
On the other hand, query means a different thing in DynamoDB. Query will return you a list of rows sorted in some order.

Resources