Skip to content

Commit e3d6509

Browse files
Initial Commit
1 parent 6fbb9c6 commit e3d6509

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

12-MiscDemo/MiscDemo.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from pyspark.sql import SparkSession
2+
from pyspark.sql.functions import col, monotonically_increasing_id, when, expr
3+
from pyspark.sql.types import *
4+
5+
from lib.logger import Log4j
6+
7+
if __name__ == "__main__":
8+
spark = SparkSession \
9+
.builder \
10+
.appName("Misc Demo") \
11+
.master("local[2]") \
12+
.getOrCreate()
13+
14+
logger = Log4j(spark)
15+
16+
data_list = [("Ravi", "28", "1", "2002"),
17+
("Abdul", "23", "5", "81"), # 1981
18+
("John", "12", "12", "6"), # 2006
19+
("Rosy", "7", "8", "63"), # 1963
20+
("Abdul", "23", "5", "81")] # 1981
21+
22+
raw_df = spark.createDataFrame(data_list).toDF("name", "day", "month", "year").repartition(3)
23+
raw_df.printSchema()
24+
25+
final_df = raw_df.withColumn("id", monotonically_increasing_id()) \
26+
.withColumn("day", col("day").cast(IntegerType())) \
27+
.withColumn("month", col("month").cast(IntegerType())) \
28+
.withColumn("year", col("year").cast(IntegerType())) \
29+
.withColumn("year", when(col("year") < 20, col("year") + 2000)
30+
.when(col("year") < 100, col("year") + 1900)
31+
.otherwise(col("year"))) \
32+
.withColumn("dob", expr("to_date(concat(day,'/',month,'/',year), 'd/M/y')")) \
33+
.drop("day", "month", "year") \
34+
.dropDuplicates(["name", "dob"]) \
35+
.sort(expr("dob desc"))
36+
37+
final_df.show()

12-MiscDemo/lib/__init__.py

Whitespace-only changes.

12-MiscDemo/lib/logger.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Log4j:
2+
def __init__(self, spark):
3+
log4j = spark._jvm.org.apache.log4j
4+
5+
root_class = "guru.learningjournal.spark.examples"
6+
conf = spark.sparkContext.getConf()
7+
app_name = conf.get("spark.app.name")
8+
9+
self.logger = log4j.LogManager.getLogger(root_class + "." + app_name)
10+
11+
def warn(self, message):
12+
self.logger.warn(message)
13+
14+
def info(self, message):
15+
self.logger.info(message)
16+
17+
def error(self, message):
18+
self.logger.error(message)
19+
20+
def debug(self, message):
21+
self.logger.debug(message)

12-MiscDemo/log4j.properties

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Set everything to be logged to the console
2+
log4j.rootCategory=WARN, console
3+
4+
# define console appender
5+
log4j.appender.console=org.apache.log4j.ConsoleAppender
6+
log4j.appender.console.target=System.out
7+
log4j.appender.console.layout=org.apache.log4j.PatternLayout
8+
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
9+
10+
#application log
11+
log4j.logger.guru.learningjournal.spark.examples=INFO, console, file
12+
log4j.additivity.guru.learningjournal.spark.examples=false
13+
14+
#define rolling file appender
15+
log4j.appender.file=org.apache.log4j.RollingFileAppender
16+
log4j.appender.file.File=${spark.yarn.app.container.log.dir}/${logfile.name}.log
17+
#define following in Java System
18+
# -Dlog4j.configuration=file:log4j.properties
19+
# -Dlogfile.name=hello-spark
20+
# -Dspark.yarn.app.container.log.dir=app-logs
21+
log4j.appender.file.ImmediateFlush=true
22+
log4j.appender.file.Append=false
23+
log4j.appender.file.MaxFileSize=500MB
24+
log4j.appender.file.MaxBackupIndex=2
25+
log4j.appender.file.layout=org.apache.log4j.PatternLayout
26+
log4j.appender.file.layout.conversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
27+
28+
# Recommendations from Spark template
29+
log4j.logger.org.apache.spark.repl.Main=WARN
30+
log4j.logger.org.spark_project.jetty=WARN
31+
log4j.logger.org.spark_project.jetty.util.component.AbstractLifeCycle=ERROR
32+
log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO
33+
log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INFO
34+
log4j.logger.org.apache.parquet=ERROR
35+
log4j.logger.parquet=ERROR
36+
log4j.logger.org.apache.hadoop.hive.metastore.RetryingHMSHandler=FATAL
37+
log4j.logger.org.apache.hadoop.hive.ql.exec.FunctionRegistry=ERROR
38+

0 commit comments

Comments
 (0)