Splunk Search

How to merge two services from same index with common field?

arunakalla
Explorer

I wanted to join services (part of same index) with common field and show chosen fields from both searches..

Index=test service=serv1

Name

RecordID

Version

Index=test service=serv2

State

RecordID

Version

wants to combine two searches by RecordID from Service2  (meaning to optimize query needs to first take RecordID from Service2 and match with Service1)... and notice fieldname Version is common both services. so hence wants to rename version field in service2 to version2. 

And final result is

Name Version1 Version2 

SQL Query:

Select A.Name, A.version, B.version

from Service1 A, Service2 B

where B.RecordID = A.RecordID

 

Labels (1)
0 Karma
1 Solution

arunakalla
Explorer

 

    index=test (service=serv1 OR service=serv2)
    | eval Version2 = if(service=="serv2", Version, null())
    | eval Version1 = if(service=="serv1", Version, null())
    | stats values(*) as * by RecordID
    | table Name RecordID Version1 Version2
    | where isnotnull(Version2)

 

the above achieved my expected result.. Thanks you for all your help @yuanliu  

View solution in original post

0 Karma

richgalloway
SplunkTrust
SplunkTrust

It would help to know what you've already tried so we don't suggest the same thing.

Since you appear to know SQL you might find this document helpful: https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/SQLtoSplunk

Here's one way to do it

index=test service=serv1
| join RecordID [ search index=test service=serv2 | rename version as version2 ]
| table Name RecordID version version2

Here's another that doesn't use join.

index=test (service=serv1 OR service=serv2)
```If Name is null then this is a serv2 event so set version2=version```
| eval version2 = if(isnull(Name), version, null())
| stats values(*) as * by RecordID
| table Name RecordID version version2
---
If this reply helps you, Karma would be appreciated.
0 Karma

arunakalla
Explorer

Thanks for the reply first one worked.. Stats did not work.. I am not getting the  version2 values.. pls see my response to other reply below

Docs says join  only works for 50000 records and after some time it terminates.. hence wants needs to make stats work

 

0 Karma

yuanliu
SplunkTrust
SplunkTrust

Just to highlight that join is more expensive than stats.  Meanwhile, the second method (no join) may need to remove original values of version from serv2 if version2 has distinct values from those from serv1.

index=test (service=serv1 OR service=serv2)
| eval version2 = if(service=serv2, version, null())
| eval version = if(service=serv1, version, null())
| stats values(*) as * by RecordID
| table Name RecordID version version2

 

0 Karma

arunakalla
Explorer

that did not work .. can only see Name and RecordID fields... version is populated because it's able to see that name in the original serv1 .. version1 is null. To test the above I modified your reply to following

index=test (service=serv1 OR service=serv2)
| eval version2 = if(service=serv2, version, null())
| eval version1 = if(service=serv1, version, null())
| stats values(*) as * by RecordID
| table Name RecordID version1 version2

 and now both version1 and version2 is empty..

 

also tried this with same results as above 

index=test (service=serv1 OR service=serv2)
| eval version2 = case(service=serv2, version)
| eval version1 = case(service=serv1, version)
| stats values(*) as * by RecordID
| table Name RecordID version1 version2

 

Just to highlight.. End result I want is only values from Serv1, where there is a matching RecordID in the Serv2.. The whole point of this exercise is I want to replace all values of version in the Serv1 with version values from Serv2 where there is a matching RecordID and discard the rest. Serv1 is major data source and Serv2 is minor.

0 Karma

yuanliu
SplunkTrust
SplunkTrust

My bad.  Literal values should be quoted when use in eval.

index=test (service=serv1 OR service=serv2)
| eval version2 = if(service=="serv2", version, null())
| eval version1 = if(service=="serv1", version, null())
| stats values(*) as * by RecordID
| table Name RecordID version1 version2

 

0 Karma

arunakalla
Explorer

