<?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>question Re: Not able to split the column into multiple columns in Spark Dataframe in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Not-able-to-split-the-column-into-multiple-columns-in-Spark/m-p/125164#M43264</link>
    <description>&lt;P&gt;You might want to use "com.databricks:spark-csv_2.10:1.5.0", e.g.&lt;/P&gt;&lt;PRE&gt;pyspark --packages "com.databricks:spark-csv_2.10:1.5.0"&lt;/PRE&gt;&lt;P&gt;and then use (the csv file is in the folder /tmp/data2):&lt;/P&gt;&lt;PRE&gt;from pyspark.sql.types import StructType, StructField, DoubleType, StringType
schema = StructType([
  StructField("IP",             StringType()),
  StructField("Time",           StringType()),
  StructField("Request_Type",   StringType()),
  StructField("Response_Code",  StringType()),
  StructField("City",           StringType()),
  StructField("Country",        StringType()),
  StructField("Isocode",        StringType()),
  StructField("Latitude",       DoubleType()),
  StructField("Longitude",      DoubleType())
])

logs_df = sqlContext.read\
                    .format("com.databricks.spark.csv")\
                    .schema(schema)\
                    .option("header", "false")\
                    .option("delimiter", "|")\
                    .load("/tmp/data2")
logs_df.show()&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;PRE&gt;+------------+--------+------------+-------------+------+--------------+-------+--------+---------+
|          IP|    Time|Request_Type|Response_Code|  City|       Country|Isocode|Latitude|Longitude|
+------------+--------+------------+-------------+------+--------------+-------+--------+---------+
|192.168.1.19|13:23:56|         GET|          200|London|United Kingdom|     UK| 51.5074|   0.1278|
|192.168.5.23|09:45:13|        POST|          404|Munich|       Germany|     DE| 48.1351|   11.582|
+------------+--------+------------+-------------+------+--------------+-------+--------+---------+&lt;/PRE&gt;</description>
    <pubDate>Wed, 12 Oct 2016 14:47:46 GMT</pubDate>
    <dc:creator>bwalter1</dc:creator>
    <dc:date>2016-10-12T14:47:46Z</dc:date>
    <item>
      <title>Not able to split the column into multiple columns in Spark Dataframe</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Not-able-to-split-the-column-into-multiple-columns-in-Spark/m-p/125163#M43263</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I want to create a dataframe in Spark and assign proper schema to the data. I have multiple files under one HDFS directory and I am reading all files using the following command:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;%pyspark&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;logs_df =  sqlContext.read.text("hdfs://sandbox.hortonworks.com:8020/tmp/nifioutput")&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;This is creating a dataframe and stores everything in a single column. Next, I want to derive multiple columns from this single column. Typing this:&lt;/P&gt;&lt;P&gt;%pyspark&lt;/P&gt;&lt;P&gt;from pyspark.sql.functions import split, expr&lt;/P&gt;&lt;P&gt;logs_df.select(expr("(split(value, '|'))[0]").cast("string").alias("IP"), expr("(split(value, '|'))[1]").cast("string").alias("Time"), expr("(split(value, '|'))[2]").cast("string").alias("Request_Type"),  expr("(split(value, '|'))[3]").cast("integer").alias("Response_Code"), expr("(split(value, '|'))[4]").cast("string").alias("City"), expr("(split(value, '|'))[5]").cast("string").alias("Country"), expr("(split(value, '|'))[6]").cast("string").alias("Isocode"), expr("(split(value, '|'))[7]").cast("double").alias("Latitude"), expr("(split(value, '|'))[8]").cast("double").alias("Longitude")).show()&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;gives me a strange result. It takes only 1 character from the row instead of using the delimiter (i.e. I) and stores it in different columns.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;+---+----+------------+-------------+----+-------+-------+--------+---------+&lt;/P&gt;&lt;P&gt;| IP|Time|Request_Type|Response_Code|City|Country|Isocode|Latitude|Longitude|&lt;/P&gt;&lt;P&gt;+---+----+------------+-------------+----+-------+-------+--------+---------+&lt;/P&gt;&lt;P&gt;| 1| 3| 3| null| 6| 8| .| 1.0| 8.0|&lt;/P&gt;&lt;P&gt;| 1| 3| 3| null| 6| 8| .| 1.0| 8.0|&lt;/P&gt;&lt;P&gt;| 1| 3| 3| null| 6| 8| .| 1.0| 8.0|&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;As you can see here, each column is taking only 1 character, 133.68.18.180 should be an IP address only. Is this the right way to create multiple columns out of one?  Please help.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;PS - Want to avoid regexp_extract in this.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 09:37:40 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Not-able-to-split-the-column-into-multiple-columns-in-Spark/m-p/125163#M43263</guid>
      <dc:creator>mrizvi</dc:creator>
      <dc:date>2016-10-12T09:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Not able to split the column into multiple columns in Spark Dataframe</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Not-able-to-split-the-column-into-multiple-columns-in-Spark/m-p/125164#M43264</link>
      <description>&lt;P&gt;You might want to use "com.databricks:spark-csv_2.10:1.5.0", e.g.&lt;/P&gt;&lt;PRE&gt;pyspark --packages "com.databricks:spark-csv_2.10:1.5.0"&lt;/PRE&gt;&lt;P&gt;and then use (the csv file is in the folder /tmp/data2):&lt;/P&gt;&lt;PRE&gt;from pyspark.sql.types import StructType, StructField, DoubleType, StringType
schema = StructType([
  StructField("IP",             StringType()),
  StructField("Time",           StringType()),
  StructField("Request_Type",   StringType()),
  StructField("Response_Code",  StringType()),
  StructField("City",           StringType()),
  StructField("Country",        StringType()),
  StructField("Isocode",        StringType()),
  StructField("Latitude",       DoubleType()),
  StructField("Longitude",      DoubleType())
])

logs_df = sqlContext.read\
                    .format("com.databricks.spark.csv")\
                    .schema(schema)\
                    .option("header", "false")\
                    .option("delimiter", "|")\
                    .load("/tmp/data2")
logs_df.show()&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;PRE&gt;+------------+--------+------------+-------------+------+--------------+-------+--------+---------+
|          IP|    Time|Request_Type|Response_Code|  City|       Country|Isocode|Latitude|Longitude|
+------------+--------+------------+-------------+------+--------------+-------+--------+---------+
|192.168.1.19|13:23:56|         GET|          200|London|United Kingdom|     UK| 51.5074|   0.1278|
|192.168.5.23|09:45:13|        POST|          404|Munich|       Germany|     DE| 48.1351|   11.582|
+------------+--------+------------+-------------+------+--------------+-------+--------+---------+&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Oct 2016 14:47:46 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Not-able-to-split-the-column-into-multiple-columns-in-Spark/m-p/125164#M43264</guid>
      <dc:creator>bwalter1</dc:creator>
      <dc:date>2016-10-12T14:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: Not able to split the column into multiple columns in Spark Dataframe</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Not-able-to-split-the-column-into-multiple-columns-in-Spark/m-p/125165#M43265</link>
      <description>&lt;P style="margin-left: 20px;"&gt;Thanks a lot &lt;A rel="user" href="https://community.cloudera.com/users/452/bwalter.html" nodeid="452"&gt;@Bernhard Walter&lt;/A&gt; , that works like a charm&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 01:30:54 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Not-able-to-split-the-column-into-multiple-columns-in-Spark/m-p/125165#M43265</guid>
      <dc:creator>mrizvi</dc:creator>
      <dc:date>2016-10-13T01:30:54Z</dc:date>
    </item>
  </channel>
</rss>

