<?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: Spark Dataframes: How can I change the order of columns in Java/Scala? in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104841#M33397</link>
    <description>&lt;P&gt;There are scenarios(though bad) where data insertion requires the ordering of columns to be in Lexicographical Sorting while inserting data into db using JDBC connection. Not sure if &lt;A href="https://community.hortonworks.com/users/11524/jestinm.html"&gt;jestin ma&lt;/A&gt; is facing similar issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 02 Jul 2016 22:29:16 GMT</pubDate>
    <dc:creator>psingh15</dc:creator>
    <dc:date>2016-07-02T22:29:16Z</dc:date>
    <item>
      <title>Spark Dataframes: How can I change the order of columns in Java/Scala?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104837#M33393</link>
      <description>&lt;P&gt;After joining two dataframes, I find that the column order has changed what I supposed it would be.&lt;/P&gt;&lt;P&gt;Ex: Joining two data frames with columns &lt;CODE&gt;[b,c,d,e]&lt;/CODE&gt; and &lt;CODE&gt;[a,b]&lt;/CODE&gt; on &lt;CODE&gt;b&lt;/CODE&gt; yields a column order of &lt;CODE&gt;[b,a,c,d,e]&lt;/CODE&gt;. &lt;/P&gt;&lt;P&gt;How can I change the order of the columns (e.g., &lt;CODE&gt;[a,b,c,d,e]&lt;/CODE&gt;)? I've found ways to do it in Python/R but not Scala or Java. Are there any methods that allow swapping or reordering of dataframe columns?&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2016 02:31:24 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104837#M33393</guid>
      <dc:creator>jestinm</dc:creator>
      <dc:date>2016-06-30T02:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: Spark Dataframes: How can I change the order of columns in Java/Scala?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104838#M33394</link>
      <description>&lt;P&gt;Your sorting should happens on the basis of the key, here is an example for scala.&lt;/P&gt;&lt;PRE&gt;val file = sc.textFile("some_local_text_file_pathname")
val wordCounts = file.flatMap(line =&amp;gt; line.split(" "))
  .map(word =&amp;gt; (word, 1))
  .reduceByKey(_ + _, 1)  // 2nd arg configures one task (same as number of partitions)
  .map(item =&amp;gt; item.swap) // interchanges position of entries in each tuple
  .sortByKey(true, 1) // 1st arg configures ascending sort, 2nd arg configures one task
  .map(item =&amp;gt; item.swap)



&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2016 02:38:36 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104838#M33394</guid>
      <dc:creator>jyadav</dc:creator>
      <dc:date>2016-06-30T02:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: Spark Dataframes: How can I change the order of columns in Java/Scala?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104839#M33395</link>
      <description>&lt;P&gt;@Jestin: Why do you need sorting columns in dataframes? Could u please elaborate.&lt;/P&gt;&lt;P&gt;However in Java there is no inbuilt function to reorder the columns.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2016 20:07:25 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104839#M33395</guid>
      <dc:creator>psingh15</dc:creator>
      <dc:date>2016-07-02T20:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Spark Dataframes: How can I change the order of columns in Java/Scala?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104840#M33396</link>
      <description>&lt;P&gt;why does the order of columns matter?&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2016 20:47:29 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104840#M33396</guid>
      <dc:creator>TimothySpann</dc:creator>
      <dc:date>2016-07-02T20:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: Spark Dataframes: How can I change the order of columns in Java/Scala?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104841#M33397</link>
      <description>&lt;P&gt;There are scenarios(though bad) where data insertion requires the ordering of columns to be in Lexicographical Sorting while inserting data into db using JDBC connection. Not sure if &lt;A href="https://community.hortonworks.com/users/11524/jestinm.html"&gt;jestin ma&lt;/A&gt; is facing similar issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2016 22:29:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104841#M33397</guid>
      <dc:creator>psingh15</dc:creator>
      <dc:date>2016-07-02T22:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: Spark Dataframes: How can I change the order of columns in Java/Scala?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104842#M33398</link>
      <description>&lt;P&gt;In order to reorder tuples (columns) in scala I think you just use a map like in Pyspark:&lt;/P&gt;&lt;PRE&gt;val rdd2 = rdd.map((x, y, z) =&amp;gt; (z, y, x)) &lt;/PRE&gt;&lt;P&gt;You should also be able to build key-value pairs this way too.&lt;/P&gt;&lt;PRE&gt;val rdd2 = rdd.map((x, y, z) =&amp;gt; (z, (y, x)))&lt;/PRE&gt;&lt;P&gt;This is very handy if you want to follow it up with sortByKey().&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jul 2016 10:18:58 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104842#M33398</guid>
      <dc:creator>don_jernigan</dc:creator>
      <dc:date>2016-07-03T10:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: Spark Dataframes: How can I change the order of columns in Java/Scala?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104843#M33399</link>
      <description>&lt;P&gt;All you need to do is use select (worked for me). Do the following:&lt;/P&gt;&lt;P&gt;val new_df = df.select("a", "b", "c", "d", "e") // Assuming you want a, b, c, d, e to be your order&lt;/P&gt;&lt;P&gt;@venki2404&lt;/P&gt;,&lt;P&gt;All you need to do do is use select (worked for me). Do the following:&lt;/P&gt;&lt;P&gt;val new_df = df.select("a", "b", "c", "d", "e") // assuming the column order you need is a, b, c, d, e&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2016 23:39:33 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Spark-Dataframes-How-can-I-change-the-order-of-columns-in/m-p/104843#M33399</guid>
      <dc:creator>venki2404</dc:creator>
      <dc:date>2016-07-15T23:39:33Z</dc:date>
    </item>
  </channel>
</rss>

