github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/testing/benchmarks/nexmark/models/nexmark_model.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 """Nexmark model. 19 20 The nexmark suite is a series of queries (streaming pipelines) performed 21 on a simulation of auction events. The model includes the three roles that 22 generate events: 23 24 - The person who starts and auction or makes a bid (Person). 25 - The auction item (Auction). 26 - The bid on an item for auction (Bid). 27 28 """ 29 from apache_beam.coders import coder_impl 30 from apache_beam.coders.coders import FastCoder 31 from apache_beam.coders.coders import StrUtf8Coder 32 from apache_beam.testing.benchmarks.nexmark import nexmark_util 33 34 35 class PersonCoder(FastCoder): 36 def to_type_hint(self): 37 return Person 38 39 def _create_impl(self): 40 return PersonCoderImpl() 41 42 def is_deterministic(self): 43 return True 44 45 46 class Person(object): 47 "Author of an auction or a bid." 48 CODER = PersonCoder() 49 50 def __init__( 51 self, id, name, email, credit_card, city, state, date_time, extra=None): 52 self.id = id 53 self.name = name 54 self.email_address = email # key 55 self.credit_card = credit_card 56 self.city = city 57 self.state = state 58 self.date_time = date_time 59 self.extra = extra 60 61 def __repr__(self): 62 return nexmark_util.model_to_json(self) 63 64 65 class AuctionCoder(FastCoder): 66 def to_type_hint(self): 67 return Auction 68 69 def _create_impl(self): 70 return AuctionCoderImpl() 71 72 def is_deterministic(self): 73 return True 74 75 76 class Auction(object): 77 "Item for auction." 78 CODER = AuctionCoder() 79 80 def __init__( 81 self, 82 id, 83 item_name, 84 description, 85 initial_bid, 86 reserve_price, 87 date_time, 88 expires, 89 seller, 90 category, 91 extra=None): 92 self.id = id 93 self.item_name = item_name # key 94 self.description = description 95 self.initial_bid = initial_bid 96 self.reserve = reserve_price 97 self.date_time = date_time 98 self.expires = expires 99 self.seller = seller 100 self.category = category 101 self.extra = extra 102 103 def __repr__(self): 104 return nexmark_util.model_to_json(self) 105 106 107 class BidCoder(FastCoder): 108 def to_type_hint(self): 109 return Bid 110 111 def _create_impl(self): 112 return BidCoderImpl() 113 114 def is_deterministic(self): 115 return True 116 117 118 class Bid(object): 119 "A bid for an item for auction." 120 CODER = BidCoder() 121 122 def __init__(self, auction, bidder, price, date_time, extra=None): 123 self.auction = auction # key 124 self.bidder = bidder 125 self.price = price 126 self.date_time = date_time 127 self.extra = extra 128 129 def __repr__(self): 130 return nexmark_util.model_to_json(self) 131 132 133 class AuctionCoderImpl(coder_impl.StreamCoderImpl): 134 _int_coder_impl = coder_impl.VarIntCoderImpl() 135 _str_coder_impl = StrUtf8Coder().get_impl() 136 _time_coder_impl = coder_impl.TimestampCoderImpl() 137 138 def encode_to_stream(self, value, stream, nested): 139 self._int_coder_impl.encode_to_stream(value.id, stream, True) 140 self._str_coder_impl.encode_to_stream(value.item_name, stream, True) 141 self._str_coder_impl.encode_to_stream(value.description, stream, True) 142 self._int_coder_impl.encode_to_stream(value.initial_bid, stream, True) 143 self._int_coder_impl.encode_to_stream(value.reserve, stream, True) 144 self._time_coder_impl.encode_to_stream(value.date_time, stream, True) 145 self._time_coder_impl.encode_to_stream(value.expires, stream, True) 146 self._int_coder_impl.encode_to_stream(value.seller, stream, True) 147 self._int_coder_impl.encode_to_stream(value.category, stream, True) 148 self._str_coder_impl.encode_to_stream(value.extra, stream, True) 149 150 def decode_from_stream(self, stream, nested): 151 id = self._int_coder_impl.decode_from_stream(stream, True) 152 item_name = self._str_coder_impl.decode_from_stream(stream, True) 153 description = self._str_coder_impl.decode_from_stream(stream, True) 154 initial_bid = self._int_coder_impl.decode_from_stream(stream, True) 155 reserve = self._int_coder_impl.decode_from_stream(stream, True) 156 date_time = self._time_coder_impl.decode_from_stream(stream, True) 157 expires = self._time_coder_impl.decode_from_stream(stream, True) 158 seller = self._int_coder_impl.decode_from_stream(stream, True) 159 category = self._int_coder_impl.decode_from_stream(stream, True) 160 extra = self._str_coder_impl.decode_from_stream(stream, True) 161 return Auction( 162 id, 163 item_name, 164 description, 165 initial_bid, 166 reserve, 167 date_time, 168 expires, 169 seller, 170 category, 171 extra) 172 173 174 class BidCoderImpl(coder_impl.StreamCoderImpl): 175 _int_coder_impl = coder_impl.VarIntCoderImpl() 176 _str_coder_impl = StrUtf8Coder().get_impl() 177 _time_coder_impl = coder_impl.TimestampCoderImpl() 178 179 def encode_to_stream(self, value, stream, nested): 180 self._int_coder_impl.encode_to_stream(value.auction, stream, True) 181 self._int_coder_impl.encode_to_stream(value.bidder, stream, True) 182 self._int_coder_impl.encode_to_stream(value.price, stream, True) 183 self._time_coder_impl.encode_to_stream(value.date_time, stream, True) 184 self._str_coder_impl.encode_to_stream(value.extra, stream, True) 185 186 def decode_from_stream(self, stream, nested): 187 auction = self._int_coder_impl.decode_from_stream(stream, True) 188 bidder = self._int_coder_impl.decode_from_stream(stream, True) 189 price = self._int_coder_impl.decode_from_stream(stream, True) 190 date_time = self._time_coder_impl.decode_from_stream(stream, True) 191 extra = self._str_coder_impl.decode_from_stream(stream, True) 192 return Bid(auction, bidder, price, date_time, extra) 193 194 195 class PersonCoderImpl(coder_impl.StreamCoderImpl): 196 _int_coder_impl = coder_impl.VarIntCoderImpl() 197 _str_coder_impl = StrUtf8Coder().get_impl() 198 _time_coder_impl = coder_impl.TimestampCoderImpl() 199 200 def encode_to_stream(self, value, stream, nested): 201 self._int_coder_impl.encode_to_stream(value.id, stream, True) 202 self._str_coder_impl.encode_to_stream(value.name, stream, True) 203 self._str_coder_impl.encode_to_stream(value.email_address, stream, True) 204 self._str_coder_impl.encode_to_stream(value.credit_card, stream, True) 205 self._str_coder_impl.encode_to_stream(value.city, stream, True) 206 self._str_coder_impl.encode_to_stream(value.state, stream, True) 207 self._time_coder_impl.encode_to_stream(value.date_time, stream, True) 208 self._str_coder_impl.encode_to_stream(value.extra, stream, True) 209 210 def decode_from_stream(self, stream, nested): 211 id = self._int_coder_impl.decode_from_stream(stream, True) 212 name = self._str_coder_impl.decode_from_stream(stream, True) 213 email = self._str_coder_impl.decode_from_stream(stream, True) 214 credit_card = self._str_coder_impl.decode_from_stream(stream, True) 215 city = self._str_coder_impl.decode_from_stream(stream, True) 216 state = self._str_coder_impl.decode_from_stream(stream, True) 217 date_time = self._time_coder_impl.decode_from_stream(stream, True) 218 extra = self._str_coder_impl.decode_from_stream(stream, True) 219 return Person(id, name, email, credit_card, city, state, date_time, extra)