how to replace string in SpEL expression? - spring

firstly,thanks for attention
i defined ftp adapter in my spring integration project and used mv command to move files in ftp server,directory structure is:
ftp-root
-----------Directory1\
-----------------in\
---------------------------file.in
-----------------out\
i want to move file file.in in ftp-root\Directory1\in\ directory to move ftp-root\Directory1\out\ with .out.rpt extension ftp-root\Directory1\out\a.out
i used int-ftp:outbound-gateway adapter to run mv command on ftp server,my code is:
<int-ftp:outbound-gateway id="gatewayMv"
session-factory="ftpSessionFactory"
expression="payload.remoteDirectory + '/' + payload.filename"
request-channel="mvChannel"
command="mv"
rename-expression="payload.remoteDirectory + '/' + payload.filename "
reply-channel="aggregateResultsChannel"/>
how to use SpEL expression to replace in with out in rename-expression option?

rename-expression="payload.remoteDirectory + '/' + payload.filename.replaceFirst('in', 'out')"
In most cases SpEL work like the regular Java. Since filename is a String you can apply for it any string operation.

thanks for #Artem Bilan ane #Gary for answer
the other way to working with string
define a bean as bellow :
public class StringUtil {
public String replacement(String value,String var1,String var2) {
return value.replace(var1,var2);
}
}
and in expression option set to:
rename-expression="#stringUtil.replacement(payload.remoteDirectory + '/' + payload.filename,'in','out')"

Related

how to pass a dynamic file path to filename field of Flexible File Writer of jmeter

I need to generate the dynamic file path in the setup thread group like below.
def result_file = new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + File.separator + 'transactions_passed_' + new Date().format('MM_dd_yyyy_HH_mm_ss') + '.csv');
props.put("result_file", result_file);
Now I want to pass that file path as a filename value of Flexible File Writer plugin of jmeter so that variables are stored inside it.
Not able to make it work. Kindly help. Thanks
I have tried below options:
Filename: ${__groovy(props.get("result_file").text)}
tried to use preprocessor and set the value:
vars.put("result_file", '${__FileToString(props.get("result_file"),,)}');
Also tried to use below groovy script in the FileName field of Flexible File Writer, however it throws an exception of FileNotFound exception:
${__groovy(new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + System.getProperty('file.separator') + 'transactions_passed_' + new Date().format('MM_dd_yyyy_HH_mm_ss') + '.csv').text)}
I want to use DYNAMIC FILE PATH (which I am setting as property in setup thread group) in the FILENAME field of FLEXIBLE FILE WRITER
The approach with __groovy() function should work however you need to remove this .text bit from there
${__groovy(new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + System.getProperty('file.separator') + 'transactions_passed_' + new Date().format('MM_dd_yyyy_HH_mm_ss') + '.csv'))}
because .text returns you the file contents and as the file doesn't exist - you're getting this FileNotFound error. More information on Groovy scripting in JMeter - Apache Groovy: What Is Groovy Used For?

ML Gradle task.Server.Eval.Task setting variables using xquery

