Custom Sink
GlassGen allows you to create your own custom sinks by extending the BaseSink class. This is useful when you need to:
- Send data to a custom destination
- Process data in a specific way
- Integrate with systems not supported by built-in sinks
Creating a Custom Sink
Extend BaseSink and implement three methods:
publish(self, data: dict): Handle a single record. Note: the GlassGen engine never callspublish()directly — it is there for you to call manually if needed.publish_bulk(self, data: list): Handle a batch of records. This is the method the engine calls during generation.close(self): Clean up any resources when generation is complete.
from glassgen.sinks import BaseSink
class PrintSink(BaseSink):
def publish(self, data):
print(data)
def publish_bulk(self, data):
for item in data:
print(item)
def close(self):
passUsing a Custom Sink
Pass your sink instance to generate() via the sink= parameter. Custom sinks cannot be configured through the "sink" block in a config dict — that block only supports the built-in sink types.
import glassgen
from glassgen.sinks import BaseSink
class PrintSink(BaseSink):
def publish(self, data):
print(data)
def publish_bulk(self, data):
for item in data:
print(item)
def close(self):
pass
config = {
"schema": {
"name": "$name",
"email": "$email",
"country": "$country",
"id": "$uuid",
"address": "$address",
"phone": "$phone_number",
"job": "$job",
"company": "$company"
},
"generator": {
"rps": 10,
"num_records": 100
}
}
# Pass the sink instance directly — do not put it in the config dict
glassgen.generate(config=config, sink=PrintSink())Last updated on