This repository has been archived by the owner. It is now read-only.
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge pull request #10 from radibnia77/main
Update lookalike application
- Loading branch information
Showing
16 changed files
with
1,157 additions
and
758 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,31 @@ | ||
score_generator: | ||
input: | ||
log_table : "lookalike_03042021_logs" | ||
did_table: "lookalike_03042021_trainready" | ||
keywords_table: "din_ad_keywords_09172020" | ||
test_table: "lookalike_trainready_jimmy_test" | ||
din_model_tf_serving_url: "http://10.193.217.105:8506/v1/models/lookalike3:predict" | ||
din_model_length: 20 | ||
seeduser_table : "lookalike_seeduser" | ||
number_of_seeduser: 1000 | ||
extend: 2000 | ||
alg: "euclidean" ##### currently just support "euclideand" and "dot" | ||
output: | ||
did_score_table: "lookalike_score_01112021" | ||
score_norm_table: "lookalike_score_norm_01112021" | ||
|
||
score_vector: | ||
keywords_table: "din_ad_keywords_09172020" | ||
score_norm_table: "lookalike_score_norm_01112021" | ||
score_vector_table: "lookalike_score_vector_01112021" | ||
did_bucket_size: 2 | ||
did_bucket_step: 2 | ||
score_vector_rebucketing: | ||
did_bucket_size: 2 | ||
did_bucket_step: 2 | ||
alpha_did_bucket_size: 1000 | ||
score_vector_alpha_table: 'lookalike_score_vector_alpha_01112021' | ||
top_n_similarity: | ||
alpha_did_bucket_step: 100 | ||
top_n: 100 | ||
similarity_table: "lookalike_similarity_01112021" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
spark-submit --executor-memory 16G --driver-memory 24G --num-executors 16 --executor-cores 5 --master yarn --conf spark.driver.maxResultSize=8g seed_user_selector.py config.yml "29" | ||
|
||
spark-submit --executor-memory 16G --driver-memory 24G --num-executors 16 --executor-cores 5 --master yarn --conf spark.driver.maxResultSize=8g score_generator.py config.yml | ||
|
||
spark-submit --master yarn --num-executors 20 --executor-cores 5 --executor-memory 8G --driver-memory 8G --conf spark.driver.maxResultSize=5g --conf spark.hadoop.hive.exec.dynamic.partition=true --conf spark.hadoop.hive.exec.dynamic.partition.mode=nonstrict score_vector_table.py config.yml | ||
|
||
spark-submit --master yarn --num-executors 20 --executor-cores 5 --executor-memory 8G --driver-memory 8G --conf spark.driver.maxResultSize=5g --conf spark.hadoop.hive.exec.dynamic.partition=true --conf spark.hadoop.hive.exec.dynamic.partition.mode=nonstrict score_vector_rebucketing.py config. | ||
|
||
spark-submit --master yarn --num-executors 20 --executor-cores 5 --executor-memory 16G --driver-memory 16G --conf spark.driver.maxResultSize=5g --conf spark.hadoop.hive.exec.dynamic.partition=true --conf spark.hadoop.hive.exec.dynamic.partition.mode=nonstrict top_n_similarity_table_generator.py config.yml | ||
|
||
spark-submit --executor-memory 16G --driver-memory 24G --num-executors 16 --executor-cores 5 --master yarn --conf spark.driver.maxResultSize=8g validation.py config.yml "29" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,103 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0.html | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import yaml | ||
import argparse | ||
from pyspark import SparkContext | ||
from pyspark.sql import HiveContext | ||
from pyspark.sql.functions import lit, col, udf | ||
from pyspark.sql.types import FloatType, StringType, StructType, StructField, ArrayType, MapType, IntegerType | ||
# from rest_client import predict, str_to_intlist | ||
import requests | ||
import json | ||
import argparse | ||
from pyspark.sql.functions import udf | ||
from math import sqrt | ||
import time | ||
import hashlib | ||
|
||
''' | ||
To run, execute the following in application folder. | ||
spark-submit --master yarn --num-executors 20 --executor-cores 5 --executor-memory 8G --driver-memory 8G --conf spark.driver.maxResultSize=5g --conf spark.hadoop.hive.exec.dynamic.partition=true --conf spark.hadoop.hive.exec.dynamic.partition.mode=nonstrict score_vector_rebucketing.py config.yml | ||
This process generates added secondary buckects ids (alpha-did-bucket). | ||
''' | ||
|
||
|
||
def __save_as_table(df, table_name, hive_context, create_table): | ||
|
||
if create_table: | ||
command = """ | ||
DROP TABLE IF EXISTS {} | ||
""".format(table_name) | ||
|
||
hive_context.sql(command) | ||
|
||
df.createOrReplaceTempView("r907_temp_table") | ||
|
||
command = """ | ||
CREATE TABLE IF NOT EXISTS {} as select * from r907_temp_table | ||
""".format(table_name) | ||
|
||
hive_context.sql(command) | ||
|
||
|
||
def assign_new_bucket_id(df, n, new_column_name): | ||
def __hash_sha256(s): | ||
hex_value = hashlib.sha256(s.encode('utf-8')).hexdigest() | ||
return int(hex_value, 16) | ||
_udf = udf(lambda x: __hash_sha256(x) % n, IntegerType()) | ||
df = df.withColumn(new_column_name, _udf(df.did)) | ||
return df | ||
|
||
|
||
def run(hive_context, cfg): | ||
|
||
score_vector_table = cfg['score_vector']['score_vector_table'] | ||
bucket_size = cfg['score_vector_rebucketing']['did_bucket_size'] | ||
bucket_step = cfg['score_vector_rebucketing']['did_bucket_step'] | ||
alpha_bucket_size = cfg['score_vector_rebucketing']['alpha_did_bucket_size'] | ||
score_vector_alpha_table = cfg['score_vector_rebucketing']['score_vector_alpha_table'] | ||
|
||
first_round = True | ||
for start_bucket in range(0, bucket_size, bucket_step): | ||
command = "SELECT did, did_bucket, score_vector FROM {} WHERE did_bucket BETWEEN {} AND {}".format(score_vector_table, start_bucket, start_bucket+bucket_size-1) | ||
|
||
df = hive_context.sql(command) | ||
df = assign_new_bucket_id(df, alpha_bucket_size, 'alpha_did_bucket') | ||
df = df.select('did', 'did_bucket', 'score_vector', 'alpha_did_bucket') | ||
__save_as_table(df, table_name=score_vector_alpha_table, hive_context=hive_context, create_table=first_round) | ||
first_round = False | ||
|
||
|
||
if __name__ == "__main__": | ||
start = time.time() | ||
parser = argparse.ArgumentParser(description='') | ||
parser.add_argument('config_file') | ||
args = parser.parse_args() | ||
with open(args.config_file, 'r') as yml_file: | ||
cfg = yaml.safe_load(yml_file) | ||
|
||
sc = SparkContext.getOrCreate() | ||
sc.setLogLevel('WARN') | ||
hive_context = HiveContext(sc) | ||
|
||
run(hive_context=hive_context, cfg=cfg) | ||
sc.stop() | ||
end = time.time() | ||
print('Runtime of the program is:', (end - start)) |
Oops, something went wrong.