Source connectors in Kafka - Time & Space Complexity
When using source connectors in Kafka, it's important to understand how the time to process data grows as the input size increases.
We want to know how the connector's work changes when more data arrives.
Analyze the time complexity of the following source connector poll method.
public List<SourceRecord> poll() throws InterruptedException {
List<SourceRecord> records = new ArrayList<>();
while (hasMoreData()) {
SourceRecord record = readNextRecord();
records.add(record);
}
return records;
}
This code reads all available data from the source system and collects it into a list to send to Kafka.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that reads each record one by one.
- How many times: Once for every available record in the source system.
As the number of records to read grows, the time to run this method grows in a similar way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and adds |
| 100 | About 100 reads and adds |
| 1000 | About 1000 reads and adds |
Pattern observation: The work grows directly with the number of records to process.
Time Complexity: O(n)
This means the time to read records grows in a straight line as the number of records increases.
[X] Wrong: "The connector reads all data instantly regardless of size."
[OK] Correct: Each record must be read one by one, so more data means more time.
Understanding how source connectors scale with data size helps you explain real-world data flow and performance in Kafka systems.
"What if the connector batches records in groups instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of source connectors
Source connectors are designed to bring data from outside systems into Kafka topics automatically.Step 2: Differentiate from sink connectors
Sink connectors do the opposite by exporting data from Kafka to external systems, so they are not the correct answer here.Final Answer:
To automatically import data from external systems into Kafka topics -> Option AQuick Check:
Source connector = import data [OK]
- Confusing source connectors with sink connectors
- Thinking connectors manage Kafka internals like partitions
- Assuming connectors monitor Kafka health
Solution
Step 1: Recall Kafka Connect configuration syntax
The key to specify the connector implementation class is always "connector.class" in the JSON config.Step 2: Eliminate incorrect options
Other options like "source.class", "connectorType", or "sourceConnector" are not valid Kafka Connect configuration keys.Final Answer:
"connector.class" -> Option DQuick Check:
Connector class key = "connector.class" [OK]
- Using incorrect or made-up JSON keys
- Confusing source connector config keys with sink connector keys
- Omitting quotes around JSON keys
{
"name": "my-source-connector",
"connector.class": "FileStreamSource",
"tasks.max": "1",
"file": "/tmp/input.txt",
"topic": "test-topic"
}What will happen when this connector runs?
Solution
Step 1: Identify connector type and purpose
The "FileStreamSource" connector reads data from a file and sends it to a Kafka topic.Step 2: Understand configuration keys
"file" specifies the input file, "topic" specifies the Kafka topic to write to, and "tasks.max" as a string is acceptable in JSON configs.Final Answer:
It reads data from /tmp/input.txt and writes it to the Kafka topic 'test-topic' -> Option BQuick Check:
FileStreamSource reads file to topic [OK]
- Confusing source and sink connectors
- Thinking 'tasks.max' must be integer type (string is valid in JSON)
- Assuming connector creates topics automatically
{
"name": "my-connector",
"connector.class": "JdbcSourceConnector",
"tasks.max": 2,
"connection.url": "jdbc:mysql://localhost:3306/mydb",
"topic.prefix": "db-"
}But the connector fails to start. What is the most likely cause?
Solution
Step 1: Review JdbcSourceConnector required properties
JdbcSourceConnector requires a 'mode' property to specify how to query the database (e.g., 'incrementing', 'timestamp+incrementing').Step 2: Check other properties
'tasks.max' as integer is valid, 'topic.prefix' is correct, and 'connection.url' can be MySQL syntax if using MySQL.Final Answer:
Missing required property 'mode' for JdbcSourceConnector -> Option AQuick Check:
JdbcSourceConnector needs 'mode' property [OK]
- Assuming 'tasks.max' must be string
- Confusing 'topic.prefix' with 'topic.name'
- Thinking connection URL must be PostgreSQL always
Solution
Step 1: Identify connector capabilities
FilePulse source connector supports reading multiple files from a directory and processing CSV files.Step 2: Evaluate other options
FileStreamSource reads only a single file, JdbcSourceConnector is for databases, and manual coding is not using source connectors.Final Answer:
Use a FilePulse source connector configured with the directory path to read all CSV files -> Option CQuick Check:
FilePulse reads multiple files from directory [OK]
- Using FileStreamSource for multiple files
- Trying to use JdbcSourceConnector for CSV files
- Ignoring built-in connectors and coding manually
Ready to prove it?
Pass the test and this pattern moves unknown
