# How to handle empty result in jdbc\_streaming filter plugin in Logstash

**URL:** <https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347>\
**Category:** Logstash\
**Created:** [August 25, 2020, 6:56pm UTC](https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347 "2020-08-25T18:56:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![naveen007](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@naveen007](https://discuss.elastic.co/u/naveen007)\
**Post date:** [August 25, 2020, 6:56pm UTC](https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347/1 "2020-08-25T18:56:32Z")

</div>

Hi,  
I'm using the _jdbc\_streaming_ filter plugin to get data from query. But in some case query return empty result or null value.

```auto
parameters => { "firstName" => "firstName" }              
statement => "SELECT DISTINCT TOP 1 userid FROM employee where first_name=:firstName "                    
target => "user"

```

Below is the query result if data is not empty.

```auto
 "user" => [
        [0] {
            "userid" => 1240932
        }

```

Then I will get it by using below code  
` add_field => { "userId" => "%{[user][0][userid]}" } `  
But in case empty or null result Logstash through exception. Below is the code

```auto
tags" => [
        [0] "_jdbcstreamingdefaultsused"
    ],

```

I want to set empty if Target object (user) is empty or null. I have tried below code but it is not working.

```auto
 if [user][0]{
              mutate{
                   add_field => { "userId" => "%{[user][0][userid]}" }       
                   remove_field => ["user"]
                  }
          }else{
            mutate {
                   
                   add_field => { "userId" => "" } 
                   remove_field => ["user"]

             }
          }

```

Can anybody tell me how to handle this?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 25, 2020, 7:06pm UTC](https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347/2 "2020-08-25T19:06:27Z")

</div>

Does setting

```
default_hash => { "userid" => "" }

```

help? You should be able to do the mutate unconditionally then.

---

<div class="post-metadata">

**Author:** ![naveen007](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@naveen007](https://discuss.elastic.co/u/naveen007)\
**Post date:** [August 25, 2020, 7:19pm UTC](https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347/4 "2020-08-25T19:19:01Z")

</div>

I am not getting your point. Where should I write this code and what about condition ?  
I want to check user is empty or not. Below is the output when query return empty

```auto
 "user" => [
        [0] {}
]

```

Please let me know if you have any queries.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 25, 2020, 8:06pm UTC](https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347/5 "2020-08-25T20:06:46Z")

</div>

If you set the default\_hash option on the jdbc\_streaming filter then if no rows match it will return that default hash in the target array. So the array will always exist and you can do your

```
mutate{
    add_field => { "userId" => "%{[user][0][userid]}" }       
    remove_field => ["user"]
}

```

unconditionally.

---

<div class="post-metadata">

**Author:** ![naveen007](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@naveen007](https://discuss.elastic.co/u/naveen007)\
**Post date:** [August 26, 2020, 7:42am UTC](https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347/6 "2020-08-26T07:42:36Z")

</div>

Got it. Thanks, it's working for me.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 23, 2020, 7:42am UTC](https://discuss.elastic.co/t/how-to-handle-empty-result-in-jdbc-streaming-filter-plugin-in-logstash/246347/7 "2020-09-23T07:42:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
