A rest service example with spring-boot, spring-jdbc and scala

I like spring-boot. I think it will be the future of java framework. Only a few files, you can setup a java micro service. You can even write all codes in groovy or scala.

This project is an example to use scala with spring-boot to provide a simple rest service. Comparing to java version, following issues need to be solved.

  1. Spring’s default message resolver cannot handle scala class. Need to register a new one.
  2. When invoking java code from scala, need to do some convertion from scala’s AnyVal to their relavant java object type and scala’s collection to java’s when passing parameters to java code.

Github repository is Here

This was posted 4 days ago. It has 0 notes and 0 comments.
This was posted 1 week ago. It has 0 notes and 0 comments. .
This was posted 1 week ago. It has 0 notes and 0 comments. .

For loop in scala

for in scala is more powerful than for in java. The basic usage is the same with java.

val names = List("Sting", "Ben", "Georte", "Jacky")
for (name <- names) {
  println(name)
}

You can have more control over the loop.

val names = List("Sting", "Ben", "George", "Jacky")
for (name <- names; if name.length > 3) {
  println(name)
}

You may even iterate more than one list.

for (i <- 1 to 10; j <- 11 to 20) {
  println(s"$i * $j")
}

Or

for (i <- 1 to 10; if i % 2 == 0; j <- 11 to 20; if j % 2 == 1) {
  println(s"$1 * $j")
}

If you are tied of \;\, it is also legal to use {} to replace ()

for {i <- 1 to 10
  if i % 2 == 0
  j <- 11 to 20
  if j % 2 == 1} {
  println(s"$i * $j")
}

Just like other expression in scala, for loop can have return value.

val names = List("Sting", "Ben", "George", "Jacky")
val filtered = for (name <- names; if name.length > 3) yield name

You may say map and filter can do the same thing, why should I still use for loop?

The answer is if you have more than one generator, for loop is more clear.

val numbers = for (i <- 1 to 10; if i % 2 == 0; j <- 11 to 20; if j % 2 == 1; if i * j < 200) yield (i * j)

for can also iterate map. Each element in the for loop is a (k, v) tuple.

val m = Map("sting" -> 30, "ben" -> 24, "george" -> 30)
for ((k, v) <- m) {
  println(k, v)
}
This was posted 1 week ago. It has 0 notes and 0 comments.

Postgresql is great

Target: study postgresql’s feature and how to use them effective on PDMS’ dynamic form’s save and search.

Table schema is as following:

id         | integer           | not null default nextval('data_id_seq'::regclass) | plain    |              |
patient_id | integer           |                                                   | plain    |              |
item_id    | integer           |                                                   | plain    |              |
data_type  | integer           |                                                   | plain    |              |
value      | character varying |

Column value is used to save the value of dynamic form which has 5 types. They are string, integer, float, date and options. I use varchar here and want to convert it to the actual data type when doing query.

I added other three partial indexes base on different data types. Thanks to postgresql’s partial index.

"data_value_dict_index" btree ((value::integer)) WHERE data_type = 3
"data_value_integer_index" btree ((value::integer)) WHERE data_type = 2
"data_value_text_index" btree (value) WHERE data_type = 1

Then I inserted about 10000000 records into the table and run following SQL.

select distinct patient_id from data where data_type = 2 and item_id = 2 and value::int > 500 limit 20;
=> 2.2s
(select distinct patient_id from data where data_type = 2 and item_id = 2 and value::int > 700) intersect (select patient_id from data where data_type = 1 and item_id = 9 and value = '99');
=> 2.0s

The result is not bad. More importantly, postgresql has intersect which is very useful for PDMS. Its partial index is also great. Although I save value as string and convert it to other type in query, the indexes still work.

Then I adjusted the indexed by adding item_id column, the result turned out to be amazing.

select distinct patient_id from data where data_type = 2 and item_id = 2 and value::int > 500 limit 20
=> 118.640 ms
(select distinct patient_id from data where data_type = 2 and item_id = 2 and value::int > 700) intersect (select patient_id from data where data_type = 1 and item_id = 9 and value = '99')
=> 143 ms

The indexes is now:

"data_value_dict_index" btree (item_id, (value::integer)) WHERE data_type = 3
"data_value_integer_index" btree (item_id, (value::integer)) WHERE data_type = 2
"data_value_text_index" btree (item_id, value) WHERE data_type = 1
This was posted 1 week ago. It has 1 note and 0 comments.
This was posted 2 weeks ago. It has 0 notes and 0 comments. .
This was posted 2 weeks ago. It has 0 notes and 0 comments. .
This was posted 2 weeks ago. It has 0 notes and 0 comments. .
This was posted 1 month ago. It has 0 notes and 0 comments. .
This was posted 1 month ago. It has 0 notes and 0 comments. .

(via squattofit)

This was posted 1 month ago. It has 6,539 notes and 0 comments. .
This was posted 1 month ago. It has 0 notes and 0 comments. .
乐高进行时

乐高进行时

This was posted 1 month ago. It has 0 notes and 0 comments. .
Spider man reloaded

Spider man reloaded

This was posted 1 month ago. It has 0 notes and 0 comments. .
Spider man reloaded

Spider man reloaded

This was posted 1 month ago. It has 0 notes and 0 comments. .