Java Spark flatMap似乎在丢失ArrayList中的项目

时间:2016-02-29 23:46:04

标签: java arraylist apache-spark cassandra datastax-java-driver

我使用spark / cassandra驱动程序在cassandra中迭代数十亿行并提取数据以运行统计信息。为了实现这一点,我在每一行数据上运行FOR循环,如果它符合一桶数据的标准,我就可以调用" channel"然后我以K,V对通道,电源的形式将它添加到ArrayList。

[[频道,电力]]

根据for循环的迭代增量,通道应该是静态的。例如,如果我的通道范围是0到10,增量为2,则通道将为0,2,4,6,8,10

FOR循环在当前数据行上运行,并检查数据是否属于通道,如果是,则将其添加到ArrayList数据中,格式为 [[通道,功率]

然后进入下一行并做同样的事情。一旦它遍历所有行,它就会递增到下一个通道并重复该过程。

问题是有数十亿行符合同一频道的资格,因此我不确定我是否应该使用ArrayListflatMap或其他内容,因为我的结果是每次运行时都会略有不同,并且通道不应该是静态的。

一小部分数据[[Channel,Power]]将是:

[[2,5]]
[[2,10]]
[[2,5]]
[[2,15]]
[[2,5]]

请注意,由于我在每个频道上运行min,max,average stats,因此我需要保留重复的项目。

频道2:最低5,最高15,平均8

我的代码如下:

JavaRDD<MeasuredValue> rdd = javaFunctions(sc).cassandraTable("SparkTestB", "Measured_Value", mapRowTo )
            .select("Start_Frequency","Bandwidth","Power");
    JavaRDD<Value> valueRdd = rdd.flatMap(new FlatMapFunction<MeasuredValue, Value>(){
      @Override
      public Iterable<Value> call(MeasuredValue row) throws Exception {
        long start_frequency = row.getStart_frequency();
        float power = row.getPower();
        long bandwidth = row.getBandwidth();

        // Define Variable
        long channel,channel_end, increment; 

        // Initialize Variables
        channel_end = 10;
        increment = 2;

        List<Value> list = new ArrayList<>();
        // Create Channel Power Buckets
        for(channel = 0; channel <= channel_end; ){
          if( (channel >= start_frequency) && (channel <= (start_frequency + bandwidth)) ) {
            list.add(new Value(channel, power));
          } // end if
          channel+=increment;
        } // end for 
        return list; 
      }
    });

     sqlContext.createDataFrame(valueRdd, Value.class).groupBy(col("channel"))
     .agg(min("power"), max("power"), avg("power"))
     .write().mode(SaveMode.Append)     
     .option("table", "results")
     .option("keyspace", "model")
     .format("org.apache.spark.sql.cassandra").save();

我的类是反思的结果:

public class Value implements Serializable {
    public Value(Long channel, Float power) {
        this.channel = channel;
        this.power = power;
    }
    Long channel;
    Float power;

    public void setChannel(Long channel) {
        this.channel = channel;
    }
    public void setPower(Float power) {
        this.power = power;
    }
    public Long getChannel() {
        return channel;
    }
    public Float getPower() {
        return power;
    }

    @Override
    public String toString() {
        return "[" +channel +","+power+"]";
    }
}

public static class MeasuredValue implements Serializable {
        public MeasuredValue() { }

        public long start_frequency;
        public long getStart_frequency() { return start_frequency; }
        public void setStart_frequency(long start_frequency) { this.start_frequency = start_frequency; }

        public long bandwidth ;
        public long getBandwidth() { return bandwidth; }
        public void setBandwidth(long bandwidth) { this.bandwidth = bandwidth; }

        public float power;    
        public float getPower() { return power; }
        public void setPower(float power) { this.power = power; }

    }

1 个答案:

答案 0 :(得分:0)

我发现我的渠道化算法存在差异。我用以下代替了解决问题。

        // Create Channel Power Buckets
        for(; channel <= channel_end; channel+=increment ){ 
            //Initial Bucket
            while((start_frequency >= channel) && (start_frequency < (channel + increment))){
                list.add(new Value(channel, power));
                channel+=increment;
            }
            //Buckets to Accomodate for Bandwidth
            while ((channel <= channel_end) && (channel >= start_frequency) && (start_frequency + bandwidth) >= channel){
                list.add(new Value(channel, power));                           
                channel+=increment;
            }                   
        }