How to make spring #ManagedOperationParameter work with scala - java

In my scala code this works fine:
import org.springframework.jmx.export.annotation.{ManagedOperationParameters, ManagedResource, ManagedOperation, ManagedOperationParameter}
#Override #ManagedOperation(description = "somedesk")
def getStatsAsStr: String = "blabla"
but as soon as i add #ManagedOperationParameters I get illegal start of simple expression for #ManagedOperationParameter( although I do import it.
so while in java this compiles fine:
#Override #ManagedOperation(description = "some description")
#ManagedOperationParameters({#ManagedOperationParameter(name = "myname", description = "myname")
})
In scala does not compile:
import org.springframework.jmx.export.annotation.{ManagedOperationParameters, ManagedResource, ManagedOperation, ManagedOperationParameter}
#Override #ManagedOperation(description = "some description")
#ManagedOperationParameters(Array(#ManagedOperationParameter(name = "myname", description = "mydesc")) // PRODUCES 'illegal start of simple expression for #ManagedOperationParameter('
def getStatsAsStr(myname: String): String = "blabla"
is there a way for it to work? if i create it as a .java with java syntax in same project all is fine (which means my depenedncies are fine) i think its something with scala syntax i don't get what is it?

Inner annotation values have to be constructed with a different syntax. This should work (whitespace added for clarity, not relevant); if not, try replacing the named parameters with positional.
#ManagedOperationParameters(
Array(
new ManagedOperationParameter(name="myname", description="mydesc")
))

Related

Format a string w/ Java MaskFormatter in Databricks/Scala

I create a function based on Java MaskFormatter function in Databricks/Scala.
But when I call it from sparksql, I received error message
Error in SQL statement: AnalysisException: Undefined function:
formatAccount. This function is neither a built-in/temporary function,
nor a persistent function that is qualified as
spark_catalog.default.formataccount.; line 1 pos 32
Here is my function
import javax.swing.text.MaskFormatter
def formatAccount(account: String, mask:String ) : String =
{
val formatter = new MaskFormatter(mask.replace("X", "A"))
formatter.setValueContainsLiteralCharacters(false)
val formatAccount = formatter.valueToString(account)
formatAccount
}
Here is the query code which received the error message
sql("""select java_method(emitToKafka ,formatAccount("1222233334", "X-XXXX-XXXX-X"))""")
However if I run below code, it works fine.
formatAccount("1222233334", "X-XXXX-XXXX-X")
res0: String = 1-2222-3333-4
what could be missed?

Updates with a script ElasticSearch API not accepting Scala Map

I came across a use-case where I need to update a specific field in the ElasticSearch document. So for this use-case, I have used the Update API with a script ES doc. But I faced an issue(compilation error) with Script Constructor which accepts the following parameters:--> type, lang, idOrCode and params and the issue was with params(java.util.Map) parameter.
I have even tried the Scala to Java converters but could not solve it.
Code snippet
import org.elasticsearch.action.update.UpdateRequest
import org.elasticsearch.client.RequestOptions
import org.elasticsearch.script.{Script, ScriptType}
object Testing extends App {
val result = updateByScript("testing", "hW7BBnQBn2nWmIjS_b0C", 10.0)
println("######result:---> " + result)
high_level_client.close()
def updateByScript(index: String, id: String, count: Double) = {
//import scala.collection.JavaConversions.mapAsJavaMap
//import collection.JavaConverters._
import scala.collection.JavaConverters._
val updateRequest = new UpdateRequest(index, id)
val params = Map[String, Double]("count" -> count)
val script = new Script(ScriptType.INLINE, "painless", "ctx._source.count += params.count", mapAsJavaMap(params))
updateRequest.script(script)
high_level_client.update(updateRequest, RequestOptions.DEFAULT)
}
}
For the above issue, I have tried the Script Constructor with the idOrCode parameter and which solved my use-case but still I did not get the solution for other Script Constructors.
Working code with Constructor which accept idOrCode parameter.
Code snippet
import org.elasticsearch.action.update.UpdateRequest
import org.elasticsearch.client.RequestOptions
import org.elasticsearch.script.{Script, ScriptType}
object Testing extends App {
val result = updateByScript("testing", "hW7BBnQBn2nWmIjS_b0C", 10.0)
println("######result:---> " + result)
high_level_client.close()
def updateByScript(index: String, id: String, count: Double) = {
val updateRequest = new UpdateRequest(index, id)
val script = new Script(s"""ctx._source.count += $count""")
updateRequest.script(script)
high_level_client.update(updateRequest, RequestOptions.DEFAULT)
}
}

