根据spark

时间:2017-03-07 10:30:44

标签: apache-spark apache-spark-sql apache-spark-2.0 apache-spark-dataset

我正在尝试根据制造商列内容将数据集拆分为不同的数据集。它非常慢
请建议一种改进代码的方法,以便它可以更快地执行并减少Java代码的使用。

    List<Row> lsts= countsByAge.collectAsList();

        for(Row lst:lsts){
             String man=lst.toString();
             man = man.replaceAll("[\\p{Ps}\\p{Pe}]", "");
             Dataset<Row> DF = src.filter("Manufacturer='"+man+"'");
             DF.show();

        }

代码,输入和输出数据集如下所示。

    package org.sparkexample;
    import org.apache.parquet.filter2.predicate.Operators.Column;
    import org.apache.spark.SparkConf;
    import org.apache.spark.api.java.JavaSparkContext;
    import org.apache.spark.sql.Dataset;
    import org.apache.spark.sql.RelationalGroupedDataset;
    import org.apache.spark.sql.Row;
    import org.apache.spark.sql.SQLContext;
    import org.apache.spark.sql.SparkSession;

    import java.util.Arrays;
    import java.util.List;

    import org.apache.spark.api.java.JavaPairRDD;
    import org.apache.spark.api.java.JavaRDD;
            public class GroupBy {

                public static void main(String[] args) {
                    System.setProperty("hadoop.home.dir", "C:\\winutils");
                    JavaSparkContext sc = new JavaSparkContext(new SparkConf().setAppName("SparkJdbcDs").setMaster("local[*]"));
                    SQLContext sqlContext = new SQLContext(sc);
                    SparkSession spark = SparkSession.builder().appName("split datasets").getOrCreate();
                    sc.setLogLevel("ERROR");

                    Dataset<Row> src= sqlContext.read()
                                .format("com.databricks.spark.csv")
                                .option("header", "true")
                                .load("sample.csv");


                    Dataset<Row> unq_manf=src.select("Manufacturer").distinct();
                    List<Row> lsts= unq_manf.collectAsList();

                    for(Row lst:lsts){
                         String man=lst.toString();
                         man = man.replaceAll("[\\p{Ps}\\p{Pe}]", "");
                         Dataset<Row> DF = src.filter("Manufacturer='"+man+"'");
                         DF.show();

                }
            }
        }

        INPUT TABLE-
        +------+------------+--------------------+---+
        |ItemID|Manufacturer|       Category name|UPC|
        +------+------------+--------------------+---+
        |   804|         ael|Brush & Broom Han...|123|
        |   805|         ael|Wheel Brush Parts...|124|
        |   813|         ael|      Drivers Gloves|125|
        |   632|        west|       Pipe Wrenches|126|
        |   804|         bil|     Masonry Brushes|127|
        |   497|        west|   Power Tools Other|128|
        |   496|        west|   Power Tools Other|129|
        |   495|         bil|           Hole Saws|130|
        |   499|         bil|    Battery Chargers|131|
        |   497|        west|   Power Tools Other|132|
        +------+------------+--------------------+---+

        OUTPUT-
        +------------+
        |Manufacturer|
        +------------+
        |         ael|
        |        west|
        |         bil|
        +------------+

        +------+------------+--------------------+---+
        |ItemID|Manufacturer|       Category name|UPC|
        +------+------------+--------------------+---+
        |   804|         ael|Brush & Broom Han...|123|
        |   805|         ael|Wheel Brush Parts...|124|
        |   813|         ael|      Drivers Gloves|125|
        +------+------------+--------------------+---+

        +------+------------+-----------------+---+
        |ItemID|Manufacturer|    Category name|UPC|
        +------+------------+-----------------+---+
        |   632|        west|    Pipe Wrenches|126|
        |   497|        west|Power Tools Other|128|
        |   496|        west|Power Tools Other|129|
        |   497|        west|Power Tools Other|132|
        +------+------------+-----------------+---+

        +------+------------+----------------+---+
        |ItemID|Manufacturer|   Category name|UPC|
        +------+------------+----------------+---+
        |   804|         bil| Masonry Brushes|127|
        |   495|         bil|       Hole Saws|130|
        |   499|         bil|Battery Chargers|131|
        +------+------------+----------------+---+

谢谢

2 个答案:

答案 0 :(得分:0)

在这种情况下你有两个选择:

  1. 首先,您必须收集唯一的制造商值,然后映射 结果数组:

    val df = Seq(("HP", 1), ("Brother", 2), ("Canon", 3), ("HP", 5)).toDF("k", "v")    
    val brands = df.select("k").distinct.collect.flatMap(_.toSeq)
    val BrandArray = brands.map(brand => df.where($"k" <=> brand))
    BrandArray.foreach { x =>
    x.show()
    println("---------------------------------------")
    }
    
  2. 您还可以根据制造商保存数据框。

    df.write.partitionBy("hour").saveAsTable("parquet")

答案 1 :(得分:0)

如果您需要经常根据制造商进行查询,那么最好使用制造商作为分区键来写数据框,而不是按照制造商来拆分数据集/数据框。

如果您仍然希望基于列值之一来分离数据帧,则使用pyspark和spark 2.0+的方法之一可能是-

from pyspark.sql import functions as F

df = spark.read.csv("sample.csv",header=True)

# collect list of manufacturers
manufacturers = df.select('manufacturer').distinct().collect()

# loop through manufacturers to filter df by manufacturers and write it separately 
for m in manufacturers:
    df1 = df.where(F.col('manufacturers')==m[0])
    df1[.repartition(repartition_col)].write.parquet(<write_path>,[write_mode])