Skip to content

We need a logic to convert events into JSON format. #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

chungeun-choi
Copy link
Contributor

Overview

In the majority of projects utilizing the pymysqlreplication library, it has been observed that binlog events are being converted into JSON format for usage.

[Examples]

Streaming Changes in a Database with Amazon Kinesis

Near Zero Downtime Migration from MySQL to DynamoDB

Consequently, we have developed logic within our library to provide binlog events in JSON format.

  • How to execute

    stream = BinLogStreamReader(
            connection_settings=MYSQL_SETTINGS, server_id=3, blocking=True
        )
    
        for binlogevent in stream:
            json_result = binlogevent.to_json()
            print(json_result)
  • Results

     {
    	"evnet_type": 30,
    	"date": "2023-09-29T04:15:11",
    	"log_pos": 994,
    	"event_size": 103,
    	"read_bytes": 12,
    	"table": ["test", "r1"],
    	"affected_columns": 3,
    	"changed_rows": 7,
    	"flag": true,
    	"event_name": "WriteRowsEvent",
    	"rows": [{
    		"values": {
    			"i1": 1,
    			"c1": "#1",
    			"d1": "2023-09-29T04:15:11"
    		}
    	}, {
    		"values": {
    			"i1": 2,
    			"c1": "#2",
    			"d1": "2023-09-29T04:15:11"
    		}
    	}, {
    		"values": {
    			"i1": 3,
    			"c1": "#3",
    			"d1": "2023-09-29T04:15:11"
    		}
    	}, {
    		"values": {
    			"i1": 4,
    			"c1": "#4",
    			"d1": "2023-09-29T04:15:11"
    		}
    	}, {
    		"values": {
    			"i1": 5,
    			"c1": "#5",
    			"d1": "2023-09-29T04:15:11"
    		}
    	}, {
    		"values": {
    			"i1": 6,
    			"c1": "#6",
    			"d1": "2023-09-29T04:15:11"
    		}
    	}, {
    		"values": {
    			"i1": 7,
    			"c1": "#7",
    			"d1": "2023-09-29T04:15:11"
    		}
    	}]
    }

Benefit

Through this logic, users can reduce the steps needed for additional processing.

The connection between heterogeneous software through JSON is expected to become more smooth and seamless.

Copy link
Collaborator

@sean-k1 sean-k1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comment

Copy link
Collaborator

@sean-k1 sean-k1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think It might be a good idea to add some test cases.

Comment on lines +773 to +774
if self.optional_metadata:
result_dict.update(self.optional_metadata.to_json())
Copy link
Collaborator

@sean-k1 sean-k1 Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.optional_metadata always got object so it is Not None

optional_metadata = OptionalMetaData()

try this variable self.__optional_meta_data when check

and can we get some testcases?

Comment on lines +147 to +157
def _to_json(self):
gtid_event_dict = {
"event_name": type(self).__name__,
"commit": self.commit_flag,
"gtid_next": self.gtid,
}
if hasattr(self, "last_committed"):
gtid_event_dict["last_committed"] = self.last_committed
gtid_event_dict["sequence_number"] = self.sequence_number
return gtid_event_dict

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some test cases is more good for us

Comment on lines +930 to +944
def _to_json(self):
user_var_event_dict = {
"event_name": type(self).__name__,
"user_variable_name": self.name,
}
if not self.is_null:
user_var_event_dict["type"] = self.type_to_codes_and_method.get(
self.type, ["UNKNOWN_TYPE"]
)[0]
user_var_event_dict["charset"] = self.charset
user_var_event_dict["value"] = self.value
user_var_event_dict["flags"] = self.flags

return user_var_event_dict

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some test cases is more good for us

def _to_json(self):
return {
"event_name": type(self).__name__,
"schema": self.schema.decode("utf-8"),
Copy link
Collaborator

@sean-k1 sean-k1 Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

563 Line I think it's better to send it in the same form as the dump, but what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants