【问题标题】:Exception in thread "main" java.lang.IllegalArgumentException: Field "features" does not exist线程“main”java.lang.IllegalArgumentException 中的异常:字段“features”不存在
【发布时间】:2017-11-14 17:19:03
【问题描述】:
Exception in thread "main" java.lang.IllegalArgumentException: Field "features" does not exist.
    at org.apache.spark.sql.types.StructType$$anonfun$apply$1.apply(StructType.scala:264)
    at org.apache.spark.sql.types.StructType$$anonfun$apply$1.apply(StructType.scala:264)
    at scala.collection.MapLike$class.getOrElse(MapLike.scala:128)
    at scala.collection.AbstractMap.getOrElse(Map.scala:59)
    at org.apache.spark.sql.types.StructType.apply(StructType.scala:263)
    at org.apache.spark.ml.util.SchemaUtils$.checkColumnType(SchemaUtils.scala:40)
    at org.apache.spark.ml.clustering.KMeansParams$class.validateAndTransformSchema(KMeans.scala:92)
    at org.apache.spark.ml.clustering.KMeans.validateAndTransformSchema(KMeans.scala:253)
    at org.apache.spark.ml.clustering.KMeans.transformSchema(KMeans.scala:330)
    at org.apache.spark.ml.PipelineStage.transformSchema(Pipeline.scala:74)
    at org.apache.spark.ml.clustering.KMeans.fit(KMeans.scala:304)
    at sparkExample.spExample.ClusteringDSPOC.main(ClusteringDSPOC.java:45)
17

我的代码是

package sparkExample.spExample;

import java.util.Properties;
import java.util.regex.Pattern;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.ml.clustering.KMeans;
import org.apache.spark.ml.clustering.KMeansModel;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

public class ClusteringDSPOC {

    private static final Pattern SPACE = Pattern.compile(" ");
    private static final SparkContext sc = new SparkContext(new SparkConf().setAppName("SparkJdbcDs").setMaster("local[*]"));
    private static final String POSTGRESQL_DRIVER = "org.postgresql.Driver";
    private static final String POSTGRESQL_USERNAME = "xyz";
    private static final String POSTGRESQL_PWD = "xyz";
    private static final String POSTGRESQL_CONNECTION_URL = "jdbc:postgresql://192.168.111.130:5432/xyzdb?user=" + POSTGRESQL_USERNAME + "&password=" + POSTGRESQL_PWD;
    private static final String POSTGRESQL_TABLE = "(select id, duration from abc where duration is not null ) as abc";

    public static void main(String[] args) throws Exception {
        //Datasource options
        SparkSession spark = SparkSession.builder().appName("JavaKMeansExample").getOrCreate();
        Class.forName(POSTGRESQL_DRIVER);  
        Properties options = new Properties();
        Dataset<Row> sdrDS = spark.read().format("libsvm").jdbc(POSTGRESQL_CONNECTION_URL, POSTGRESQL_TABLE, options);

        Dataset<Row> durationDS = sdrDS.select("duration");


        KMeans kmeans = new KMeans().setK(2).setSeed(1L);
        KMeansModel model = kmeans.fit(durationDS);
    }
}

我正在关注这个 https://spark.apache.org/docs/latest/ml-clustering.html.

在调用 fit 方法时出现此错误。请帮助我解决此问题或其他一些替代选项来执行此操作。谢谢

在这里,我尝试将持续时间划分为 2 到 3 个集群,然后使用 id 映射集群。我可以通过这种方式使用 Spark mllib 库来做同样的事情

package sparkExample.spExample;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.mllib.clustering.KMeans;
import org.apache.spark.mllib.clustering.KMeansModel;
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;

public class ClusteringPOC1 {

    private static final Pattern SPACE = Pattern.compile(" ");
    private static final JavaSparkContext sc = new JavaSparkContext(new SparkConf().setAppName("SparkJdbcDs").setMaster("local[*]"));
    private static final String POSTGRESQL_DRIVER = "org.postgresql.Driver";
    private static final String POSTGRESQL_USERNAME = "abc";
    private static final String POSTGRESQL_PWD = "abc";
    private static final String POSTGRESQL_CONNECTION_URL = "jdbc:postgresql://192.168.111.130:5432/abcdb?user=" + POSTGRESQL_USERNAME + "&password=" + POSTGRESQL_PWD;

