Reading error results from mongod in java after insert attempt - java

I am getting started with mongodb and I would like to do an operation where I attempt to insert a user with a username, password, and email. I have built unique indexes on username and email so the insert will fail if the specified username or email already exists.
So now I would like to report to the user that either, their email is already registered, or that the username the chose is taken. So I have gotten as far as:
CommandResult result = db.getLastError();
However, I dont see an easy way to read the error other than parsing through the single error message that it is giving me.
{ "serverUsed" : "127.0.0.1:27017" ,
"err" : "E11000 duplicate key error index: mojulo.users.$username_1 dup key: { : \"blahblah\" }" ,
"code" : 11000 ,
"n" : 0 ,
"connectionId" : 12 ,
"ok" : 1.0}
Also it appears that this is only reporting the first error that it encounters, is there anyway to do the check for both email and username in a single query?

If you want to do a check for both, then you will have to issue a query yourself to check for it. If you just insert documents, then it will report the first violation of a unique index only. The code E11000 should only indicate a duplicate key error which should make it easy to spot that it happens. You will need to parse the error message to figure out the collection though.

Related

getting “input value must be specified” error while sending graphQL query through java

I am trying to get some data by using graphQL. code should be in following format ,but while debugging ,i got error as following:
[{"message":"\"input\" value must be specified","category":"graphql-input","locations":[{"line":1,"column":10}]}]
Basically I want data in following format in java .
query { customer { id addresses { id firstname lastname street city region { region_code region region_id } postcode country_id telephone } } }
query i wrote in java is as following :
return q -> q.customerId()
.id()
.firstname()
.lastname()
.street()
.city()
.region(getCustomerAddressRegionQuery())
.postcode()
.countryId()
.telephone();
so what java query should i write in order to get the output. attached image of error here as well
error says:
"input" value must be specified","category":"graphql-input","locations":[{"line":1,"column":10}
The problem is not in returning the value from java, the problem is with the graphql query you are performing. The query expects an input named 'input' but you are not giving it. What does your GraphQL query look like?
If you are testing it through postman i think you should enter input data, e.g in body.

Stream insert from java: templateSuffix

I would like to stream data from my java code to a BigQuery table using templateSuffix, but I can't make it work properly. My code:
return bigquery.tabledata()
.insertAll(
projectId,
datasetId,
tableId,
new TableDataInsertAllRequest()
.setTemplateSuffix(templateSuffix)
.setRows(singletonList(row))
).execute();
When I run it with projectId, datasetId, MyTable20160426 and 20160426, I get the error:
"message" : "404 Not found: Table projectId:datasetId.MyTable20160426"
When I run it with projectId, datasetId, MyTable and 20160426, I get the error:
"message" : "404 Not found: Table projectId:datasetId.MyTable"
The table MyTable already exists and already is templated on date (I used the bulk upload for GCS) (20160426 is the today date)
How cat I make it work ?
Where should I look to understand what's wrong ?
Thanks
First, the base table projectId:datasetId.MyTable should exist and should already have a schema. Thisis how BigQuery knows how to find the schema of the templated table that gets created.
Second, You should pass MyTable and instead of MyTable20160426 as the table ID in your request.
Third, the existence (or non-existence) of a table is cached. So if you get a "not found" error and then create the table, you'll still get a "not found" error for up to a half hour.
It sounds like you might be able to wait and try again. If this doesn't work, please provide the actual project, dataset, and table ids you're using and e-mail the details to tigani#google.com, and I can help look into what is going on.

How to get last status message of each record using SQL?

Consider the following scenario.
I have a Java application which uses Oracle database to store some status codes and messages.
Ex: I have patient record that process in 3 layers (assume 1. Receiving class 2. Translation class 3. Sending class). We store data into database in each layer. When we run query it will show like this.
Name Status Status_Message
XYZ 11 XML message received
XYZ 21 XML message translated to swift format
XYZ 31 Completed message send to destination
ABC 11 XML message received
ABC 21 XML message translated to swift format
ABC 91 Failed message send to destination
On Java class I am executing the below query to get the last status message.
select STATUS_MESSAGE from INTERFACE_MESSAGE_STATUS
where NAME = ? order by STATUS
I publish this status message on a webpage. But my problem is I am not getting the last status message; it's behaving differently. It is printing sometimes "XML message received", sometimes "XML message translated to swift format", etc.
But I want to publish the last status like "Completed message send to destination" or "Failed message send to destination" depending on the last status. How can I do that? Please suggest.
You can use a query like this:
select
i.STATUS_MESSAGE
from
INTERFACE_MESSAGE_STATUS i,
(select
max(status) as last_status
from
INTERFACE_MESSAGE_STATUS
where
name = ?) s
where
i.name = ? and
i.status = s.last_status
In the above example I am assuming the status with the highest status is the last status.
I would recommend you to create a view out of this select query and then use that in your codebase. The reason is that it is much easier to read and makes it possible to easily select on multiple last_statuses without complicating your queries too much.
You have no explicit ordering specified. As the data is stored in a HEAP in Oracle, there is no specific order given. In other words: many factors influence the element you get. Only explicit ORDER BY guarantees desired order. And/or creating a index on some of the rows.
My suggestion: add a date_created field to your DB and sort based on that.

Google BigQuery - query ran successfully but results not pushed to destination table

