Available: defineTable() v1.163.0
The defineTable()
function is available from
version 1.163
Executes a subquery that generates an in-memory, ad-hoc table based on its
results. The ad-hoc table can be joined with the results of the primary
query using the match()
function.
defineTable()
is the recommended alternative to the
join()
function, allowing for easier query writing of
complex joins. For more explanations on the benefits of using ad-hoc
tables with defineTable()
instead of
join()
, see
Ad-hoc Tables vs. join().
Combined with match()
and
readFile()
query functions,
defineTable()
can be used to create several types of
join-like queries — see
defineTable()
Examples.
Parameter | Type | Required | Default Value | Description |
---|---|---|---|---|
query | function | required | Subquery used to generate the ad-hoc table. | |
name [a] | string | required | Name of the ad-hoc table that is generated. Used to reference the table in other functions within the primary query. | |
include | Array of strings | required | Fields to include as columns in the temporary table. If set to * all fields will be included. | |
view | string | optional[b] | same as primary query | View in which to perform the subquery. |
start | string | optional[b] | same as primary query | Start of time interval of subquery: milliseconds since UNIX epoch, or time interval using Relative Time Syntax (for example, 2d, 24h, 10sec). |
end | string | optional[b] | same as primary query | End of time interval of subquery: milliseconds since UNIX epoch, or time interval using Relative Time Syntax (for example, 2d, 24h, 10sec). |
[b] Optional parameters use their default value unless explicitly set. |
Hide omitted argument names for this function
Omitted Argument NamesThe argument name for
name
can be omitted; the following forms of this function are equivalent:logscaledefineTable("value",include=["value"],query="value")
and:
logscaledefineTable(name="value",include=["value"],query="value")
These examples show basic structure only.
The function's signature combined with match()
:
defineTable(query={a=hello}, name="tablename", include=[col1,col2,col3])
| match(table="tablename",field=fieldname, column=col1)
For example, to match email data within the same view:
email=bob@example.com firstname=bob lastname=thomas
loginemail=bob@example.com action=register
perform a subquery and a primary query:
defineTable(query={email=*}, name="emails", include=[email,firstname, lastname])
| match(table="emails", field=loginemail, column=email)
The first query in the pipeline is the subquery used for table definition.
The second query in the pipeline is the primary query that uses
match()
.
The following example query combines information about the
ProcessRollUp2
and NetworkListenIP4
to find
processes that have created listeners on a port.
This is the full query:
logscaledefineTable(query={#event_simpleName=NetworkListenIP4 LocalPort<1024 LocalPort!=0}, name="network_listener", include=[ContextProcessId,LocalAddressIP4, LocalPort]) | #event_simpleName=ProcessRollup2 | match(table="network_listener",field=TargetProcessId,column=ContextProcessId)
The subquery with
defineTable()
generates a result table namednetwork_listener
:ContextProcessId LocalAddressIP4 LocalPort 123 172.16.254.1 1010 456 172.19.254.1 2020 789 190.16.254.1 3030 The second item in the pipeline filters #event_simpleName field to only take the
ProcessRollUp2
value.match()
joins the results of thenetwork_listener
ad-hoc table with the primary query, by matching:TargetProcessId field from the primary query
ContextProcessId column field from the ad-hoc, generated table.
Note
To ensure optimal performance when using
defineTable()
, follow the best practice described
at Ad-hoc Tables Optimization.
For more information on the different methods of creating join queries
— including ad-hoc tables with the
defineTable()
function — see
Join Methods.
Important
When using defineTable()
, be aware that ad-hoc
tables are not supported in Alerts, for the
reasons explained at
Ad-hoc Tables in Live Queries. Use
Scheduled Searches instead.
defineTable()
Examples
Perform a Left Join Query to Combine Two Datasets
Query
defineTable(name="users_table",query={orgId=1},include=[username, name])
| operation=createdFile
| match(table=users_table, field=username, strict=false)
| select([username, name])
Introduction
In this example, the defineTable()
function
is used as a left join query to extract and combine information
from two different datasets.
The event set for the query is in one repository, but the event set for each query is shown separately to identify the two sets of information. The first event set is:
username,name,orgId |
user1,"John Doe",1 |
user2,"Jane Doe",1 |
user3,"Bob Smith",2 |
and the other event set:
username,operation |
user1,createdFile |
user2,deletedFile |
user3,createdFile |
Step-by-Step
Starting with the source repository events.
- logscale
defineTable(name="users_table",query={orgId=1},include=[username, name])
Generates an ad-hoc table named
users_table
that has the fields username and name and includes users where orgId field equals1
. - logscale
| operation=createdFile
Filters on the field operation for users who performed the action of creating a file by looking for the value
createdFile
. - logscale
| match(table=users_table, field=username, strict=false)
Joins with
users_table
table. - logscale
| select([username, name])
Selects the username and name fields to be displayed from the event set.
Event Result set.
Summary and Results
The result will output two events:
username | name |
---|---|
user1 | John Doe |
user3 | no value |
where user3 has no value since this user is
not included in the users_table
table
user2
(not belonging to
orgId=1).
Perform a Nested Join Query to Combine Two Datasets and Two Tables
Query
defineTable(name="organization_table",query={orgId=1},include=[username, orgId],view=organizations)
| defineTable(name="users_table",query={match(table=organization_table, field=username)},include=[username, name])
| operation=createdFile
| match(table=users_table, field=username)
| select([username, name])
Introduction
Similar to the inner
join example, defineTable()
first
creates a separate table for organizations belonging to a
different view, which is then matched against a users' table as
a nested-like join.
The event set for the query is in one repository, but the event set for each query is shown separately to identify the two sets of information. The first event set is:
username,orgId |
user1,1 |
user2,1 |
user3,2 |
and the other event set:
username,name |
user1,"John Doe" |
user2,"Jane Doe" |
user3,"Bob Smith" |
Step-by-Step
Starting with the source repository events.
- logscale
defineTable(name="organization_table",query={orgId=1},include=[username, orgId],view=organizations)
Generates an ad-hoc table named
organization_table
that has the fields username and orgId and includes users where orgId field equals1
from theorganizations
view. - logscale
| defineTable(name="users_table",query={match(table=organization_table, field=username)},include=[username, name])
Generates an ad-hoc table named
users_table
that has the fields username and name and enriches rows with orgId=1 fromorganization_table
- logscale
| operation=createdFile
Filters on the field operation for users who performed the action of creating a file by looking for the value
createdFile
. - logscale
| match(table=users_table, field=username)
Joins with
users_table
table, to filter out users who are not from orgId=1 and to enrich with the users' names. - logscale
| select([username, name])
Selects the username and name fields to be displayed from the event set.
Event Result set.
Summary and Results
The result will output one event:
username | name |
---|---|
user1 | John Doe |
Perform a Right Join Query to Combine Two Datasets
Query
defineTable(name="users",query={orgId=1},include=[username, name])
| defineTable(name="operations",query={*},include=[username, operation])
| readFile(users)
| match(operations, field=username, strict=false)
| select([username, operation])
Introduction
In this example, the defineTable()
function
is used as a right join query to extract and combine information
from two different datasets.
The event set for the query is in one repository, but the event set for each query is shown separately to identify the two sets of information. The first event set is:
username,name,orgId |
user1,"John Doe",1 |
user2,"Jane Doe",1 |
user3,"Bob Smith",2 |
and the other event set:
username,operation |
user1,createdFile |
user3,createdFile |
Step-by-Step
Starting with the source repository events.
- logscale
defineTable(name="users",query={orgId=1},include=[username, name])
Generates an ad-hoc table named
users
that has the fields username and name and includes users where orgId field equals1
. - logscale
| defineTable(name="operations",query={*},include=[username, operation])
Defines a new ad-hoc table that uses all the fields (username and operation) in a table named
operations
. - logscale
| readFile(users)
Reads the
users
ad-hoc table as events usingreadFile()
. - logscale
| match(operations, field=username, strict=false)
Matches the events that have a matching operation from the
operations
table with theusers
table using the username as the common field. Events are not filtered if the events do not match, (implying a right join), by usingstrict=false
- logscale
| select([username, operation])
Selects the username and operation fields to be displayed from the event set.
Event Result set.
Summary and Results
The result will output two events:
username | operation |
---|---|
user1 | createdFile |
user2 | no value |
Note that in the event set all operations have been included
even when there is no match between the
username field, resulting in the
no value
for user2
. If
strict=true
had been
used to the match()
function, then the
event for user2
would not have been outputted.
Perform an Inner Join Query to Combine Two Datasets
Query
defineTable(name="users_table",query={orgId=1},include=[username, name])
| orgId=1
| operation=createdFile
| match(table=users_table, field=username)
| select([username, name])
Introduction
In this example, the defineTable()
function
is used as an inner join query to extract and combine
information from two different datasets.
The event set for the query is in one repository, but the event set for each query is shown separately to identify the two sets of information. The first event set is:
username,name,orgId |
user1,"John Doe",1 |
user2,Jane Doe",1 |
user3,"Bob Smith",2 |
and the other event set:
username,operation |
user1,createdFile |
user2,deletedFile |
user3,createdFile |
Step-by-Step
Starting with the source repository events.
- logscale
defineTable(name="users_table",query={orgId=1},include=[username, name])
Generates an ad-hoc table named
users_table
that has the fields username and name and filters used where the orgId field equals1
. - logscale
| orgId=1
Selects only users from the orgId where the value equals
1
. - logscale
| operation=createdFile
Filters on the field operation for users who performed the action of creating a file by looking for the value
createdFile
. - logscale
| match(table=users_table, field=username)
Joins the filtered events (users that created a file in organization 1) using the username field with the
users_table
table. - logscale
| select([username, name])
Selects the username and name fields to be displayed in the result set.
Event Result set.
Summary and Results
The result will output one event:
username | name |
---|---|
user1 | John Doe |
Using Ad-hoc Table With CSV File
Query
defineTable(name="users_table",query={match(file=organizations.csv, field=username)
| orgId=1 },include=[username, name])
| operation=createdFile
| match(table=users_table, field=username)
| select([username, name])
Introduction
In this example, the defineTable()
is used
to extract and combine information from two different datasets,
with the mapping between username and
orgId stored in a CSV file. The example
file organizations.csv
has the following
content:
username,orgId
user1,1
user2,1
user3,2
The event set for the query is in one repository, but the event set for each query is shown separately to identify the two sets of information. The first event set is:
username,name |
user1,"John Doe" |
user2,"Jane Doe" |
user3,"Bob Smith" |
and the other event set:
username,operation |
user1,createdFile |
user2,deletedFile |
user3,createdFile |
Step-by-Step
Starting with the source repository events.
- logscale
defineTable(name="users_table",query={match(file=organizations.csv, field=username) | orgId=1 },include=[username, name])
Generates an ad-hoc table named
users_table
that has the fields username and name and includes users where orgId field equals1
. Then usesmatch()
to enrich rows with orgId fromorganizations.csv
file. - logscale
| operation=createdFile
Filters on the field operation for users who performed the action of creating a file by looking for the value
createdFile
. - logscale
| match(table=users_table, field=username)
Joins the username field with the
users_table
table, to filter out users who are not from orgId=1 and to enrich with the users' names. - logscale
| select([username, name])
Selects the username and name fields to be displayed from the event set.
Event Result set.
Summary and Results
The result will output one event:
username | name |
---|---|
user1 | John Doe |