Monday, December 2, 2019

spring annotations i never had the chance to use part 2: @ConfigurationProperties

Few days ago, I accidentally stumbled upon a Spring annotation from Spring Boot project while I was checking something else.

We all know how to bind property values with "@Value" to the classes and we all know that this can be quite cumbersome if there are multiple properties to bind. Spring Boot is here to help. You can use "@ConfigurationProperties" and bind multiple values quite concisely. We will give a prefix to differentiate other configs from ours. e.g. "@ConfigurationProperties(prefix = "jdbc")".
Any field this annotated class has is populated with property values from the property resource. For instance if it has a username parameter then property resource with "jdbc.username" key will populate this field. The most practical way of using this annotation is using it with "@Configuration".


You can check how we create the config class.
package com.sezinkarli.tryconfigprops;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "jdbc")
public class JdbcConfig
{
    private String user;
    private String password;
    private String url;
    private String driver;

    public String getUser()
    {
        return user;
    }

    public void setUser(String user)
    {
        this.user = user;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public String getDriver()
    {
        return driver;
    }

    public void setDriver(String driver)
    {
        this.driver = driver;
    }

    public String getProperty(String key)
    {
        return propertyMap.get(key);
    }
}

And below you can check the properties we map from application properties
jdbc.user=myJdbcUser
jdbc.password=myPwd
jdbc.url=myUrl
jdbc.driver=myJdbcDriver
After that you can easily get these values by injecting the configuration class to somewhere.
@Service
public class YourService
{

    @Autowired
    private JdbcConfig jdbcConfig;
}
You can also check here for a working toy project using "@ConfigurationProperties".

Wednesday, October 23, 2019

benchmark for new string methods of java 11

While I was checking what's new in Java 11, I saw that there are several new methods for String class. So I wanted to do a microbenchmark with old way of doing things and by using new methods. These new methods are;

boolean isBlank()

String strip()

Stream lines()


isBlank() is tested agains trim().isEmpty(), strip() is tested agains trim() and lines() is tested agains split().

Here are the results:

Benchmark Score
lines 3252919
split 2486539
strip 18280130
trim 18222362
isBlank 25126454
trim + isEmpty 19854156

Scores are based on operations per second so the more the better.
As you can see lines() is much faster than split().
strip() and trim() performed quite similar.
isBlank() outperformed trim() + empty().

You can check the benchmark code here.

Tuesday, February 12, 2019

cool elasticsearch client

I was searching for an elastic client so I can have auto-complete for queries and a graphical way of seeing how things are going for our test elastic servers. Few years ago we had Sense plug-in which I think now a part of Kibana. After a little googling, I found head plug-in for chrome and it is so cool. I highly recommend it to everyone with this kind of need.

Friday, February 8, 2019

simple introduction to CAP theorem

Yesterday I discovered a beautifully written introduction to CAP theorem. Thanks to Kaushik Sathupadi for this.

Tuesday, June 19, 2018

machine learning ass-kicking in java part 2

Welcome to the second part of the tutorial for scoring your PMML files using  LightningScorer, which is a side project of mine.

Let's find out how additional parameters work.
The initial steps are similar to the first part of the tutorial.

Get your local copy first
git clone https://github.com/sezinkarli/lightningscorer.git

and build it with maven
mvn clean install

and start it by going to your target folder
java -jar lightningscorer-uberjar-1.0.jar


Now lets make sure our server is up and running by going to
http://localhost:8080/
.
Server returns
{
"data": "I have come here to chew bubblegum and kick ass...",
"success": true
}

Ok then we are now ready to kick ass, again.

I'll use apache commons' http get/post methods. First, we'll deploy our machine learning model with an additional parameter. Then we will check if it's working and then use our input values and score it.  After the scoring we will use our additional parameter.


    
        final String url = "http://localhost:8080/model/";
        final String modelId = "test2";

        //http://dmg.org/pmml/pmml_examples/knime_pmml_examples/ElNinoPolReg.xml
        File pmmlFile = new File("/tmp/ElNinoPolReg.xml");

        CloseableHttpClient client = HttpClients.createDefault();

        // deployment
        // notice that I give a variance value as an additional parameter that I will use later
        HttpPost deployPost = new HttpPost(url + modelId + "?variance=3.25");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("model", new File(pmmlFile.getAbsolutePath()), ContentType.APPLICATION_OCTET_STREAM, "model");
        HttpEntity multipart = builder.build();
        deployPost.setEntity(multipart);

        CloseableHttpResponse response = client.execute(deployPost);
        String deployResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
        System.out.println(deployResponse);
        // {"data":true,"success":true}
        deployPost.releaseConnection();

        // check deployed model
        HttpGet httpGet = new HttpGet(url + "ids");

        response = client.execute(httpGet);
        String getAllModelsResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
        System.out.println(getAllModelsResponse);
        // {"data":["test1"],"success":true}
        httpGet.releaseConnection();

        //score deployed model
        HttpPost scorePost = new HttpPost(url + modelId + "/score");
        StringEntity params = new StringEntity("{" +
                "\"fields\":" +
                "{\"latitude\":2.5," +
                "\"longitude\":11.4," +
                "\"zon_winds\":3.5," +
                "\"mer_winds\":3," +
                "\"humidity\":31.2," +
                "\"s_s_temp\":25.21" +
                "}" +
                "} ");
        scorePost.addHeader("content-type", "application/json");
        scorePost.setEntity(params);

        CloseableHttpResponse response2 = client.execute(scorePost);
        String scoreResponse = IOUtils.toString(response2.getEntity().getContent(), Charset.forName("UTF-8"));
        System.out.println(scoreResponse);
        // {"data":{"result":{"airtemp":29.788226026392735}},"success":true}
        scorePost.releaseConnection();


        HttpGet additionalParamGet = new HttpGet(url + modelId + "/additional");
        CloseableHttpResponse response3 = client.execute(additionalParamGet);
        String additionalParamResponse = IOUtils.toString(response3.getEntity().getContent(), Charset.forName("UTF-8"));
        System.out.println(additionalParamResponse);
        // {"data":{"variance":"3.25"},"success":true}
        additionalParamGet.releaseConnection();


        // Then you can use the variance value with your result in airtemp to calculate an interval for your score


        client.close();