This is acting like left join with difference.. so basically keeping all records of the version1 and inserting value of the version2 into the version1 (now I can see old value of version1+version2) , along with version2 value in it's column.  it looks like this

Serv1

NameRecordIDVersion
Name1RecordID1^5
Name2RecordID25.5
Name3RecordID35.7

 

Serv2

PlaceRecordIDVersion
Pl1RecordID15.5
Pl2RecordID2 5.5
Pl3RecordID74.7

 

End Result

NameRecordIDVesrion1Version2
Name1RecordID1

^5

5.5

5.5
Name2RecordID25.55.5
Name3RecordID35.7 
0 Karma

yuanliu
SplunkTrust
SplunkTrust

This is indeed very unexpected.  I suspect that your serv1 data already contain multiple Version values.  Could you examine data with this in the same period you tested the other search:

 

index=test (service=serv1)
| eval Version2 = if(service=="serv2", Version, null())
| eval Version1 = if(service=="serv1", Version, null())
| stats values(*) as * by RecordID
| table Name RecordID Version1 Version2

 

(I notice that you originally posted Version as capitalized, but my sample code used all lowercase "version".  I guess that you have corrected this in your tests.) In this test, there is no data from serv2.  Therefore, I expect this result

NameRecordIDVesrion1Version2
Name1RecordID1

^5

5.5

 
Name2RecordID25.5 
Name3RecordID35.7 
If the above is the case, the search is working as expected.  To replace Version with version2 where RecordID match in serv1 and serv2, just force it.

 

index=test (service=serv1 OR service=serv2)
| eval version2 = if(service=="serv2", Version, null())
| eval Version = if(service=="serv1", Version, null())
| stats values(*) as * by RecordID
| eval Version = if(isnull(version2), Version, version2)

 

 

0 Karma

arunakalla
Explorer

yes other search is fine, except that it's getting throttled/timed out

0 Karma

yuanliu
SplunkTrust
SplunkTrust

I am confused.  Did you confirm that from serv1, Version1 gives the exact multiple values as if searching both serv1 and serv2?

0 Karma

arunakalla
Explorer

My requirement is not to merge Version2 and Version1 and display multiple values in Version1.. Merge two services then display only rows that matches RecordID in Version2. Instead

(1) It is displaying all values from Serv1

(2) Version1 is displaying merged values

0 Karma

yuanliu
SplunkTrust
SplunkTrust

@arunakalla wrote:

My requirement is not to merge Version2 and Version1 and display multiple values in Version1.. Merge two services then display only rows that matches RecordID in Version2. Instead

(1) It is displaying all values from Serv1

(2) Version1 is displaying merged values


(The forum really mixed up response order:-) Sorry I didn't explain clearly.  I suspect that your data contains multiple values, or the output from the search will not be as illustrated in the final results.  Maybe one step at a time.  Can you confirm the output from serv1 alone:

 

index=test (service=serv1)
| eval Version2 = if(service=="serv2", Version, null())
| eval Version1 = if(service=="serv1", Version, null())
| stats values(*) as * by RecordID
| table Name RecordID Version1 Version2 service

 

How does it look like? (Note this search includes nothing from serv2 unless service itself is also multivalued.) Then, substitute for serv2 in the first line to verify whether multiple values occur in serv2.

0 Karma

arunakalla
Explorer

 

    index=test (service=serv1 OR service=serv2)
    | eval Version2 = if(service=="serv2", Version, null())
    | eval Version1 = if(service=="serv1", Version, null())
    | stats values(*) as * by RecordID
    | table Name RecordID Version1 Version2
    | where isnotnull(Version2)

 

the above achieved my expected result.. Thanks you for all your help @yuanliu  

0 Karma

yuanliu
SplunkTrust
SplunkTrust

Did the previously revised search work?

0 Karma

arunakalla
Explorer

Yes, double quotes worked and hence the above issue..

0 Karma

arunakalla
Explorer

I tried all help and  [used Join command and/or stats command] .. nothing worked for my case

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...