I have the following table creation code for DynamoDB (C#):
client.CreateTable(new CreateTableRequest
{
TableName = tableName,
ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 20, WriteCapacityUnits = 10 },
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement
{
AttributeName = "RID",
KeyType = KeyType.HASH
}
}
,
AttributeDefinitions = new List<AttributeDefinition>
{
new AttributeDefinition {
AttributeName = "RID",
AttributeType = ScalarAttributeType.N
}
}
});
the data that gets populated into this table comes from this JSON:
[
{"RID": 208649, "CLI_RID": 935476, "PRT_DT": "VAL_AA", "DISTR": "INTERNAL"},
{"RID": 217427, "CLI_RID": 1009561, "PRT_DT": "VAL_BB", "DISTR": "INTERNAL", "STATE": "VAL_BD"},
{"RID": 223331, "CLI_RID": 1325477, "PRT_DT": "VAL_CB", "DISTR": "", "STATE": "VAL_CD", "FNAME": "VAL_CE", "START": "VAL_CF"},
{"RID": 227717, "CLI_RID": 1023478, "PRT_DT": "VAL_DB", "DISTR": "", "STATE": "VAL_DD"}
{"RID": 217462, "CLI_RID": 1009561, "PRT_DT": "VAL_BB", "DISTR": "", "STATE": "VAL_BD"},
{"RID": 218679, "CLI_RID": 1009561, "PRT_DT": "VAL_AA", "DISTR": "INTERNAL"},
{"RID": 222376, "CLI_RID": 1263978, "PRT_DT": "VAL_DB", "DISTR": "", "STATE": "VAL_DD"}
]
How would I Query or Filter for all records containing 1009561 in column "CLI_RID" and column "DISTR" <> "INTERNAL"?
There will be about 15 mil records in this DynamoDB table.
Is my table defined correctly for this query/filter?
Updated table creation:
// CLI_RIDIndex
var cli_ridIndex = new GlobalSecondaryIndex
{
IndexName = "cli_ridIndex",
ProvisionedThroughput = new ProvisionedThroughput
{
ReadCapacityUnits = 20,
WriteCapacityUnits = 10
},
KeySchema = {
new KeySchemaElement
{
AttributeName = "CLI_RID", KeyType = "HASH"
}
},
Projection = new Projection { ProjectionType = "ALL" }
};
client.CreateTable(new CreateTableRequest
{
TableName = tableName,
ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 20, WriteCapacityUnits = 10 },
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement
{
AttributeName = "RID",
KeyType = KeyType.HASH // Partiton Key (Unique)
},
new KeySchemaElement
{
AttributeName = "CLI_RID",
KeyType = KeyType.RANGE // Sort Key
}
}
,
AttributeDefinitions = new List<AttributeDefinition>
{
new AttributeDefinition {
AttributeName = "RID",
AttributeType = ScalarAttributeType.N
},
new AttributeDefinition {
AttributeName = "CLI_RID",
AttributeType = ScalarAttributeType.N
}
},
GlobalSecondaryIndexes = { cli_ridIndex }
});
But when attempting to query it,
var request = new QueryRequest
{
TableName = "TNAArchive",
KeyConditionExpression = "CLI_RID = :v_cli_rid",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{":v_cli_rid", new AttributeValue { S = "905466" }}}
};
var response = client.Query(request);
I get this error:
Query condition missed key schema element: RID
I guess I'm not really understanding how to do this.
According to your table structure, you won't be able to perform Query on the table but you have to Scan it which we need to avoid.
To perform Query you need to modify certain things
1) Add a Global Secondary Index(GSI) with the field CLI_RID as Hash
2) Now You Query GSI by passing CLI_RID and add query filter with condition <> your value
Here is the reference link.
Edit: Your Main table structure will be same no need to change, but you need to add one more GSI with Hash key as CLI_RID and project required table attribute.
Now you need to query your GSI instead of the table with a hash key as CLI_RID, you don't need to pass RID here.
here is the link on how to add GSI on table.
If CLI_RID is not present in a master table then that record will not be reflected in the GSI so no need to worry.
Edit 2: Just add (IndexName = NameOFYourIndex) attribute while querying and everything should work.
var request = new QueryRequest
{
TableName = "TNAArchive",
IndexName = "NameOfYourIndex",
KeyConditionExpression = "CLI_RID = :v_cli_rid",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{":v_cli_rid", new AttributeValue { S = "905466" }}}
};
Hope that helps
Related
I want to transform my API return result
{ "type": "animal" , "age": 3 }
into
{ "myAnimal": [ "type": "cat", "age": 3 ] }
I tried doing
var myAnimal = JsonConvert.SerializeObject(new { MyAnimal = originalResult });
and return it
However, the API return type is set up as <IEnumerable<AnimalModel> whereas variable myAnimal is string type after JsonConvert. So there's a conflict.
I still want to return IEnum, is there anyway for me to work on this?
Thank you in advance!
The json you want is not valid. But if you want to convert an object to an array, IMHO , the easiest way is to parse your origional result
var origAnimal = new { type = "animal", age = 3 };
var newAnimal = new JObject();
newAnimal["myAnimal"] = new JArray(JObject.FromObject(origAnimal).Properties()
.Select(jo => new JObject(jo)));
var json = newAnimal.ToString();
valid json
{
"myAnimal": [
{
"type": "animal"
},
{
"age": 3
}
]
}
or you could be needed this json
var newAnimal = new JObject();
newAnimal["myAnimal"] = new JArray( JObject.FromObject(origAnimal));
var json = newAnimal.ToString();
json
{
"myAnimal": [
{
"type": "animal",
"age": 3
}
]
}
or you have acces to API you will have to change the input parameter to JObject. In this case you don need to parse
public IActionResult MyApi(JObject myAnimal)
{
var newAnimal = new JObject();
newAnimal["myAnimal"] = new JArray(myAnimal.Properties().Select(jo => new JObject(jo)));
...
}
I'm having a error when I use c# API clockify
When I create a Project with Memberships I get this error:
"{\"message\":\"Internal server error.\",\"code\":500}"
When i comment Memberships attribute the project it's created.
var assignees = new List<MembershipRequest>();
var member = new MembershipRequest();
member.MembershipStatus = MembershipStatus.Active;
member.MembershipType = "PROJECT";
member.UserId = "UserId";
assignees.Add(member);
var project = new ProjectRequest
{
Name = "ProjetName",
ClientId = "ClienteID",
IsPublic = false,
Billable = true,
Color = "#000000",
Memberships = assignees
};
var clockify = new ClockifyClient("APIKey");
var response = await clockify.CreateProjectAsync("WorkSpaceID", project).ConfigureAwait(true);
Memberships are a nested object.
So if it would work, the code would be
{
Name = "ProjetName",
ClientId = "ClienteID",
IsPublic = false,
Billable = true,
Color = "#000000",
"memberships": [{
"userId": assignee
}]
}
However this is not included in the endpoint, so what I did to make this work was:
PATCH /workspaces/{workspaceId}/projects/{projectId}/memberships
{
"memberships": [
{
"userId": assignee
}
]
}
I'm trying to get the key values of the documents in my Firestore database, but I'm not getting it.
This value below:
This my code:
function objectsToArray(objects) {
var outputArray = [];
for (var i in objects){
outputArray.push([
objects[i].fields.id, objects[i].fields.data, objects[i].fields.acao,
objects[i].fields.categoria, objects[i].fields.movimentos, objects[i].fields.descricao
]);
}
return outputArray;
}
My output Logger.log of JSON:
[20-01-11 19:42:06:370 CET] [{"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc","fields":
{"categoria":"SUPERMERCADO","data":"2019-07- 31T00:00:00.000Z","descricao":"","acao":"Despesa","movimentos":23.82,"id":107},
"createTime":"2019-12-31T14:35:47.959299Z","updateTime":"2019-12- 31T14:35:47.959299Z"},
any suggestions?
Thanks
The original data seems to be structured like this:
[
{
"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc",
"fields": {
"categoria":"SUPERMERCADO",
"data":"2019-07- 31T00:00:00.000Z",
"descricao":"",
"acao":"Despesa",
"movimentos":23.82,
"id":107
},
"createTime":"2019-12-31T14:35:47.959299Z",
"updateTime":"2019-12- 31T14:35:47.959299Z"
},
You want the value: 0MwgqEm9abho3bpB5yCc
Which is in the element with the name property key.
function objectsToArray(objects) {
var L,nameValue,finalValue;
objects = [
{
"name":"projects/orcamento- b37bb/databases/(default)/documents/orcamento/0MwgqEm9abho3bpB5yCc",
"fields": {
"categoria":"SUPERMERCADO",
"data":"2019-07- 31T00:00:00.000Z",
"descricao":"",
"acao":"Despesa",
"movimentos":23.82,
"id":107
},
"createTime":"2019-12-31T14:35:47.959299Z",
"updateTime":"2019-12- 31T14:35:47.959299Z"
},
]
var outputArray = [];
L = objects.length;
for (var i=0;i<L;i++){
nameValue = objects[i].name;
Logger.log('nameValue: ' + nameValue)
finalValue = nameValue.slice(nameValue.lastIndexOf("/")+1);
Logger.log('finalValue: ' + finalValue)
outputArray.push(finalValue);
}
Logger.log('outputArray: ' + JSON.stringify(outputArray))
return outputArray;
}
Where can I get the SourceToken for the customer creation in Stripe?
Here is the example of the code which uses the SourceToken:
if (Request.Form["stripeToken"] != null) {
var customers = new CustomerService();
var charges = new ChargeService();
var customer = customers.Create(new CustomerCreateOptions {
Email = Request.Form["stripeEmail"],
SourceToken = Request.Form["stripeToken"]
});
var charge = charges.Create(new ChargeCreateOptions {
Amount = 500,
Description = "Sample Charge",
Currency = "usd",
CustomerId = customer.Id
});
Console.WriteLine(charge);
}
What is SourceToken out of this json?
{
"id": "tok_1EL43MKfPgrABB02rh7IGG7l",
"object": "token",
"card": {
"id": "card_1EL43MKfPgrABB02yFgReMXc",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2020,
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": "foo#gmail.com",
"tokenization_method": null
},
"client_ip": "194.44.142.6",
"created": 1554278556,
"email": "foo#gmail.com",
"livemode": false,
"type": "card",
"used": false
}
Here is how I got the json:
var stripe = Stripe('pk_test_ob6s7KZxZU1mouJbbsuFBjEe');
var handler = StripeCheckout.configure({
key: 'pk_test_ob6s7KZxZU1mouJbbsuFBjEe',
token: function(token, args) {
var stop = 0;
}
});
document.getElementById('myBtn').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Demo Site',
description: '2 widgets ($20.00)',
amount: 2000
});
e.preventDefault();
});
on line var stop = 0;.
The example is taken from here.
The SourceToken in that JSON is "tok_1EL43MKfPgrABB02rh7IGG7l".
After the customer is created with the source attached (e.g. that token), you can retrieve the customer which will include that customer's sources (they'll be listed in the sources object under the customer record).
The token itself gets consumed during the creation of the source and customer, and the result is a source that's reusable.
I have a sample Table info as -
items: [{
"key1":"value1",
"key2":"value2"
}
]
I have to update this like below -
items: [{
"key1":"value1",
"key2":"value2",
"key3": [{"subkey1":"value1"}, {"subkey2:"value2"}]
}
]
You can use Update API with SET operator.
In the below example, categoryList has the key3 value mentioned in OP.
Sample Code:-
var docClient = new AWS.DynamoDB.DocumentClient();
var categoryList = [{"subkey1":"value1"}, {"subkey2" : "value2"}];
var params = {
TableName : "Movies",
Key : {
"yearkey" : 2016,
"title" : "The Big New Movie 1"
},
UpdateExpression : "set #category = :categoryList",
ExpressionAttributeNames: {
'#category' : 'category'
},
ExpressionAttributeValues: {':categoryList' : categoryList},
ReturnValues: 'UPDATED_NEW'
};
console.log("Updating the item...");
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data));
}
});