【问题标题】:Copy file to hadoop hdfs using scala?使用scala将文件复制到hadoop hdfs?
【发布时间】:2017-05-18 04:35:32
【问题描述】:

我正在尝试将本地计算机上的文件复制到我的 hdfs。但是,我不确定如何在 scala 中执行此操作,因为我正在编写的脚本当前写入本地 CSV 文件。如何使用 scala 将此文件移动到 HDFS?

编辑: 我现在做了什么:

val hiveServer = new HiveJDBC
    val file =  new File(TMP_DIR, fileName)
    val firstRow = getFirstRow(tableName, hiveServer)
    val restData = getRestData(tableName, hiveServer)
    withPrintWriter(file) { printWriter => 
      printWriter.write(firstRow) 
      printWriter.write("\n")
      printWriter.write(restData)} 

我现在想在 HDFS 中存储“文件”

【问题讨论】:

  • 你能告诉我们你做了什么吗?
  • @Tariq 完成!见上文:)

标签: scala hadoop hdfs


【解决方案1】:

Scala 可以直接调用 Hadoop API。例如,

    val conf = new Configuration()
    val fs= FileSystem.get(conf)
    val output = fs.create(new Path("/your/path"))
    val writer = new PrintWriter(output)
    try {
        writer.write(firstRow) 
        writer.write("\n")
        writer.write(restData)
    }
    finally {
        writer.close()
    }

【讨论】:

    【解决方案2】:

    在run方法中添加代码内容。

    val conf = getConf()
    val hdfs = FileSystem.get(conf)
    val localInputFilePath = arg(0)
    val inputFileName = getFileName(localInputFilePath)
    
    var hdfsDestinationPath = arg(1)
    val hdfsDestFilePath = new Path(hdfsDestinationPath + File.separator + inputFileName)
    
    try {
      val inputStream: InputStream = new FileInputStream(localInputFilePath);
      val fsdos: FSDataOutputStream = hdfs.create(hdfsDestFilePath);
      IOUtils.copyBytes(inputStream, fsdos, conf, true);
    
    } catch {
      case fnfe: FileNotFoundException => fnfe.printStackTrace();
      case ioe: IOException            => ioe.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      相关资源
      最近更新 更多