InfluxDB Query issue when measurement name have hyphen in it - java

I am using Java and querying InfluxDb as shown below,
queryResult1 = influxDB.query(new Query("SELECT last(timestamp) FROM vale" , eachDatabase));
This statement is working fine, but when the name have any of the special character for e.g. if measurement name is "vale-ab" instead of vale, it won't work.
Error i am getting is:
java.lang.RuntimeException: {"error":"error parsing query: found -, expected ; at line 1, char 34"}
Any idea how can i escape measurement name inside the queries.

You need to wrap your measurement with double quotes ".
Bad:
select * from a-b
ERR: error parsing query: found -, expected ; at line 1, char 16
Good:
select * from "a-b"
name: a-b
time tag1 value
---- ---- -----
1434089562000000000 10i 5
I don't have Java installed on this machine but the code below should solve your problem.
queryResult1 = influxDB.query(new Query("SELECT last(timestamp) FROM \"vale-vale\"" , eachDatabase));

Related

Spring data query substring

I have a column called PAYMENT_REF that contains value of the following sequence:
DBC2020999999999999
It will always have the length 19 and starts with DBC followed by the year which is this part: DBC2020
The following numbers after the year is a sequence number which is 999999999999.
Basically I need to do a query that find the max value only from the sequence number ignoring the static DBC and year 2020.
Basically when I run the following query on oracle developer ide I get the correct result:
select MAX(SUBSTR(PAYMENT_REF,7,19))
from PAYMENT p
where PAYMENT_REF is not null;
So I tried the following query which looks should get the max value of:
public interface PaymentRepository extends JpaRepository<Payment, String> {
#Query(" select MAX(SUBSTRING(p.paymentRef,7,19)) from payment p where p.paymentRef is not null")
int getMaxRefNumber();
}
But I get a compilation error cannot resolve symbol: payment, any idea what I a missing here please?
I guess you should correct your query in the following way:
#Query("select MAX(SUBSTRING(p.paymentRef, 8, 12)) from Payment p where p.paymentRef is not null")
String getMaxRefNumber();
As it is stated in the documentation:
SUBSTRING
Extracts a portion of a string value. The second argument denotes the starting position, where 1 is the first character of the string. The third (optional) argument denotes the length.
1 8
| |
DBC2020999999999999
\ /
12 chars
P.S. To be honest I wasn't able to cast string to number in the hql.
The statement like the following:
select MAX( cast(SUBSTRING(p.paymentRef, 8, 12) as long) )
from Payment p where p.paymentRef is not null
will not work, the following exception is raced:
java.lang.IllegalArgumentException: Type specified for TypedQuery [java.lang.Long] is incompatible with query return type [class java.lang.String]
at org.hibernate.internal.AbstractSharedSessionContract.resultClassChecking(AbstractSharedSessionContract.java:863)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:817)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)

How to get the difference between two Timestamp in Postgres SQL in Java

Query query = session.createSQLQuery("SELECT ((extract (epoch from (end_time::timestamp - start_time::timestamp))))::integer from Trip_Details where td_id = "+transacId+" ");
I am trying this query to get the diff between two timestamps i.e.,start_time, stop_time.
but getting below exception
ERROR: stderr - org.hibernate.QueryException: Not all named parameters have been set: [:timestamp, :integer] [SELECT ((extract (epoch from (end_time::timestamp - start_time::timestamp))))::integer from Trip_Details where td_id = 1 ]
Its working fine on PG Admin console of postgres and getting output as 202 sec in integer format.
Please help me to trace out the error and resolve it.
Thanks in Advance
I suspect double quotes treated you bad. A colon before word in Java is used to set variable. I assume if you change your code to:
Query query = session.createSQLQuery("SELECT cast((extract (epoch from (cast(end_time as timestamp)- cast(start_time as timestamp)))) AS integer ) from Trip_Details where td_id = "+transacId+" ");
It should start working...

What is the correct way to set expression with JasperReports API?

