-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy path2-Basics.py
379 lines (278 loc) · 15.5 KB
/
2-Basics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# Databricks notebook source
# DBTITLE 1,Install package from PyPi
# MAGIC %pip install dbldatagen
# COMMAND ----------
# MAGIC %md ##Generating a simple data set
# MAGIC
# MAGIC Lets look at generating a simple data set as follows:
# MAGIC
# MAGIC - use the `id` field as the key
# MAGIC - generate a predictable value for a theoretical field `code1` that will be the same on every run.
# MAGIC The field will be generated based on modulo arithmetic on the `id` field to produce a code
# MAGIC - generate random fields `code2`, `code3` and `code4`
# MAGIC - generate a `site code` as a string version of the `site id`
# MAGIC - generate a sector status description
# MAGIC - generate a communications technology field with a discrete set of values
# COMMAND ----------
import dbldatagen as dg
# will have implied column `id` for ordinal of row
testdata_generator = (dg.DataGenerator(spark, name="test_dataset1", rows=100000, partitions=20, randomSeedMethod="hash_fieldname")
.withIdOutput() # id column will be emitted in the output
.withColumn("code1", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code2", "integer", minValue=1, maxValue=20, step=1, random=True)
.withColumn("code3", "integer", minValue=1, maxValue=20, step=1, random=True)
.withColumn("code4", "integer", minValue=1, maxValue=20, step=1, random=True)
# base column specifies dependent column
.withColumn("site_cd", "string", prefix='site', baseColumn='code1')
.withColumn("sector_status_desc", "string", minValue=1, maxValue=200, step=1, prefix='status',
random=True)
.withColumn("tech", "string", values=["GSM", "UMTS", "LTE", "UNKNOWN"], random=True)
.withColumn("test_cell_flg", "integer", values=[0, 1], random=True)
)
df = testdata_generator.build() # build our dataset
df.count()
# COMMAND ----------
display(df)
# COMMAND ----------
# MAGIC %md ### Controlling the starting ID
# MAGIC
# MAGIC Often when we are generating test data, we want multiple data sets and to control how keys are generated for datasets after the first. We can control the generation of the `id` field by specifing the starting `id` - for example to simulate arrival of new data rows
# COMMAND ----------
df = testdata_generator
df2 = testdata_generator.option("startingId", 200000).build() # build our dataset
display(df2)
# COMMAND ----------
# MAGIC %md ### Using weights
# MAGIC
# MAGIC In many cases when we have a series of values for a column, they are not distributed uniformly. By specifying weights, the frequency of generation of specific values can be weighted.
# MAGIC
# MAGIC For example:
# COMMAND ----------
import dbldatagen as dg
# will have implied column `id` for ordinal of row
testdata_generator2 = (dg.DataGenerator(spark, name="test_dataset2", rows=100000, partitions=20,
randomSeedMethod="hash_fieldname")
.withIdOutput() # id column will be emitted in the output
.withColumn("code1", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code2", "integer", minValue=1, maxValue=20, step=1, random=True)
.withColumn("code3", "integer", minValue=1, maxValue=20, step=1, random=True)
.withColumn("code4", "integer", minValue=1, maxValue=20, step=1, random=True)
# base column specifies dependent column
.withColumn("site_cd", "string", prefix='site', baseColumn='code1')
.withColumn("sector_status_desc", "string", minValue=1, maxValue=200, step=1, prefix='status',
random=True)
.withColumn("tech", "string", values=["GSM", "UMTS", "LTE", "UNKNOWN"], weights=[5, 1, 1, 1],
random=True)
.withColumn("test_cell_flg", "integer", values=[0, 1], random=True)
)
df3 = testdata_generator2.build() # build our dataset
display(df3)
# COMMAND ----------
# MAGIC %md ### Generating timestamps
# MAGIC
# MAGIC In many cases when testing ingest pipelines, our test data needs simulated dates or timestamps to simulate the time, date or timestamp to simulate time of ingest or extraction.
# COMMAND ----------
from datetime import timedelta, datetime
from pyspark.sql.types import StructType, StructField, IntegerType, StringType
import dbldatagen as dg
interval = timedelta(days=1, hours=1)
start = datetime(2017, 10, 1, 0, 0, 0)
end = datetime(2018, 10, 1, 6, 0, 0)
schema = StructType([
StructField("site_id", IntegerType(), True),
StructField("site_cd", StringType(), True),
StructField("c", StringType(), True),
StructField("c1", StringType(), True),
StructField("sector_technology_desc", StringType(), True),
])
# build spark session
# will have implied column `id` for ordinal of row
ds = (dg.DataGenerator(spark, name="association_oss_cell_info", rows=100000, partitions=20)
.withSchema(schema)
# withColumnSpec adds specification for existing column
.withColumnSpec("site_id", minValue=1, maxValue=20, step=1)
# base column specifies dependent column
.withIdOutput()
.withColumnSpec("site_cd", prefix='site', baseColumn='site_id')
.withColumn("sector_status_desc", "string", minValue=1, maxValue=200, step=1, prefix='status', random=True)
# withColumn adds specification for new column
.withColumn("rand", "float", expr="floor(rand() * 350) * (86400 + 3600)")
.withColumn("last_sync_dt", "timestamp", begin=start, end=end, interval=interval, random=True)
.withColumnSpec("sector_technology_desc", values=["GSM", "UMTS", "LTE", "UNKNOWN"], random=True)
.withColumn("test_cell_flg", "integer", values=[0, 1], random=True)
)
df = ds.build()
df.printSchema()
# COMMAND ----------
# MAGIC %md ## Generating text data
# MAGIC You can use the included template, prefix, suffix and format mechanisms to generate text
# COMMAND ----------
import dbldatagen as dg
shuffle_partitions_requested = 8
partitions_requested = 8
data_rows = 10000000
spark.sql("""Create table if not exists test_vehicle_data(
name string,
serial_number string,
license_plate string,
email string
) using Delta""")
table_schema = spark.table("test_vehicle_data").schema
print(table_schema)
dataspec = (dg.DataGenerator(spark, rows=10000000, partitions=8,
randomSeedMethod="hash_fieldname")
.withSchema(table_schema))
dataspec = (dataspec
.withColumnSpec("name", percentNulls=0.01, template=r'\\w \\w|\\w a. \\w')
.withColumnSpec("serial_number", minValue=1000000, maxValue=10000000,
prefix="dr", random=True)
.withColumnSpec("email", template=r'\\w.\\w@\\w.com')
.withColumnSpec("license_plate", template=r'\\n-\\n')
)
df1 = dataspec.build()
display(df1)
# COMMAND ----------
# MAGIC %md ### Generating IOT style data
# COMMAND ----------
from pyspark.sql.types import LongType, IntegerType, StringType
import dbldatagen as dg
shuffle_partitions_requested = 8
device_population = 100000
data_rows = 20 * 1000000
partitions_requested = 20
spark.conf.set("spark.sql.shuffle.partitions", shuffle_partitions_requested)
country_codes = ['CN', 'US', 'FR', 'CA', 'IN', 'JM', 'IE', 'PK', 'GB', 'IL', 'AU', 'SG',
'ES', 'GE', 'MX', 'ET', 'SA', 'LB', 'NL']
country_weights = [1300, 365, 67, 38, 1300, 3, 7, 212, 67, 9, 25, 6, 47, 83, 126, 109, 58, 8,
17]
manufacturers = ['Delta corp', 'Xyzzy Inc.', 'Lakehouse Ltd', 'Acme Corp', 'Embanks Devices']
lines = ['delta', 'xyzzy', 'lakehouse', 'gadget', 'droid']
testDataSpec = (dg.DataGenerator(spark, name="device_data_set", rows=data_rows,
partitions=partitions_requested, randomSeedMethod='hash_fieldname')
.withIdOutput()
# we'll use hash of the base field to generate the ids to
# avoid a simple incrementing sequence
.withColumn("internal_device_id", LongType(), minValue=0x1000000000000,
uniqueValues=device_population, omit=True, baseColumnType="hash")
# note for format strings, we must use "%lx" not "%x" as the
# underlying value is a long
.withColumn("device_id", StringType(), format="0x%013x",
baseColumn="internal_device_id")
# the device / user attributes will be the same for the same device id
# so lets use the internal device id as the base column for these attribute
.withColumn("country", StringType(), values=country_codes,
weights=country_weights,
baseColumn="internal_device_id")
.withColumn("manufacturer", StringType(), values=manufacturers,
baseColumn="internal_device_id")
# use omit = True if you don't want a column to appear in the final output
# but just want to use it as part of generation of another column
.withColumn("line", StringType(), values=lines, baseColumn="manufacturer",
baseColumnType="hash", omit=True)
.withColumn("model_ser", IntegerType(), minValue=1, maxValue=11,
baseColumn="device_id",
baseColumnType="hash", omit=True)
.withColumn("model_line", StringType(), expr="concat(line, '#', model_ser)",
baseColumn=["line", "model_ser"])
.withColumn("event_type", StringType(),
values=["activation", "deactivation", "plan change",
"telecoms activity", "internet activity", "device error"],
random=True)
.withColumn("event_ts", "timestamp", begin="2020-01-01 01:00:00", end="2020-12-31 23:59:00", interval="1 minute", random=True)
)
dfTestData = testDataSpec.build()
display(dfTestData)
# COMMAND ----------
# MAGIC %md ##Generating Streaming Data
# MAGIC
# MAGIC We can use the specs from the previous exampled to generate streaming data
# COMMAND ----------
# MAGIC %md IOT example
# COMMAND ----------
import time
time_to_run = 180
from pyspark.sql.types import LongType, IntegerType, StringType
import dbldatagen as dg
device_population = 10000
data_rows = 20 * 100000
partitions_requested = 8
country_codes = ['CN', 'US', 'FR', 'CA', 'IN', 'JM', 'IE', 'PK', 'GB', 'IL', 'AU', 'SG',
'ES', 'GE', 'MX', 'ET', 'SA', 'LB', 'NL']
country_weights = [1300, 365, 67, 38, 1300, 3, 7, 212, 67, 9, 25, 6, 47, 83, 126, 109, 58, 8,
17]
manufacturers = ['Delta corp', 'Xyzzy Inc.', 'Lakehouse Ltd', 'Acme Corp', 'Embanks Devices']
lines = ['delta', 'xyzzy', 'lakehouse', 'gadget', 'droid']
testDataSpec = (dg.DataGenerator(spark, name="device_data_set", rows=data_rows,
partitions=partitions_requested, randomSeedMethod='hash_fieldname',
verbose=True)
.withIdOutput()
# we'll use hash of the base field to generate the ids to
# avoid a simple incrementing sequence
.withColumn("internal_device_id", LongType(), minValue=0x1000000000000,
uniqueValues=device_population, omit=True, baseColumnType="hash")
# note for format strings, we must use "%lx" not "%x" as the
# underlying value is a long
.withColumn("device_id", StringType(), format="0x%013x",
baseColumn="internal_device_id")
# the device / user attributes will be the same for the same device id
# so lets use the internal device id as the base column for these attribute
.withColumn("country", StringType(), values=country_codes,
weights=country_weights,
baseColumn="internal_device_id")
.withColumn("manufacturer", StringType(), values=manufacturers,
baseColumn="internal_device_id")
# use omit = True if you don't want a column to appear in the final output
# but just want to use it as part of generation of another column
.withColumn("line", StringType(), values=lines, baseColumn="manufacturer",
baseColumnType="hash", omit=True)
.withColumn("model_ser", IntegerType(), minValue=1, maxValue=11,
baseColumn="device_id",
baseColumnType="hash", omit=True)
.withColumn("model_line", StringType(), expr="concat(line, '#', model_ser)",
baseColumn=["line", "model_ser"])
.withColumn("event_type", StringType(),
values=["activation", "deactivation", "plan change",
"telecoms activity", "internet activity", "device error"],
random=True)
.withColumn("event_ts", "timestamp", expr="now()")
)
dfTestDataStreaming = testDataSpec.build(withStreaming=True, options={'rowsPerSecond': 500})
display(dfTestDataStreaming)
start_time = time.time()
time.sleep(time_to_run)
# note stopping the stream may produce exceptions - these can be ignored
for x in spark.streams.active:
x.stop()
# COMMAND ----------
# MAGIC %md Site id and technology example
# COMMAND ----------
from datetime import timedelta, datetime
from pyspark.sql.types import StructType, StructField, IntegerType, StringType, FloatType, TimestampType
import dbldatagen as dg
interval = timedelta(days=1, hours=1)
start = datetime(2017, 10, 1, 0, 0, 0)
end = datetime(2018, 10, 1, 6, 0, 0)
schema = StructType([
StructField("site_id", IntegerType(), True),
StructField("site_cd", StringType(), True),
StructField("c", StringType(), True),
StructField("c1", StringType(), True),
StructField("sector_technology_desc", StringType(), True),
])
# will have implied column `id` for ordinal of row
ds = (dg.DataGenerator(spark, name="association_oss_cell_info", rows=100000, partitions=20)
.withSchema(schema)
# withColumnSpec adds specification for existing column
.withColumnSpec("site_id", minValue=1, maxValue=20, step=1)
# base column specifies dependent column
.withIdOutput()
.withColumnSpec("site_cd", prefix='site', baseColumn='site_id')
.withColumn("sector_status_desc", "string", minValue=1, maxValue=200, step=1, prefix='status', random=True)
# withColumn adds specification for new column
.withColumn("rand", "float", expr="floor(rand() * 350) * (86400 + 3600)")
.withColumn("last_sync_dt", "timestamp", begin=start, end=end, interval=interval, random=True)
.withColumnSpec("sector_technology_desc", values=["GSM", "UMTS", "LTE", "UNKNOWN"], random=True)
.withColumn("test_cell_flg", "integer", values=[0, 1], random=True)
)
df = ds.build(withStreaming=True, options={'rowsPerSecond': 500})
display(df)