Getting error in Excel to MongoDB JSON convert using Java [duplicate] - java

I figured out that of course . and SPACE aren't allowed. Are there other forbidden characters ?

You can use any (UTF8) character in the field name which aren't
special (contains ".", or starts with "$").
https://jira.mongodb.org/browse/SERVER-3229
https://stackoverflow.com/a/7976235/311220
It's generally best to stick with lowercase alphanumeric with underscores though.

Something else to look out for is the fact that you can make a property name called "query" but then use query operators on it, making it awkward to do a large number of queries.
Example:
Insert document with a property named
db.coll.insert({ query: 'foo' });
Equality query works:
db.coll.findOne({ query: 'foo' });
Not equal ($ne) does not:
db.coll.findOne({ query: { $ne: 'bar' } });

Related

How to escape reserved keywords in DynamoDB ExecuteStatementRequest?

I'm trying to issue an update ExecuteStatementRequest using DynamoDB and Java SDK 2.0. I'm struggling to escape keywords that are columns in my table schema.
The following statement:
var response = client.executeStatement(ExecuteStatementRequest.builder()
.statement("""
UPDATE "my-table"
SET value=12.5
WHERE assignmentId='item1#123#item2#456#item3#789'
RETURNING ALL NEW *
""")
.build());
When I run the following statement (notice that value column is a reserved keyword) I get the following error:
Exception in thread "main"
software.amazon.awssdk.services.dynamodb.model.DynamoDbException:
Statement wasn't well formed, can't be processed: Expected identifier
for simple path (Service: DynamoDb, Status Code: 400, Request ID: XXX)
If instead of value I change the column name to val, the statement works fine. I know that in UdateItem operations I can pass in an array of expressionAttributeNames to replace keywords with aliases. Is there a similar primitive I can use for ExecuteStatementRequest?
The documentation you link explicitly says what you need to do:
You can use a reserved keyword as a quoted identifier with double
quotation marks (for example, "user").
That is the SQL standard way of escaping reserved words. In other words, use "value" (or possibly "VALUE") in your statement instead of value.

What should be included/imported to recognize "$" operation join function of in my spark -java code?

Using spark-sql-2.4.1v with java8.
I am trying to join two data sets as below:
computed_df.as('s).join(accumulated_results_df.as('f),$"s.company_id" === $"f.company_id","inner")
Which is working fine in databrick's notebooks.
But when I try to implement the same in my spark java code in my Ide.
It wont recognize the "$" function/operator even after including
import static org.apache.spark.sql.functions.*;
So what should be done to use it in my spark java code ?
thanks
The answer is org.apache.spark.sql.Column. See This.
public class Column
...
df("columnName") // On a specific DataFrame.
col("columnName") // A generic column no yet associcated with a DataFrame.
col("columnName.field") // Extracting a struct field
col("`a.column.with.dots`") // Escape `.` in column names.
$"columnName" // Scala short hand for a named column.
expr("a + 1") // A column that is constructed from a parsed SQL Expression.
lit("abc") // A column that produces a literal (constant) value.

neo4j escaping for regular expressions

I receive a user input keyword and want to use it to search my database. I built a query that looks something like this:
db.execute("MATCH (n:User) WHERE n.firstname CONTAINS {keyword} OR n.lastname CONTAINS {keyword} RETURN n.username", params);
But this isn't case sensitive, so I thought of manually building the expression and using regular expressions, sort of as follows:
db.execute("MATCH (n:User) WHERE n.firstname =~ '(?i).*" + keyword + ".*' OR n.lastname =~ '(?i).*" + keyword + ".*' RETURN n.username");
I'm looking either for a function for escaping the regex or a better solution for making the query case-insensitive. Any ideas?
I would suggest storing the properties as all lowercase (or uppercase) and then using the Cypher lower() function to convert user input to lowercase for comparison.
Add lowercase name properties
MATCH (n:User)
SET n.lowerFirstName = lower(n.firstname),
n.lowerLastName = lower(n.lastname)
Find lower case matches based on user input
db.execute("MATCH (n:User) WHERE n.lowerFirstName CONTAINS lower({keyword}) OR n.lowerLastName CONTAINS lower({keyword}) RETURN n.username", params);

Hibernate/JPA MSSQL replaces Brackets with `

I am dealing with a database that has column with spaces. So I try to enclose the Column in brackets like this...
#Column(name = "[Licensee Address]")
However when I run the app. Hibernate replaces Brackets with this
`Licensee Address`
Which throws a error
Incorrect syntax near '`'.
MSSQL uses brackets not quotes. How do I fix this ??
When I do it without the brackets it generates the following sql...
Hibernate: select licenseein0_.WRRAppID as WRRAppID1_1_0_, licenseein0_.ApplicantTypeID as Applican2_1_0_, licenseein0_.BusinessActivity as Business3_1_0_, licenseein0_.CompanyID as CompanyI4_1_0_, licenseein0_.CreatedBy as CreatedB5_1_0_, licenseein0_.CreatedDate as CreatedD6_1_0_, licenseein0_.DBAName as DBAName7_1_0_, licenseein0_.FCCCallSign as FCCCallS8_1_0_, licenseein0_.FCCFileNumber as FCCFileN9_1_0_, licenseein0_.FCCRegistration# as FCCRegi10_1_0_, licenseein0_.FedTaxID# as FedTaxI11_1_0_, licenseein0_.LicenseActionID as License12_1_0_, licenseein0_.LicenseeName as License13_1_0_, licenseein0_.RadioService as RadioSe14_1_0_, licenseein0_.RadioUse as RadioUs15_1_0_, licenseein0_.SubmittedDate as Submitt16_1_0_ from Licensee_Information licenseein0_ where licenseein0_.WRRAppID=?
And the error message I get is...
Incorrect syntax near the keyword 'as'.
Figured it out. Had to incorrect hibernate dialect set up. It must be the following
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServerDialect
The column name isn't [Licensee Address], the column name is Licensee Address. If you remove the [ and ] it should work. If you are writing a query in T-SQL and you need to reference a column named with a space, special characters or reserved words, you need to put the [ and ] so the query parser knows where the column name starts and stops. But you are not writing T-SQL, so you don't need to do that.

How can I save this JSON in mongoDB?

I need save this JSON: {edbff2886c8ca7aa1bd02b092aa03930.zip=T001.zip}. It's from a Map<String,String>. Why isn't possible?
com.mongodb.WriteConcernException: { "serverUsed" : "127.0.0.1:27017" , "ok" : 1 , "n" : 0 , "updatedExisting" : false , "err" : "The dotted field 'edbff2886c8ca7aa1bd02b092aa03930.zip' in 'filenameMap.edbff2886c8ca7aa1bd02b092aa03930.zip' is not valid for storage." , "code" : 57}
Query
public Validacao update(String id, Validacao validacao) {
mongoCollection.update("{_id : #}", id)
.with("{$set: #}", validacao);
return validacao;
}
This:
{edbff2886c8ca7aa1bd02b092aa03930.zip=T001.zip}
isn't valid JSON to start with. You'd need quotes around the field name and the value.
However, even that wouldn't be enough. From the MongoDB documentation:
Field names cannot contain dots (i.e. .) or null characters, and they must not start with a dollar sign (i.e. $). See Dollar Sign Operator Escaping for an alternate approach.
Now, depending on what other characters your field name might have, you could potentially replace all dots with something else before storing - or you may need some sort of escaping mechanism.

Categories

Resources