have any example about `state`? I don't know what...
# troubleshooting
h
have any example about
state
? I don't know what to do when customizing the tap
can someone help me?😭
v
I don't understand your question 😕
h
@visch I'm sorry for my bad english , I mean I want to define a new tap that supports state like tap-mysql, does Meltano have relevant examples because I don't understand tap-mysql's source code
e
Hi @hello_dev! More details on what you’re building and what you’re using to build it would certainly help. But, if you’re using the SDK, check out get_starting_timestamp. You can use the returned value in a method like get_url_params to filter records.
h
Hi @edgar_ramirez_mondragon , I use the SDK,but don't understand from the documentation how to save the state
e
h
Hi @edgar_ramirez_mondragon,this is my code,how to save state?😭
Copy code
state=self.get_context_state()
        state = singer.write_bookmark(state,
                                      'log',
                                      'last_time', toTime
                                      )
        # should save state,but i don't know how to do
e
You don’t need to call
singer.write_bookmark
, and that would only write it to stdout, not save it. Does you stream declare a
replication_key
?
h
no,I try this
self.replication_key=[str(toTime)]
,but fail
@edgar_ramirez_mondragon Can you help me, I have tried many ways but can't operate state, As shown below, state can be inserted into the database only when the
elt
command is used, and
state.json
cannot be generated, and state cannot be inserted using the
run
command
Copy code
class LogStream(SlsStream):
    """Define custom stream."""
    primary_keys = ["time"]
    replication_key = 'time'
e
You don’t need to insert state, both Meltano
elt
and
run
will generate the state JSON object and store it in its system db (sqlite by default).
a
Hi, @hello_dev. Just circling back on this. Did you find what you needed? As @edgar_ramirez_mondragon mentions, state is really handled for you automatically. Saving state: As long as
replication_key
is set in the stream, the SDK will track state on that key automatically and the state bookmarks will automatically be sent to your target. (If using Meltano, the state is stored in an artifact linked to
state_id
which is automatic when using
meltano run
or driven by
--state-id
in other cases.) The target will write out the state artifact to STDOUT once it has processed all records up to that point. Use state on the next run(s): Again, if using Meltano and a valid state ID (either explicitly or via auto-generated IDs from
meltano run
) the state will be loaded automatically using the
--state
CLI arg, and you just need to make sure you send the latest bookmarks from
get_starting_timestamp()
into
get_url_params()
. Basically, as long as you have
replication_key
defined and you are passing its value back to your API, the rest should be automatic. Does this help?