【问题标题】:How can I define a file naming convention of incoming files in Spark如何在 Spark 中定义传入文件的文件命名约定
【发布时间】:2018-06-27 09:26:50
【问题描述】:

我在 hdfs 中实时接收文件,它们具有相同的命名约定。

id_name_..._timestamp

我能否以某种方式在 spark (scala) 上定义此命名约定,以便稍后将其与 ID 进行比较?

谢谢

【问题讨论】:

    标签: apache-spark hadoop naming convention


    【解决方案1】:

    你使用这样的东西:

    注册udf

    spark.udf()
      .register("get_only_file_name", (String fullPath) -> {
         int lastIndex = fullPath.lastIndexOf("/");
         return fullPath.substring(lastIndex, fullPath.length - 1);
        }, DataTypes.StringType);
    

    导入 org.apache.spark.sql.functions.input_file_name

    #use the udf to get last token(filename) in full path
    Dataset<Row> initialDs = spark.read()
      .option("dateFormat", conf.dateFormat)
      .schema(conf.schema)
      .csv(conf.path)
      .withColumn("input_file_name", get_only_file_name(input_file_name()));
    

    【讨论】:

    • 您好,谢谢!但是我变成了 DF 中的内容和“input_file_name”,实际上我只需要定义命名约定,以便当文件到来时 spark 可以自动识别 ID 在哪里,时间戳在哪里等(可能在 DF 中) .命名约定如下:id_name_..._timestamp
    • 我变成文件名如下: def getFilenames(fullpath: String) = { val dir = new File(fullpath) dir.listFiles.map(_.getName) }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2010-09-22
    • 1970-01-01
    • 2012-12-24
    • 2022-01-11
    • 2021-04-13
    相关资源
    最近更新 更多