Hi I'm trying to make a simple adnroid app that works with dynamodb and following through this tutorial:
Link of Tutorial
I have been able to connect with the dbClient and access the table. I can successfully perform the dbTable.putItem and also other methods like dbTable.getTableDescription.
I'm having trouble understanding how to execute and dbTable.getItem method which requires a Primitive as an input. I don't quite understand how to use the Hashkey or primary key.
My table looks like this:
Click image
these are the hash keys
Primary Key
When I execute this line of code:
Document doc = dbTable.getItem(new Primitive("1"));
where 1 is the value of the first value in the table.
I get this error.
java.lang.IllegalStateException: hash key type does not match the one
in table defination
at com.amazonaws.mobileconnectors.dynamodbv2.document.Table.makeKey(Table.java:720)
at com.amazonaws.mobileconnectors.dynamodbv2.document.Table.getItem(Table.java:298)
at com.example.user.dynamodb.MainActivity$1.run(MainActivity.java:65)
Related
I am using Java and SQL to push data to a Timestamp partitioned table in BigQuery. In my code, I specify the destination table:
.setDestinationTable(TableId.of("MyDataset", "MyTable"))
When I run it, it creates a table perfectly. However, when I attempt to insert new data, it throws a BigQueryException claiming the table already exists:
Exception in thread "main" com.google.cloud.bigquery.BigQueryException:
Already Exists: Table MyProject:MyDataset.MyTable
After some documentation digging, I found a solution that works:
.setWriteDisposition(WriteDisposition.WRITE_APPEND)
Adding the above appends any data (even if it's duplicate). I'm not sure why the default setting for .setDestinationTable() is the equivalent of WRITE_EMPTY, which returns a duplicate error. The google docs for .setDestinationTable() are:
Describes the table where the query results should be stored. If not
present, a new table will be created to store the results.
The docs should probably clarify the default value.
When trying to do a delete via AWS Java SDK I get the error
The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 52N303HS3D535K28KSN3R3803VVV4KQNSO5AEMVJF66Q9ASUAAJG)
I have a delete item spec defined that looks like this
DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
.withPrimaryKey("pk", messageId)
.withConditionExpression("#ip > :val")
.withNameMap(new NameMap()
.with("#ip", "timestamp"))
.withValueMap(new ValueMap()
.withNumber(":val", 0))
.withReturnValues(ReturnValue.NONE);
And my table is created like this
List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("pk")
.withAttributeType(ScalarAttributeType.S));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("timestamp")
.withAttributeType(ScalarAttributeType.N));
List<KeySchemaElement> keySchema = new ArrayList<>();
keySchema.add(new KeySchemaElement()
.withAttributeName("pk")
.withKeyType(KeyType.HASH));
keySchema.add(new KeySchemaElement()
.withAttributeName("timestamp")
.withKeyType(KeyType.RANGE));
I'm wondering if the sort key for timestamp is causing this issue. Do I need to specify the timestamp other than > 0?
The issue is that you must specify both the hash and range key when you delete an object. Your hash key is "pk" and your range key is "timestamp" but you are only passing in the hash key into the withPrimaryKey method.
It looks like you are trying to delete multiple items at a time. This is not possible with DynamoDB. You will first need to do a query on the key and you can apply your condition expression to that to only retrieve the keys of the items you want to delete. However, you will then need to call the delete API individually for each record or use the batch API to delete records in batches while still specifying the hash and range key for each individual item.
I'm trying to do upsert using mongodb driver, here is a code:
BulkWriteOperation builder = coll.initializeUnorderedBulkOperation();
DBObject toDBObject;
for (T entity : entities) {
toDBObject = morphia.toDBObject(entity);
builder.find(toDBObject).upsert().replaceOne(toDBObject);
}
BulkWriteResult result = builder.execute();
where "entity" is morphia object. When I'm running the code first time (there are no entities in the DB, so all of the queries should be insert) it works fine and I see the entities in the database with generated _id field. Second run I'm changing some fields and trying to save changed entities and then I receive the folowing error from mongo:
E11000 duplicate key error collection: statistics.counters index: _id_ dup key: { : ObjectId('56adfbf43d801b870e63be29') }
what I forgot to configure in my example?
I don't know the structure of dbObject, but that bulk Upsert needs a valid query in order to work.
Let's say, for example, that you have a unique (_id) property called "id". A valid query would look like:
builder.find({id: toDBObject.id}).upsert().replaceOne(toDBObject);
This way, the engine can (a) find an object to update and then (b) update it (or, insert if the object wasn't found). Of course, you need the Java syntax for find, but same rule applies: make sure your .find will find something, then do an update.
I believe (just a guess) that the way it's written now will find "all" docs and try to update the first one ... but the behavior you are describing suggests it's finding "no doc" and attempting an insert.
I have been trying to retrieve information from querying a specific Asset(Story/Defect) on V1 using the VersionOne.SDK.Java.APIClient. I have been able to retrieve information like ID.Number, Status.Name but not Requests.Custom_SFDCChangeReqID2 under a Story or a Defect.
I check the metadata for:
https://.../Story?xsl=api.xsl
https://.../meta.V1/Defect?xsl=api.xsl
https://.../meta.V1/Request?xsl=api.xsl
And the naming and information looks right.
Here is my code:
IAssetType type = metaModel.getAssetType("Story");
IAttributeDefinition requestCRIDAttribute = type.getAttributeDefinition("Requests.Custom_SFDCChangeReqID2");
IAttributeDefinition idNumberAttribute = type.getAttributeDefinition("ID.Number")
Query query = new Query(type);
query.getSelection().add(requestCRIDAttribute);
query.getSelection().add(idNumberAttribute);
Asset[] results = v1Api.retrieve(query).getAssets();
String RequestCRID= result.getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= result.getAttribute(idNumberAttribute).getValue().toString();
At this point, I can get some values for ID.Number but I am not able to retrieving any information for the value Custom_SFDCChangeReqID2.
When I run the restful query to retrieve information using a browser from a server standpoint it works and it does retrieve the information I am looking for. I used this syntax:
https://.../rest-1.v1/Data/Story?sel=Number,ID,Story.Requests.Custom_SFDCChangeReqID2,Story.
Alex: Remember that Results is an array of Asset´s, so I guess you should be accessing the information using something like
String RequestCRID= results[0].getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= results[0].getAttribute(idNumberAttribute).getValue().toString();
or Iterate through the array.
Also notice that you have defined:
Asset[] results and not result
Hi thanks for your answer! I completely forgot about representing the loop, I was too focus on the retriving information part, yes I was actually using a loop and yes I created a temporary variable to check what I was getting from the query in the form
Because I was getting the variables one by one so I was only using the first record. My code works after all. It was just that What I was querying didn't contain any information of my use, that's why I was not finding any. Anyway thanks for your comment and observations
Folks,
I am using Play 1.2.5. The database is Oracle 10g and I am using an existing table for my application.
I am generating a unique key like this:
#GeneratedValue(strategy=GenerationType.SEQUENCE)
public int transactionId;
When I use the below code, the transactionId is generated and saved successfully ib the database:
transactionDetails.save();
But I am not able to get the uniquely generated transactionId once the save operation performs successfully. The save method returns a type <JPABase>. Then how can I retrieve the transactionId after a successful save operation from the <JPABase> ?
Note: I don't want to make another DB hit for fetching the transactionId because I believe that there might be some way to retrieve it for a successful save operation.
Please let me know about this.
Thanks,
You should be able to write
TransactionDetails savedDetails = transactionDetails.save();
save() method actually is not returning JPABase, it is declared as so you will get the saved entity