I'm using ml-gradle to run a block of XQuery to update the MarkLogic database. The problem I am running into is I need to wrap all of the code in quotes, but since the code itself has quotes in it I am running into some errors when I try to declare variables i.e. let $config. Does anyone know a way around this? I was thinking I could concatenate all of the code into one big string so it ignores the first and last quotation.
task addCron(type: com.marklogic.gradle.task.ServerEvalTask) {
xquery = "xquery version \"1.0-ml\";\n" +
"import module namespace admin = \"http://marklogic.com/xdmp/admin\" at \"/MarkLogic/admin.xqy\";\n" +
"declare namespace group = \"http://marklogic.com/xdmp/group\";\n" +
" let $config := admin:get-configuration()\n" +
It bombs out when it is trying to declare $config as a variable. With the error:
> Could not get unknown property 'config' for task ':
Here is an example that works
task setSchemasPermissions(type: com.marklogic.gradle.task.ServerEvalTask) {
doFirst {
println "Changing permissions in " + mlAppConfig.schemasDatabaseName + " for:"
}
xquery = "xdmp:invoke('/admin/fix-permissions.xqy', (), map:entry('database', xdmp:database('" + mlAppConfig.schemasDatabaseName + "')))"
}
Here is some documentation for ServerEvalTask: https://github.com/marklogic-community/ml-gradle/wiki/Writing-your-own-task
I suspect you are hitting some string template mechanism in Groovy/Gradle. Try escaping the $ sign as well.
Note that you can use both single and double quotes in XQuery code.
HTH!

Error in Apache Spark called input path does not exist

Are there any algorithms in Apache Spark to find out the frequent patterns in a text file. I tried following example but always end up with this error:
org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file:
/D:/spark-1.3.1-bin-hadoop2.6/bin/data/mllib/sample_fpgrowth.txt
Can anyone help me solve this problem?
import org.apache.spark.mllib.fpm.FPGrowth
val transactions = sc.textFile("...").map(_.split(" ")).cache()
val model = new FPGrowth()
model.setMinSupport(0.5)
model.setNumPartitions(10)
model.run(transactions)
model.freqItemsets.collect().foreach {
itemset => println(itemset.items.mkString("[", ",", "]") + ", " + itemset.freq)
}
try this
file://D:/spark-1.3.1-bin-hadoop2.6/bin/data/mllib/sample_fpgrowth.txt
or
D:/spark-1.3.1-bin-hadoop2.6/bin/data/mllib/sample_fpgrowth.txt
if not work, replace / with //
I assume you are running spark on windows.
Use file path like
D:\spark-1.3.1-bin-hadoop2.6\bin\data\mllib\sample_fpgrowth.txt
NOTE : Escape "\" if necessary .

Gradle: using a directory variable

In gradle I want to use a directory variable, what in ant would be ${mydir}.
def mydir = new File("mydir")
copy {
from 'example.txt'
into '${mydir}/out/'
}
How to do this? The docs don't contain information on this issue AFAICS, http://gradle.org/docs/current/userguide/working_with_files.html
Try:
into new File(mydir, out)
or if with GString
into "${mydir}/out/"
(note the double " quotes)

h2 with custom java alias and javac compiler issues in multi process environment

H2 database with custom function alias defined as:
create alias to_date as $$
java.util.Date toDate(java.lang.String dateString, java.lang.String pattern) {
try {
return new java.text.SimpleDateFormat(javaPattern).parse(dateString);
} catch(java.text.ParseException e) {
throw new java.lang.RuntimeException(e);
}
}
$$;
H2 initialized as:
jdbc:h2:mem:testdb;INIT=runscript from 'classpath:create_alias.sql
This is used in tests, executed for multiple projects concurrently on a Jenkins instance. Sometimes such tests would fail with following error:
Could not get JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "javac: file not found: org/h2/dynamic/TO_DATE.java
Usage: javac <options> <source files>
use -help for a list of possible options
"; SQL statement:
create alias to_date as $$
java.util.Date toDate(java.lang.String dateString, java.lang.String pattern) {
....
My guess is that org.h2.util.SourceCompiler is assuming that there is only one instance of h2 running at the time and writes the generated Java source to 'java.io.tmpdir', which is shared among all processes running under same account. I propose following fix:
Index: SourceCompiler.java
===================================================================
--- SourceCompiler.java (revision 5086)
+++ SourceCompiler.java (working copy)
## -40,7 +40,15 ##
*/
final HashMap<String, Class<?>> compiled = New.hashMap();
- private final String compileDir = Utils.getProperty("java.io.tmpdir", ".");
+ private final String compileDir;
+
+ {
+ // use random folder under java.io.tmpdir so multiple h2 could compile at the same time
+ // without overwriting each other files
+ File tmp = File.createTempFile("h2tmp", ".tmp");
+ tmp.mkdir();
+ compileDir = tmp.getAbsolutePath();
+ }
static {
Class<?> clazz;
Should I open the support ticket or there are workarounds for this issue?
You can use javax.tools.JavaCompiler API and provide your own implementation for in-memory JavaFileManager to completely avoid creating those temp files.
BTW, Janino also support javax.tools.JavaCompiler API.
I had the same problem running multiple Jenkins executors and having Arquillian/Wildfly/H2 integration tests configuration. I found a workaround by setting java.io.tmpdir property to the build directory in the test standalone.xml.

Resources