zoukankan      html  css  js  c++  java
  • java Api 读取HDFS文件内容

    package dao;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.*;
    import java.io.*;
    
    public class HDFSApi {
        /**
         * 读取文件内容
         */
        public static void cat(Configuration conf, String remoteFilePath) throws IOException {
            FileSystem fs = FileSystem.get(conf);
            Path remotePath = new Path(remoteFilePath);
            FSDataInputStream in = fs.open(remotePath);
            BufferedReader d = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while ((line = d.readLine()) != null) {
                String[] strarray = line.split(" ");
                for (int i = 0; i < strarray.length; i++) {
                    System.out.print(strarray[i]);
                    System.out.print(" ");
    
                }
    
                System.out.println(" ");
                // System.out.println(line);
    
                // System.out.print(strarray[0]);
            }
            d.close();
            in.close();
            fs.close();
        }
    
        /**
         * 主函数
         */
        public static void main(String[] args) {
            Configuration conf = new Configuration();
            conf.set("fs.default.name", "hdfs://yt:9000");
            String remoteFilePath = "/hadoop/hadoop1.txt"; // HDFS路径
    
            try {
                System.out.println("读取文件: " + remoteFilePath);
                HDFSApi.cat(conf, remoteFilePath);
                System.out.println("
    读取完成");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    hive sql基础了解
    创建自增字段,修改字段
    flysql 里两种传参的方式
    创建有赞商品资料
    cortable 使用方法
    学会如何使用,pycharm,和gitlanb
    进程和线程
    创建商品资料模板
    SAP 实例 5 CFW Events
    SAP 实例 4 CFW
  • 原文地址:https://www.cnblogs.com/zyt-bg/p/9997752.html
Copyright © 2011-2022 走看看