|
@@ -4,11 +4,13 @@ import json
|
4
|
4
|
from sqlite3 import Row
|
5
|
5
|
from typing import Iterable, List
|
6
|
6
|
|
7
|
|
-from pyspark import RDD
|
8
|
|
-
|
9
|
7
|
from pyspark.sql import SparkSession, DataFrame, Row
|
10
|
8
|
from pyspark.sql import functions as F
|
11
|
9
|
|
|
10
|
+import time
|
|
11
|
+start = time.time()
|
|
12
|
+
|
|
13
|
+
|
12
|
14
|
config = json.load(open("./settings.json"))
|
13
|
15
|
debug = config['debug']
|
14
|
16
|
|
|
@@ -63,18 +65,22 @@ class Master:
|
63
|
65
|
.toDF(["tx_group", "index"])
|
64
|
66
|
|
65
|
67
|
def rewrite_cluster_parent(self, cluster_roots: Iterable[str], new_cluster_root: str) -> None:
|
66
|
|
- sqlstr = f"""
|
67
|
|
- UPDATE {self.CLUSTERS_TABLE}
|
68
|
|
- SET parent='{new_cluster_root}'
|
69
|
|
- WHERE parent IN ({','.join(map(lambda r: f"'{r}'", cluster_roots))})"""
|
|
68
|
+ cluster_rewrite = self.spark \
|
|
69
|
+ .table(self.CLUSTERS_TABLE) \
|
|
70
|
+ .where(F.col('parent').isin(cluster_roots)) \
|
|
71
|
+ .select('address') \
|
|
72
|
+ .rdd \
|
|
73
|
+ .map(lambda addr: (addr['address'], new_cluster_root)) \
|
|
74
|
+ .toDF(['address', 'parent']) \
|
70
|
75
|
|
71
|
76
|
if(debug):
|
72
|
|
- print("UPDATE SQL")
|
73
|
|
- print(sqlstr)
|
|
77
|
+ print("REWRITE JOB")
|
|
78
|
+ cluster_rewrite.show(truncate=False, vertical=True)
|
74
|
79
|
print()
|
75
|
80
|
|
76
|
|
- self.spark.sql(sqlstr)
|
77
|
|
-
|
|
81
|
+ cluster_rewrite.writeTo(self.CLUSTERS_TABLE).append()
|
|
82
|
+
|
|
83
|
+
|
78
|
84
|
# end class Master
|
79
|
85
|
|
80
|
86
|
|
|
@@ -104,7 +110,7 @@ for i in range(0, tx_addr_groups.count()):
|
104
|
110
|
|
105
|
111
|
if(debug):
|
106
|
112
|
print("KNOWN CLUSTERS")
|
107
|
|
- cluster_addr_groups.show(truncate=False)
|
|
113
|
+ cluster_addr_groups.show(truncate=True)
|
108
|
114
|
print()
|
109
|
115
|
|
110
|
116
|
tx_addrs: Iterable[str] = tx_groups_indexed \
|
|
@@ -129,7 +135,7 @@ for i in range(0, tx_addr_groups.count()):
|
129
|
135
|
print("cluster_tx_mapping")
|
130
|
136
|
cluster_tx_mapping \
|
131
|
137
|
.toDF(['cluster', 'tx']) \
|
132
|
|
- .show(truncate=False)
|
|
138
|
+ .show(truncate=True)
|
133
|
139
|
print()
|
134
|
140
|
|
135
|
141
|
|
|
@@ -145,7 +151,7 @@ for i in range(0, tx_addr_groups.count()):
|
145
|
151
|
|
146
|
152
|
|
147
|
153
|
if(len(matched_roots) == 0):
|
148
|
|
- new_root = master.insertNewCluster(tx_addrs)
|
|
154
|
+ master.insertNewCluster(tx_addrs)
|
149
|
155
|
elif(len(matched_roots) == 1):
|
150
|
156
|
master.insertNewCluster(tx_addrs, matched_roots[0])
|
151
|
157
|
else:
|
|
@@ -153,4 +159,7 @@ for i in range(0, tx_addr_groups.count()):
|
153
|
159
|
master.insertNewCluster(tx_addrs, matched_roots[0])
|
154
|
160
|
|
155
|
161
|
if(debug):
|
156
|
|
- print("==============")
|
|
162
|
+ print("======================================================================")
|
|
163
|
+
|
|
164
|
+end = time.time()
|
|
165
|
+print("ELAPSED TIME:", end-start)
|