The type of options(Object...) is erroneous, Telerivet API

I would like to test Telerivet API and have the following code:
TelerivetAPI tr = new TelerivetAPI(YOUR_API_KEY);
Project project = tr.initProjectById(project_id);
Message sent_msg = project.sendMessage(Util.options(
"content", "hello world",
"to_number", "+16505550123"
));
The error I am getting is on .options(..), it says The type of options(Object...) is erroneous. How to solve that? thanks.

How to convert a string to camel case in phpstorms (velocity based) file tempates?

What I've tried so far is:
## $NAME is something like 'my_controller_c'
#set($NAME = $NAME.removeAndHump($NAME))
#set($NAME = $NAME.underscoresToCamelCase(String)
But that does not work. The first one does nothing, the second one throws an java error.
I also tried using regular expressions and to loop through the string, but my java knowledge is very basic.
The following works in PhpStorm 9 (and probably all of the other JetBrains IDEs, I would guess):
#set($new_name = ${StringUtils.removeAndHump(${NAME}, "-")})
class $new_name {
}
This is, what I ended up doing:
#set($ctrlName = $NAME.replaceAll("-c$", ""))
#set($ctrlNewName = "")
#foreach($str in $ctrlName.split("-"))
#set($str = $str.substring(0,1).toUpperCase()+$str.substring(1))
#set($ctrlNewName = $ctrlNewName + $str)
#end
#set ( $ctrlNewName = $ctrlNewName + "Ctrl" )

Ingest(Update hash Code) in echoprint servers using JAVA

I am developing an android application using JAVA. All I want is to
record a song and generate its hash(CODE), then query the echoprint server for a match.
If a match is not found, then upload it to the server (ingest) for future references.
I have been able to achieve the first part. Can someone suggest me about the second part in JAVA? (P.S. : I've seen how to do it using python codes - but that won't be helpful in my case.)
Another question, may I achieve the second objective with the global echoprint server? Or, do I need to set up one of my own?
The references I've used are:
http://masl.cis.gvsu.edu/2012/01/25/android-echoprint/
https://github.com/gvsumasl/EchoprintForAndroid
To insert a song into the echoprint server database, all you need to do is call the ingest method. Basically, it is only a HTTP POST request with correct json body. Here is a Scala code (Java would be very similar) that I am using for that:
import EasyJSON.JSON
import EasyJSON.ScalaJSON
import dispatch.Defaults.executor
import dispatch._
class EchoprintAPI {
val API_URL = "http://your.api.server"
def queryURL(code: String) = url(s"$API_URL/query?fp_code=$code")
def query(code: String): scala.concurrent.Future[ScalaJSON] = {
jsonResponse(queryURL(code))
}
def ingest(json: ScalaJSON, trackId: String): scala.concurrent.Future[ScalaJSON] = {
val metadata = json("metadata")
val request = url(s"$API_URL/ingest").POST
.addParameter("fp_code", json("code").toString)
.addParameter("artist", metadata("artist").toString)
.addParameter("release", metadata("release").toString)
.addParameter("track", metadata("title").toString)
.addParameter("codever", metadata("version").toString)
.addParameter("length", metadata("duration").toString)
.addParameter("genre", metadata("genre").toString)
.addParameter("bitrate", metadata("bitrate").toString)
.addParameter("source", metadata("filename").toString)
.addParameter("track_id", trackId)
.addParameter("sample_rate", metadata("sample_rate").toString)
jsonResponse(request)
}
def delete(trackId: String): scala.concurrent.Future[ScalaJSON] = {
jsonResponse(url(s"$API_URL/query?track_id=$trackId").DELETE)
}
protected def jsonResponse(request: dispatch.Req): scala.concurrent.Future[EasyJSON.ScalaJSON] = {
val response = Http(request OK as.String)
for (c <- response) yield JSON.parseJSON(c)
}
}
To generate the fingerprint code, you can use echoprint-codegen command line call or use the Java JNI integration with C lib

Categories

Resources