How to bind dynamic variable inside pyspark selectExpr? - pyspark

I am trying to bind dynamic variable in PySpark selectExpr.
Code:
name = "random_string"
df.selectExpr("variable_name as $name")
And this throws error.
Thanks.

Use standard string formatting:
df.selectExpr(f"variable_name as {name}")
in Python 3.6 or later, or
df.selectExpr("variable_name as {}".format(name))
before.

I tried
df.selectExpr(f"variable_name as " + name)
and it is working.

Related

Can't convert string to lower case in Ruta

I have a functioning RUTA script. All I want to do is convert a string variable to lowercase doing this ASSIGN(s1, toLowerCase(s2)) where both s1 and s2 are strings. My script works when I do this ASSIGN(s1,s2) but causes an error when I add toLowerCase to my script. The error I get is not very helpful.
2021-08-28 11:27:39 ERROR AnnotateFileHandler:67 - org.apache.uima.resource.ResourceInitializationException: Initialization of annotator class "org.apache.uima.ruta.engine.RutaEngine" failed. (Descriptor: )
I found an answer posted by Peter.
here
I had to change the way I was configuring my Ruta engine to import the string functions, like this:
createEngineDescription(RutaEngine.class,
RutaEngine.PARAM_MAIN_SCRIPT, "system8.annotator.system8",
RutaEngine.PARAM_ADDITIONAL_EXTENSIONS,
new String[]{
BooleanOperationsExtension.class.getName(),
StringOperationsExtension.class.getName()})
Thank goodness for Peter Kluegl

How to correct use $X in jasper??. Problem with parameters and IN clause in JSS

I have a report made with Jaspersoft Studio and in the dataset query I need to use an IN clause, for that I am using the expression "$X{IN ..."
Question # 1: What is the correct type to use for the parameter?
I'm using the following format:
Question # 2: How do I test in the preview?
Parameters screen:
To help, follow the excerpt of where with the parameter being used:
"...Where (($X{IN, db.empresa, paramIdEmpresa}) OR $ P!{ParamIdEmpresa} IS NULL) and (db_view ... "
Error that appears in the preview with the above parameters:
net.sf.jasperreports.engine.JRException: Error executing SQL statement
for: unit1. at com.jaspersoft.studio.editor.preview.view.control.ReportController.fillReport
(ReportController.java:551) at com.jaspersoft.studio.editor.preview.view.control.ReportController.access
(BaseFillHandle.java:135) at java.lang.Thread.run (Thread.java:748)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at
or near "["   Position: 199 at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse
(QueryExecutorImpl.java:2440) at
org.postgresql.core.v3.QueryExecutorImpl.processResults
the problem was in the query and not in jasper...
the correct query is:
where
(($X{IN,db.idempresa, paramIdEmpresa}) OR db.idempresa IS NULL )
thanks to GP...
The best possible way to solve it is simple:
create a parameter called $P!{paramWhere}. of your application
pass the complete where clause with this parameter, e.g. "id = 200"
In your query inside jasper, you put it like this: "... select * from <anywhere> where $P!{paramWhere} ..." and it will magically work.

Find all names in a mongoDB containing a given substring

Hi there I am using scala and play2-reactivemongo version 0.16.2-play26.
I want to be able to do a search query on a json collection where all names will be returned that contain a given substring. I have come across using $text with indexes but I am not sure how to set this up in the version of reactivemongo I am using. Is $text supposed to be declared in my code?
Does any have an example that is written in Scala?
Many thanks
Thank you #cchantep :)
I managed to solve this problem with this code:
def searchItem(name: String): Future[List[Item]] =
jsonCollectionFuture.flatMap(
_.find(Json.obj("name" -> Json.obj("$regex" -> (".*" + name + ".*"))), None)
.cursor[Item](ReadPreference.Primary)
.collect[List](-1, Cursor.FailOnError[List[Item]]())
)
In case you want a BSONDocument, please use the below code snippet:
val query = BSONDocument("columnName" -> "tally")
collection.find(query,Option.empty[BSONDocument]).cursor[BSONDocument]().collect[List](-1,Cursor.FailOnError[List[BSONDocument]]())

why Jedis can not 'get' out CHINESE character which set in by redis-cli.exe?

I used win-redis-server-2.6 to store some simple key-value pairs,
redis-cli.exe to set in key-values, and then get the value out through Jedis.
First,
redis-cli > set foo "中"
the responde is ok
Then,
Using Jedis
JedisPoolConfig poolConfig = new JedisPoolConfig();
jPool = new JedisPool(poolConfig, host, port);
Jedis jedis = jPool.getResource();
String test= jedis.get("foo");
System.out.println(test);
BUT, i got this
��
I have searched the problem, but i got no help.
Redis - problem with accents (UTF-8 encoding)
How to save and retrieve string with accents in redis?
these two ways can not solve my problem, because i must use the jedis to get out the value.
I have tried
redis-cli --raw get foo
this can get out the value, but when i remove the --raw option , the redis-cli.exe just crashed.
And for set values i have tried using hiredis-C-client to set value, but also i got the problem.
BUT when i set value through Jedis and get value also using Jedis ,It is OK.
Also i have tried to decode the value get out by jedis,using the following code
String newStr = new String(test.getBytes("UTF-8"), "UTF-8");
I have tried "GBK", "GB2312", "ISO-8859-1" , but all failed.
Could anyone please help me?
Thanks in advance.
Kong
At last, We use another way to implement this function.
That is we encode any chinese values we want to set in redis by UTF8 . After this , i can get the right string out by Jedis.

Unable to use Variable in XPath in Eclipse - Selenium

I need to pass values from excel sheet ( stored in variable api ) to XPATH in eclipse (java - Selenium).
I tried several options but none works. Please guide.
Here is my line of code.
String appcode = //input[contains(#id,'app') and contains(#type,'text') and ancestor::div[contains(#id, '+api+')]]
When i hardcode the value of api as below it works.
String appcode="//input[contains(#id,'app') and contains(#type,'text') and ancestor::div[contains(#id, 'setmember')]]";
Isnt it this easy?
Appreciate your help
pk
you probably did not end the String constructor properly. Try this:
String appcode = "//input[contains(#id,'app') and contains(#type,'text') and ancestor::div[contains(#id, '" +api+" ')]]";
My assumption is, that api variable is type String
you can use \" so you would have something like
String appcode ="//input[contains(#id,\" "+[VARIABLE]+ " \")[..] ";