    private static final SQLContext sqlContext = new SQLContext(sc);
    public static void main(String[] args) throws Exception {
        //Datasource options
        Map<String, String> options = new HashMap<String, String>();
        options.put("driver", POSTGRESQL_DRIVER);
        options.put("url", POSTGRESQL_CONNECTION_URL);
        options.put("dbtable", "(select id, duration from sdr_log where duration is not null ) as sdr_log");
        Dataset<Row> sdrDF = sqlContext.load("jdbc", options);
        JavaRDD<Row> sdrData = sdrDF.toJavaRDD();
        sdrData.cache();
        JavaRDD<Vector> durationData = sdrData.map(row -> {
            double value = new Double(row.get(2).toString());
            return Vectors.dense(value);
        });
        durationData.cache();

        KMeansModel clusters = KMeans.train(durationData.rdd(), numClusters, numIterations);
        JavaRDD<Integer> clusterLabel =  clusters.predict(durationData);
        JavaRDD<Long> id = sdrData.map(row -> new Long(row.get(1).toString()));
        JavaPairRDD<Long, Integer> clusterLableData = id.zip(clusterLabel);
        clusterLableData.saveAsTextFile("data/mlib/kmeans_output11.txt");

    }
}

但我想用 spark ml 库来做到这一点。

【问题讨论】:

    标签: apache-spark apache-spark-ml


    【解决方案1】:

    K-means 是一种无监督聚类算法,它试图将一组点划分为 K 个集合(簇),以使每个簇中的点趋向于彼此靠近。

        Dataset<Row> durationDS = sdrDS.select("duration");
    

    在您的代码中,您在选择单列“持续时间”时对行进行迭代,并且您将集群数设置为 2。但是当您没有依据时如何将数据分类为集群?

    无监督学习算法(在本例中为 Kmeans)的本质是,您无需在使用数据集时指定与数据集逻辑相关的参数。您只需在模型中传递(拟合)数据集并将其分类为集群。

    在 K-means 算法中,模型试图找到 K-最近邻。它需要一些数据来对集群进行分类,而您只传递一个列。

    最好使用 Spark 的 Dataframe API 来解决您面临的错误。 Spark 自动从 MySQL 表中读取模式并将其类型映射回 Spark SQL 的类型

    导入数据框对象

    > DataFrame jdbcDF= sql.Context.read().format("libsvm").jdbc(POSTGRESQL_CONNECTION_URL,POSTGRESQL_TABLE, options);
    

    您现在可以使用 DF.drop('ColumnName') 函数删除不需要的列。 或者/并以这种方式拟合您的数据集..

    > KMeansModel model = kmeans.fit(jdbcDF);
    

    另外,如果你能提供数据集那就太好了

    【讨论】:

    • 您好 Akshat 感谢您的回复。但我使用的是 2.1.0 版本,没有 DataFrame 类。在我的代码中,如果我使用 KMeansModel model = kmeans.fit(sdrDS) 而不是 KMeansModel model = kmeans.fit(durationDS) 也会遇到同样的问题。跨度>
    • 另外,如果我要使用旧版本 1.6.3 并按照您的方法进行操作,我会在线程“main”org.apache.spark.sql.AnalysisException 中遇到此异常异常:无法解析“功能” ' 给定输入列:[duration];
    • sdrDS.show();给出+---------+--------+ | id|时长| +---------+--------+ |170359216| 10.225| |171133923| 10.008| |171133924| 10.002| |171133925| 10.010| |170359219| 10.228| |170423864| 5.106| |170423865| 5.106| |170423866| 5.106| |170423867| 5.104| |170423868| 5.105| |170423869| 5.104| |170423870| 5.104| |170423871| 5.105| |170423872| 5.104| |170423873| 5.106| |170423874| 5.103| |170423875| 5.106| |170423876| 5.106| |170423877| 5.103| |170423878| 5.107| +---------+--------+
    • durationDs.show() 给出 +--------+ |duration| +--------+ | 10.225| | 10.008| | 10.002| | 10.010| | 10.228| | 5.106| | 5.106| | 5.106| | 5.104| | 5.105| | 5.104| | 5.104| | 5.105| | 5.104| | 5.106| | 5.103| | 5.106| | 5.106| | 5.103| | 5.107| +--------+ 并且都包含列名
    • JavaRDD sdrDS = spark.read().format("libsvm").jdbc(POSTGRESQL_CONNECTION_URL, POSTGRESQL_TABLE, options); StructField[] fields = {new StructField("features", new VectorUDT(), false, Metadata.empty())}; StructType schema = new StructType(fields); DataFrame 数据集 = sqlContext.createDataFrame(sdrDS, schema);尝试这个。该模型正在尝试查找在这种情况下您必须明确创建的特征列。
    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多