We run a nightly query against BigQuery via the Java REST API that specifies a destination table for the results to be pushed to (write disposition=WRITE_TRUNCATE). Today's query appeared to run without errors but the results were not pushed to the destination table.
This query has been running for a few weeks now and we've had no issues. No code changes were made either.
Manually running it a second time after it "failed" worked fine. It was just this one glitch that we spotted and we're concerned it may happen again.
Our logged JSON response from the "failed" query looks fine (I've obfuscated any sensitive data):
INFO: Job finished successfully: {
"configuration" : {
"dryRun" : false,
"query" : {
"createDisposition" : "CREATE_IF_NEEDED",
"destinationTable" : {
"datasetId" : "[REMOVED]",
"projectId" : "[REMOVED]",
"tableId" : "[REMOVED]"
},
"priority" : "INTERACTIVE",
"query" : "[REMOVED]",
"writeDisposition" : "WRITE_TRUNCATE"
}
},
"etag" : "[REMOVED]",
"id" : "[REMOVED]",
"jobReference" : {
"jobId" : "[REMOVED]",
"projectId" : "[REMOVED]"
},
"kind" : "bigquery#job",
"selfLink" : "[REMOVED]",
"statistics" : {
"creationTime" : "1390435780070",
"endTime" : "1390435780769",
"query" : {
"cacheHit" : false,
"totalBytesProcessed" : "12546"
},
"startTime" : "1390435780245",
"totalBytesProcessed" : "12546"
},
"status" : {
"state" : "DONE"
}
}
Using the "try it!" for Jobs/GET here and plugging in the job id also shows the job was indeed successful and matches our logged output (pasted above).
Checking the web console shows the destination table has been truncated but not updated. Weirdly, the "Last Modified" has not been updated (I did try refreshing the page numerous times):
http://i.stack.imgur.com/384NL.png
Has anyone experienced this before with BigQuery - a query appearing to run successfully but if a destination/reference table was specified the results were not pushed yet the table was truncated?
I am a developer on the BigQuery team. I've looked up the details of you job from the breadcrumbs you left (your query was the only one that started at that start time).
It looks like your destination table was truncated at 4:09 pm today PST, which is the time your job ran, but it was left empty -- the query that truncated it didn't actually fill in any information.
I'm having a little bit of trouble piecing together the details, because one of the source tables appears to have been overwritten (the left table in your left outer of join was created at 4:20 PM).
However, there is a clue in the "total bytes processed" field -- it says that the query only processed 12K of data. The internal statistics say that only 384 rows were involved in the query among both tables that were involved.
My guess is that the query legitimately returned 0 rows, so the table was cleared.
There is a bug in that deleting all of the data in a table doesn't update the last modified time. We use last modified to mean either ast time the metadata was updated (like description, schema, etc) or the last time the table had data added to it). But if you just truncate the table, that doesn't update the metadata or add data, so we end up with a stale last-modified time.
If this doesn't sound like a reasonable chain of events, we'll need more information from you about how to debug it (especially since it looks like the tables involved have been modified since you ran this query), and a way that we can reproduce it would be great.
So, we figured out what the problem is with this. It failed again a few times over the last few days so we dug in further.
The query that is being executed is dependant on a another query which is executed immediately before it. Although we do wait for the first query to finish (job status = "DONE"), it appears that behind the scenes it's actually not fully complete and it's data is not yet available to be used.
Current process is:
Fetch data from another data source and stream the results into table A
When (1) is complete (poll job id and get status "DONE") submit another query which uses the results in table A to join on to create table B
Table A's data is not yet available so query from (2) results in an empty table
We've noticed it takes about 5-10 seconds for the data to actually appear and be available in BigQuery when using streaming for the first query.
We used a fairly ugly workaround - simply wait a few seconds after the first query before running the next one. Not exactly elegant but it works.

Mongo DuplicateKey error despite no overlap

I have a well-logged pool of several java servers behind an F5 load balancer (professionally managed, it's not sending traffic to >1 host) running Tomcat with my application installed, connecting to a sharded mongo cluster. I'm using a base64-encoded SHA-1 hash of the primary natural key as the _id. When a new record is to be created, I do a pretty basic:
BasicDBObject query = new BasicDBObject();
query.put("userId", userId);
query.put("_id", id);
DBObject user = getUsersCollection().findOne(query);
if (user == null) {
getUsersCollection().insert(new UserObject(userId));
}
This is simplified. In fact there are multiple checks for the pre-existence of this user, including one which should throw a custom exception, and none are triggered. The traffic logs indicate a single incoming create request, and here's an example of what happens:
2014-01-19 20:03:45,167 [http-bio-7950-exec-827]:[...] : ERROR FATAL [...] - Internal server error
[...]: com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "[...]" , "singleShard" : "replicaset_2/host1:27017,host2:27017,host3:27017" , "err" : "E11000 duplicate key error index: Users.$_id_ dup key: { : \"HASH\" }" , "code" : 11000 , "n" : 0 , "lastOp" : { "$ts" : 1390190614 , "$inc" : 1} , "connectionId" : 335764 , "ok" : 1.0}
Yet in my Users collection the record has been created:
db.Users.findOne({_id:"HASH"}):
{
"_id" : "HASH",
"createDate" : ISODate("2014-01-20T04:03:45.161Z"),
...
}
I'm pasting this as important because of the timestamps. We have a timezone issue, but that aside I interpret the 6ms difference as clock skew between the mongo cluster and my application servers. There is no other record of this incoming traffic (and it is logged as it bounces from server to server, even - nothing else!) So I am 99.999% confident that my SINGLE LEGITIMATE insert call is both inserting and throwing an error.
Any theories as to how/why this is happening would be greatly appreciated. I'll run tracers and examples if needed to answer questions with more information.
You are searching for a user using both _id and userId fields. Try to comment out this line: query.put("_id", id);.
It's not clear in your code where Java variable userId comes from. It's also not clear how UserObject sets an _id if at all.
Overall it looks like the way you search for user and the way you create him does not match, i.e. what defines a unique key on that user.
One fix could to replace these lines:
query.put("userId", userId);
query.put("_id", id);
with:
query.put("_id", userId);
To make _id field to be your userId.

Categories

Resources