Loading data from Azure blob csv file to dataframe

I’m importing data from a SQL table into an Azure Databricks dataframe by exporting it to a CSV file, saving it in Azure Blob Storage, and then loading it into the dataframe. Most records are imported correctly, but for a few, the RecordInsertedDate shows 2023-12-31 in both SQL and the CSV, yet appears as NULL in the dataframe.

Could someone help me understand why this happens? Also, is there a simpler way to import data directly from a SQL table into a dataframe? Here’s the code I’m using:

from pyspark.sql.functions import month, year, current_date, col
g5_df1 = spark. read. format ("csv") 
option ("header", "true")  option ("inferSchema", "true") load ("dbfs:/mnt/userspace/test/output/a.csv")
g5_df1.createOrReplaceTempView("g5_df1")
gs_df1.display() 

from pyspark.sql.functions import month, year, current_date, col gs_df1 = spark.read.format(“csv”)
.option(“header”, “true”)
.option(“inferSchema”, “true”) .load(“dbfs:/mnt/userspace/test/output/a.csv”)
#gs_df1.createOrReplaceTempView(“gs_df1”)

gs_df1.display()

Recognized by Microsoft Azure Collective

3

You can directly read the data from you SQL tables and Load into dataframe and create temporary table.
I have tried the below approach:

from pyspark.sql import SparkSession
import datetime
from multiprocessing import Process
jdbcHostname = "<your SQL SERVER>"
jdbcPort = 1433
jdbcDatabase = "db02"
jdbcUrl = f"jdbc:sqlserver://{jdbcHostname}:{jdbcPort};database={jdbcDatabase}"
connectionProperties = {
    "user": "admin02",
    "password": "Welcome@1"
}
dilip_df = spark.read.jdbc(url=jdbcUrl, table="SampleTable", properties=connectionProperties)
display(dilip_df)

Results:

ID  Name    RecordInsertedDate
1   Sample Name 1   2023-12-31
2   Sample Name 2   2023-12-31

Creating a temporary view:

dilip_df.createOrReplaceTempView("temp_sample_table")
result_df = spark.sql("SELECT * FROM temp_sample_table")
display(result_df)
ID  Name    RecordInsertedDate
1   Sample Name 1   2023-12-31
2   Sample Name 2   2023-12-31

As you mentioned you are seeing NULLs when reading the .CSV file from the ADLS

I have tried the below approach:

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, IntegerType, StringType
import pyspark.sql.functions as F
schema = StructType([
    StructField("ID", IntegerType(), True),
    StructField("Name", StringType(), True),
    StructField("RecordInsertedDate", StringType(), True)
])
csv_file_path = f"abfss://folder02@<Your STORAGE Account>.dfs.core.windows.net/new02/Query 1 (3).csv"
dilip_df2 = spark.read.csv(csv_file_path, header=True, schema=schema)
dilip_df2 = dilip_df2.withColumn(
    "RecordInsertedDate", 
    F.to_date(F.col("RecordInsertedDate"), "MM/dd/yyyy")  
)
display(dilip_df2)

Results:

ID  Name    RecordInsertedDate
1   Sample Name 1   2023-12-31
2   Sample Name 2   2023-12-31
dilip_df2.createOrReplaceTempView("temp_sample_table2")
spark.sql("SELECT * FROM temp_sample_table2").show()

Results:

+---+-------------+------------------+
| ID|         Name|RecordInsertedDate|
+---+-------------+------------------+
|  1|Sample Name 1|        2023-12-31|
|  2|Sample Name 2|        2023-12-31|
+---+-------------+------------------+
  • In the above code defining the schema for the CSV file
  • Reading the CSV file with the specified schema and infer date as string temporarily set as StringType to handle parsing
  • Convert the RecordInsertedDate column to date format after reading the CSV & change the format according to your data
Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật