Apache Pinot
Apache Pinot
A real-time distributed OLAP datastore. Developed by LinkedIn.
Apache Pinot
A real-time distributed OLAP datastore. Developed by LinkedIn.
Apache Pinot is a real-time distributed OLAP datastore developed at LinkedIn that delivers ultra-low-latency (sub-second) analytical queries on large-scale datasets. It is optimized for workloads that require millisecond-level responses, such as user-facing analytics applications, real-time dashboards, and anomaly detection systems.
Pinot's architecture consists of three core components. The Controller is responsible for cluster metadata and resource management, the Broker handles query routing and result merging, and the Server stores the actual data and executes queries. This distributed architecture enables horizontal scaling, and LinkedIn operates clusters made up of thousands of nodes.
Pinot supports two table types: real-time (REALTIME) and offline (OFFLINE). Real-time tables ingest data directly from streaming sources such as Kafka or Kinesis and make it queryable immediately, while offline tables serve historical data loaded in batches. Hybrid tables combine the two types, allowing real-time data and historical data to be queried together.
The key to its high performance lies in its indexing strategy. Pinot provides a variety of indexes, including Inverted Index, Sorted Index, Star-Tree Index, Range Index, Text Index, and JSON Index, and optimizes performance by combining appropriate indexes according to query patterns. In particular, the Star-Tree Index dramatically accelerates high-dimensional analytical queries through pre-aggregation.
# Apache Pinot ํ
์ด๋ธ ์คํค๋ง ๋ฐ ์ค์
# ============================================
# 1. ์คํค๋ง ์ ์ (events_schema.json)
# ============================================
{
"schemaName": "user_events",
"dimensionFieldSpecs": [
{
"name": "event_id",
"dataType": "STRING"
},
{
"name": "user_id",
"dataType": "STRING"
},
{
"name": "event_type",
"dataType": "STRING"
},
{
"name": "device_type",
"dataType": "STRING"
},
{
"name": "country",
"dataType": "STRING"
},
{
"name": "city",
"dataType": "STRING"
}
],
"metricFieldSpecs": [
{
"name": "event_count",
"dataType": "LONG"
},
{
"name": "revenue",
"dataType": "DOUBLE"
},
{
"name": "session_duration_ms",
"dataType": "LONG"
}
],
"dateTimeFieldSpecs": [
{
"name": "event_timestamp",
"dataType": "TIMESTAMP",
"format": "1:MILLISECONDS:EPOCH",
"granularity": "1:MILLISECONDS"
}
]
}
# ============================================
# 2. ์ค์๊ฐ ํ
์ด๋ธ ์ค์ (events_realtime_table.json)
# ============================================
{
"tableName": "user_events_REALTIME",
"tableType": "REALTIME",
"segmentsConfig": {
"timeColumnName": "event_timestamp",
"timeType": "MILLISECONDS",
"schemaName": "user_events",
"replicasPerPartition": "2",
"retentionTimeUnit": "DAYS",
"retentionTimeValue": "7"
},
"tenants": {
"broker": "DefaultTenant",
"server": "DefaultTenant"
},
"tableIndexConfig": {
"loadMode": "MMAP",
"invertedIndexColumns": [
"user_id",
"event_type",
"device_type",
"country"
],
"rangeIndexColumns": [
"event_timestamp",
"revenue"
],
"sortedColumn": [
"event_timestamp"
],
"bloomFilterColumns": [
"user_id",
"event_id"
],
"noDictionaryColumns": [
"event_id",
"session_duration_ms"
],
"streamConfigs": {
"streamType": "kafka",
"stream.kafka.consumer.type": "lowLevel",
"stream.kafka.topic.name": "user-events",
"stream.kafka.decoder.class.name": "org.apache.pinot.plugin.stream.kafka.KafkaJSONMessageDecoder",
"stream.kafka.consumer.factory.class.name": "org.apache.pinot.plugin.stream.kafka20.KafkaConsumerFactory",
"stream.kafka.broker.list": "kafka-broker:9092",
"stream.kafka.consumer.prop.auto.offset.reset": "smallest",
"realtime.segment.flush.threshold.rows": "500000",
"realtime.segment.flush.threshold.time": "6h"
}
},
"routing": {
"instanceSelectorType": "strictReplicaGroup"
},
"upsertConfig": {
"mode": "FULL",
"hashFunction": "MURMUR3"
},
"metadata": {
"customConfigs": {}
}
}
# ============================================
# 3. Star-Tree ์ธ๋ฑ์ค ์ค์ (๊ณ ์ฑ๋ฅ ์ง๊ณ์ฉ)
# ============================================
{
"tableIndexConfig": {
"starTreeIndexConfigs": [
{
"dimensionsSplitOrder": [
"country",
"device_type",
"event_type"
],
"skipStarNodeCreationForDimensions": [],
"functionColumnPairs": [
"COUNT__*",
"SUM__revenue",
"SUM__event_count",
"AVG__session_duration_ms"
],
"maxLeafRecords": 10000
}
]
}
}
# ============================================
# 4. ์ค์๊ฐ ๋ถ์ ์ฟผ๋ฆฌ ์์ (SQL)
# ============================================
-- ์ค์๊ฐ ์ด๋ฒคํธ ์ง๊ณ (์ง๋ 1์๊ฐ)
SELECT
event_type,
country,
COUNT(*) as event_count,
SUM(revenue) as total_revenue,
AVG(session_duration_ms) as avg_session_ms
FROM user_events
WHERE event_timestamp > ago('PT1H')
GROUP BY event_type, country
ORDER BY event_count DESC
LIMIT 100
-- ์๊ฐ๋๋ณ ํธ๋ ๋ ๋ถ์
SELECT
DATETIMECONVERT(event_timestamp, '1:MILLISECONDS:EPOCH',
'1:MINUTES:EPOCH', '5:MINUTES') as time_bucket,
event_type,
COUNT(*) as events,
DISTINCTCOUNT(user_id) as unique_users
FROM user_events
WHERE event_timestamp BETWEEN
FromDateTime('2024-01-15 00:00:00', 'yyyy-MM-dd HH:mm:ss')
AND FromDateTime('2024-01-15 23:59:59', 'yyyy-MM-dd HH:mm:ss')
GROUP BY time_bucket, event_type
ORDER BY time_bucket
-- ์ค์๊ฐ Top-K ์ฌ์ฉ์
SELECT
user_id,
COUNT(*) as activity_count,
SUM(revenue) as total_spent
FROM user_events
WHERE event_timestamp > ago('PT24H')
GROUP BY user_id
ORDER BY activity_count DESC
LIMIT 10
-- ํผ๋ ๋ถ์ (๊ทผ์ฌ)
SELECT
SUM(CASE WHEN event_type = 'page_view' THEN 1 ELSE 0 END) as step1_views,
SUM(CASE WHEN event_type = 'add_to_cart' THEN 1 ELSE 0 END) as step2_cart,
SUM(CASE WHEN event_type = 'checkout' THEN 1 ELSE 0 END) as step3_checkout,
SUM(CASE WHEN event_type = 'purchase' THEN 1 ELSE 0 END) as step4_purchase
FROM user_events
WHERE event_timestamp > ago('PT7D')
-- ์ง์ญ๋ณ ์ค์๊ฐ ๋์๋ณด๋
SELECT
country,
city,
COUNT(*) as events,
DISTINCTCOUNTHLL(user_id) as approx_users,
PERCENTILEEST(session_duration_ms, 50) as median_session,
PERCENTILEEST(session_duration_ms, 95) as p95_session
FROM user_events
WHERE event_timestamp > ago('PT1H')
GROUP BY country, city
HAVING events > 100
ORDER BY events DESC
# ============================================
# 5. Python ํด๋ผ์ด์ธํธ ์์
# ============================================
from pinotdb import connect
import pandas as pd
# Pinot ์ฐ๊ฒฐ
conn = connect(
host='pinot-broker',
port=8099,
path='/query/sql',
scheme='http'
)
# ์ฟผ๋ฆฌ ์คํ
cursor = conn.cursor()
query = """
SELECT
event_type,
COUNT(*) as count,
SUM(revenue) as revenue
FROM user_events
WHERE event_timestamp > ago('PT1H')
GROUP BY event_type
ORDER BY count DESC
LIMIT 10
"""
cursor.execute(query)
results = cursor.fetchall()
# Pandas DataFrame์ผ๋ก ๋ณํ
df = pd.DataFrame(results, columns=[desc[0] for desc in cursor.description])
print(df)
# ============================================
# 6. ํ
์ด๋ธ ๊ด๋ฆฌ CLI ๋ช
๋ น์ด
# ============================================
# ์คํค๋ง ์ถ๊ฐ
curl -X POST "http://localhost:9000/schemas" \
-H "Content-Type: application/json" \
-d @events_schema.json
# ํ
์ด๋ธ ์์ฑ
curl -X POST "http://localhost:9000/tables" \
-H "Content-Type: application/json" \
-d @events_realtime_table.json
# ์ธ๊ทธ๋จผํธ ์ํ ํ์ธ
curl "http://localhost:9000/segments/user_events_REALTIME"
# ํ
์ด๋ธ ๋ฆฌ๋ก๋
curl -X POST "http://localhost:9000/segments/user_events_REALTIME/reload"
# ์ฟผ๋ฆฌ ์คํ
curl -X POST "http://localhost:8099/query/sql" \
-H "Content-Type: application/json" \
-d '{"sql": "SELECT COUNT(*) FROM user_events"}'
Product Manager: "The real-time dashboard takes over 5 seconds to respond, and users are complaining."
Data Engineer: "We're currently handling it with Elasticsearch, but it has limitations with large-scale aggregation queries. Adopting Apache Pinot would enable millisecond-level responses."
Product Manager: "Can we query real-time data right away, too?"
Data Engineer: "Yes, it ingests from Kafka in real time so you can query it right away. If you set it up as a hybrid table, you can query real-time data and historical data together."
Interviewer: "Please explain how Apache Pinot's Star-Tree index works and what kinds of queries it is effective for."
Candidate: "Star-Tree is an index that pre-aggregates data and stores it in a tree structure. For example, if you precompute SUM(revenue) for the country, device, and event_type dimensions, you can return results immediately at runtime without scanning the raw data. It is effective when the dimensions in the GROUP BY clause and the aggregation functions match the Star-Tree configuration, and it is best to set the dimension order from lowest to highest cardinality. However, Star-Tree requires additional storage space and increases ingestion time, so it is best to apply it only to frequently used query patterns."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "You're using dictionary encoding on the user_id column, but if there are hundreds of millions of unique values, memory issues may arise."
Developer: "Then should I add it to noDictionaryColumns?"
Reviewer: "Yes, it's better to store high-cardinality columns without a dictionary. Instead, adding a Bloom Filter index lets you maintain point lookup performance. Also, using DISTINCTCOUNTHLL instead of DISTINCTCOUNT improves memory efficiency."