<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I specify column's data type with spark dataframes? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25140#M17447</link>
    <description>&lt;P&gt;Hi @Raie A​&amp;nbsp; : It will create data frame with wider dataTypes for example Long for (Int/BigInt) etc.. It depends on the use case. If you have created a table and want to overwrite/append data to that then you need to explicitly cast as per your column DataType.  &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;one option is by creating a data class and convert DF as DS&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;case class person(id: Int, name: String)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dfnew.as[person]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 20 Mar 2022 14:45:23 GMT</pubDate>
    <dc:creator>RKNutalapati</dc:creator>
    <dc:date>2022-03-20T14:45:23Z</dc:date>
    <item>
      <title>How do I specify column's data type with spark dataframes?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25139#M17446</link>
      <description>&lt;P&gt;What I am doing:&lt;/P&gt;&lt;P&gt;spark_df = spark.createDataFrame(dfnew)&lt;/P&gt;&lt;P&gt;spark_df.write.saveAsTable("default.test_table", index=False, header=True)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This automatically detects the datatypes and is working right now. BUT, what if the datatype cannot be detected or detects wrong? Mostly concerned about doubles, ints, bigints.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tested casting but it doesnt work on databricks:&lt;/P&gt;&lt;P&gt;spark_df = spark.createDataFrame(dfnew.select(dfnew("Year").cast(IntegerType).as("Year")))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to feed a DDL to spark dataframe for databricks? Should I not use spark to create the table?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 17:24:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25139#M17446</guid>
      <dc:creator>Raie</dc:creator>
      <dc:date>2022-03-18T17:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I specify column's data type with spark dataframes?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25140#M17447</link>
      <description>&lt;P&gt;Hi @Raie A​&amp;nbsp; : It will create data frame with wider dataTypes for example Long for (Int/BigInt) etc.. It depends on the use case. If you have created a table and want to overwrite/append data to that then you need to explicitly cast as per your column DataType.  &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;one option is by creating a data class and convert DF as DS&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;case class person(id: Int, name: String)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dfnew.as[person]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Mar 2022 14:45:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25140#M17447</guid>
      <dc:creator>RKNutalapati</dc:creator>
      <dc:date>2022-03-20T14:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I specify column's data type with spark dataframes?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25141#M17448</link>
      <description>&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;just create table earlier and set column types (CREATE TABLE ... LOCATION ( path path)&lt;/LI&gt;&lt;LI&gt;in dataframe you need to have corresponding data types which you can make using cast syntax, just your syntax is incorrect, here is example of correct syntax:&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql.types import IntegerType
from pyspark.sql.functions import *
&amp;nbsp;
dfnew = spark.createDataFrame([("2022",), ("2021",), ("2020",)], ["Year"])
dfnew = dfnew.withColumn("Year", col("Year").cast(IntegerType()))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Mar 2022 14:47:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25141#M17448</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2022-03-20T14:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: How do I specify column's data type with spark dataframes?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25142#M17449</link>
      <description>&lt;P&gt;Thanks @Hubert Dudek​&amp;nbsp; I changed the syntax and imported all the data types and casting is working!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 17:23:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-do-i-specify-column-s-data-type-with-spark-dataframes/m-p/25142#M17449</guid>
      <dc:creator>Raie</dc:creator>
      <dc:date>2022-03-21T17:23:55Z</dc:date>
    </item>
  </channel>
</rss>

