官术网_书友最值得收藏!

Event modeling

The first step in event modeling is to express the event in English in the following form:

Subject-verb-direct object

For this example, we are modeling the event customer consults the ETH price:

  • The subject in this sentence is customer, a noun in nominative case. The subject is the entity performing the action.
  • The verb in this sentence is consults; it describes the action performed by the subject.
  • The direct object in this sentence is ETH price. The object is the entity in which the action is being done.

We can represent our message in several message formats (covered in other sections of this book):

  • JavaScript Object Notation (JSON)
  • Apache Avro
  • Apache Thrift
  • Protocol Buffers

JSON is easily read and written by both humans and machines. For example, we could chose binary as the representation, but it has a rigid format and it was not designed for humans to read it; as counterweight, binary representation is very fast and lightweight in processing.

Listing 2.1, shows the representation of the CUSTOMER_CONSULTS_ETHPRICE event in JSON format:

{
"event": "CUSTOMER_CONSULTS_ETHPRICE",
"customer": {
"id": "14862768",
"name": "Snowden, Edward",
"ipAddress": "95.31.18.111"
},
"currency": {
"name": "ethereum",
"price": "RUB"
},
"timestamp": "2018-09-28T09:09:09Z"
}
Listing 2.1:  customer_consults_ETHprice.json

For this example, the Ethereum (ETH) currency price is expressed in Russian rouble (RUB). This JSON message has four sections:

  • event: This is a string with the event's name.
  • customer: This represents the person (in this case its id is 14862768) consulting the Ethereum price. In this representation, there is a unique id for the customer, the name, and the browser ipAddress, which is the IP address of the computer the customer is logged on.
  • currency: This contains the cryptocurrency name and the currency in which the price is expressed.
  • timestamp: The timestamp in which the customer made the request (UTC).

From another perspective, the message has two parts: the metadata—this is the event name and the timestamp and two business entities, the customer and the currency. As we can see, this message can be read and understood by a human.

Other messages from the same use case in JSON format could be as follows:

{ "event": "CUSTOMER_CONSULTS_ETHPRICE",
"customer": {
"id": "13548310",
"name": "Assange, Julian",
"ipAddress": "185.86.151.11"
},
"currency": {
"name": "ethereum",
"price": "EUR"
},
"timestamp": "2018-09-28T08:08:14Z"
}

This is another example:

{ "event": "CUSTOMER_CONSULTS_ETHPRICE",
"customer": {
"id": "15887564",
"name": "Mills, Lindsay",
"ipAddress": "186.46.129.15"
},
"currency": {
"name": "ethereum",
"price": "USD"
},
"timestamp": "2018-09-28T19:51:35Z"
}

What happens if we want to represent our message in the Avro schema? Yes, the Avro schema of our message (note that it's not the message, but the schema) is in Listing 2.2:

{ "name": "customer_consults_ethprice",
"namespace": "monedero.avro",
"type": "record",
"fields": [
{ "name": "event", "type": "string" },
{ "name": "customer",
"type": {
"name": "id", "type": "long",
"name": "name", "type": "string",
"name": "ipAddress", "type": "string"
}
},
{ "name": "currency",
"type": {
"name": "name", "type": "string",
"name": "price", "type": {
"type": "enum", "namespace": "monedero.avro",
"name": "priceEnum", "symbols": ["USD", "EUR", "RUB"]}
}
},
{ "name": "timestamp", "type": "long",
"logicalType": "timestamp-millis"
}
]
}
Listing 2.2: customer_consults_ethprice.avsc

For more information about the Avro schema, check the Apache Avro specification:

https://avro.apache.org/docs/1.8.2/spec.html

主站蜘蛛池模板: 金门县| 兖州市| 永善县| 化州市| 沧源| 开平市| 卢龙县| 璧山县| 吉木萨尔县| 县级市| 突泉县| 和龙市| 中方县| 旺苍县| 灌阳县| 贵港市| 闸北区| 阜城县| 包头市| 建水县| 彭山县| 永城市| 黄山市| 东源县| 莒南县| 博客| 虞城县| 宜兰县| 郁南县| 大连市| 三原县| 利辛县| 北川| 眉山市| 柏乡县| 宜章县| 苏尼特左旗| 色达县| 福泉市| 赤城县| 舒兰市|