I am using text fields for displaying column names . For showing the corresponding name of the column I have tried the following method:
Method 1:
textField.setX(currentXPos);
textField.setY(0);
textField.setWidth(columnWidth);
textField.setPrintWhenDetailOverflows(false);
textField.setHeight(colDtlBandHeight);
textField.setStretchWithOverflow(true);
textField.setStretchType(StretchTypeEnum.RELATIVE_TO_BAND_HEIGHT);
textField.setStyle(normalFont);
textField.setBlankWhenNull(true);
JRDesignExpression expression = new JRDesignExpression();
expression.setValueClass(columnClass);
expression.setText("$F{" + columnName + "}");
But on using the above method it throws an exception saying:
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. Syntax error on token "ID", delete this token
value = SHIFT ID; //$JR_EXPR_ID=44$
2. Syntax error, insert ";" to complete BlockStatements
value = BILL NO.; //$JR_EXPR_ID=45$
3. Syntax error on token ".", invalid VariableDeclarator
value = BILL NO.; //$JR_EXPR_ID=45$
4. Syntax error on token "DATE", delete this token
value = BILL DATE; //$JR_EXPR_ID=46$
But on using the below lines the column Names are set correctly .
Method 2:
textField.setExpression(new JRDesignExpression("new String(\""+colTitle+"\")"));
My doubts are:
1. For displaying the data the first method mentioned is used . Then how come there are no exceptions in that case ?
2. Why did it throw those exceptions when the same method was used for displaying column names?
3. How did the 2nd method work ?
1.:
I suppose the data is properly enclosed in quotes.
2.:
Judging by the exception explanation (e.g. Syntax error on token "ID", delete this token) the interpreter sees two values, SHIFT and ID. It seems here that quotes are missing, e.g.
"SHIFT ID"
"BILL NO."
3.:
In your first example, you create a JRDesignExpression, set the value class and set the text.
The field isn't enclosed in quotes as seen in your lower example. It should look like this:
expression.setText("\"$F{" + columnName + "}\"");
Also, you didn't assign the expression to your textField:
textField.setExpression(expression)

Why Cassandra cql query with hexadecimal literal works but textAsBinary does not?

I can do cassandra cql queries using hexadecimal literal, but when I try to use textAsBlob it does not work
For example the following works just fine:
cqlsh:sprich> SELECT * FROM "STOCK_CHECK_RTAM_ITEM_INDEX" WHERE key in (0x3631313230);
key | column1 | value
--------------+----------------+------------------------------------
0x3631313230 | 0x000330303100 | 0xf77374b5eced11e3a877005056b37d30
0x3631313230 | 0x000330303200 | 0xf7757084eced11e3a877005056b37d30
0x3631313230 | 0x000330303400 | 0xf7712ac8eced11e3a877005056b37d30
But the following two queries do not:
cqlsh:sprich> SELECT * FROM "STOCK_CHECK_RTAM_ITEM_INDEX" WHERE key in (textAsBlob("0x3631313230"));
Bad Request: line 1:84 missing EOF at ')'
text could not be lexed at line 1, char 15
cqlsh:sprich> SELECT * FROM "STOCK_CHECK_RTAM_ITEM_INDEX" WHERE key in (textAsBlob("3631313230"));
Bad Request: line 1:82 missing EOF at ')'
text could not be lexed at line 1, char 15
The following two queries do not give a syntax error but do not return the proper results as well:
cqlsh:sprich> SELECT * FROM "STOCK_CHECK_RTAM_ITEM_INDEX" WHERE key in (textAsBlob('0x3631313230'));
cqlsh:sprich> SELECT * FROM "STOCK_CHECK_RTAM_ITEM_INDEX" WHERE key in (textAsBlob('3631313230'));
Why is textAsBlob is not working? What mistake am I making in the proper use of textAsBlob?
Cassandra represents a blob data type as a hexadecimal number, such as 0x3631313230.
This is different than a text data type (which is enclosed in single quotation marks), such as '0x3631313230'.
For those reasons, in your statements:
textAsBlob doesn't work because it expects a string in single quotation marks.
When you do use single quotation marks, since 0x3631313230 is different than '0x3631313230', it doesn't return the correct results.
You can see it clearly in the following example. Assume:
CREATE TABLE test(b blob, t text, PRIMARY KEY(b));
Now execute the following INSERT:
INSERT INTO test(b,t) VALUES(0x3631313230, blobAsText(0x3631313230));
When you do a SELECT, something like this will be returned:
b | t
0x3631313230 | 61120
As you can see, 0x3631313230 is not equivalent to '0x3631313230', and that's why you should use your first query to get the correct results:
SELECT * FROM "STOCK_CHECK_RTAM_ITEM_INDEX" WHERE key in (0x3631313230);
Here's the reference:
http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cql_data_types_c.html

Why do I have an incorrect result set when I pass a String parameter?

I've debating with this problem for a while and I couldn't yet find a solution. It's very simple to explain.
I've a Java method that passes a String as parameter, and this String is a SQL Query.
When I execute this query passing the String parameter I get a result of 1 row.
ResultSet rs1 = stmt.executeQuery(sqlq);
When I type the exact same query in the code i get the correct return of 5 rows.
ResultSet rs1 = stmt.executeQuery("SELECT a.GSM_NO AS
A_GSM_NO,a.START_DATE AS A_START_DATE,a.INVOICE_DETAIL_CODE AS
A_INVOICE_DETAIL_CODE,a.VALUE_VAT_EXCLUDED AS A_VALUE_VAT_EXCLUDED,
b.GSM_NO AS B_GSM_NO,b.START_DATE AS
B_START_DATE,b.INVOICE_DETAIL_CODE AS
B_INVOICE_DETAIL_CODE,b.VALUE_VAT_EXCLUDED AS B_VALUE_VAT_EXCLUDED
FROM INVOICEPRINT_DETAIL_LEGACY a, INVOICEPRINT_DETAIL_GENESIS b WHERE
a.GSM_NO=b.GSM_NO AND a.START_DATE=b.START_DATE AND
a.INVOICE_DETAIL_CODE=b.INVOICE_DETAIL_CODE AND a.INVOICE_DETAIL_CODE
IN ( 'DT-ASUTOP' , 'DT-AVMAH4' , 'DT-BAGTOP' , 'DT-BNDTOP' ,
'DT-DIGTOP' , 'DT-FATTUT ' , 'DT-GORGPRF' )");
Yes, I printed the String Parameter and the content is exactly the same, although the output is diferent.
I made some tests and when the last part of the string has only:
IN ( 'DT-ASUTOP' , 'DT-AVMAH4' , 'DT-BAGTOP' , 'DT-BNDTOP' ,
'DT-DIGTOP')");
It works correctly passing the parameter or typing the Query manually. In this case it returns 4 rows, because in the field INVOICE_DETAIL_CODE 4 lines with DT-DIGTOP are found. Perfect and correct.
When I add DT-GORGPRF which is another value I'm looking for in the Column INVOICE_DETAIL_CODE, and it exists but only in one line, the result of the query with:
IN ( 'DT-ASUTOP' , 'DT-AVMAH4' , 'DT-BAGTOP' , 'DT-BNDTOP' ,
'DT-DIGTOP' , 'DT-FATTUT ' , 'DT-GORGPRF' )");
when I pass the String Parameter returns only 1 row, the row with DT-GORGPRF and ignores the last 4 rows. When I type the query manually to the executeQuery method i get the correct result.
This makes no sense. Can I get an input about how to workaround it?
Well after quite some days in top of this issue we found out that the dynamic query we were building through a StringBuilder had some invalid characters that caused this. To workaround we converted the Query to Ascii characters and remove all 13 (/n) that existed in ascii and shouldnt be there. We noticed that in some cases when using comas (,) the StringBuilder was adding /n characters that caused this awkard behaviour..!! Solved.
In java code Remove ( ' ) and test .
IN ( DT-ASUTOP , DT-AVMAH4 , DT-BAGTOP , DT-BNDTOP , DT-DIGTOP , DT-FATTUT , DT-GORGPRF )");

Categories

Resources