往HDFS 上写文件

public class HdfsFileTools {
    public static FileSystem fs ;
    private static void init(String url,String user) throws IOException {
        Configuration config = new Configuration();
        config.set("fs.defaultFS", url);
        try {
            fs = FileSystem.get(URI.create(url), config,user);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void appendToHdfsFile(String pathString,String contens) throws IOException{
        FSDataOutputStream out;
        Configuration conf = new Configuration();
        init("hdfs://SANDBOX-HADOOP-01.whh.net:8022","bigdata");
        Path path = new Path(pathString);

        if (fs.exists(path))
        {
            out = fs.append(path);
        }
        else {
            out = fs.create(path);

        }
        System.out.println(contens);
        //out.writeChars(contens);
       // out.writeBytes(contens);
      //  out.writeUTF(contens);
        out.write(contens.getBytes());
        fs.close();
    }

我说几点注意事项: 一、appendToHdfsFile的功能是写HDFS文件,当有文件存在的时候追加;不存在的时候是新建文件;(要注意文件的权限问题)app

二、写中文的时候只有FSDataOutputStream.writeUTF()方法能写,可是会在文本前面加2个字节的内容,因此用out.write(contens.getBytes())方法代替;如下是详细解说:编码

2.1 在writeBytes(String s)这个方法上。url

JAVA中的char是16位的,一个char存储一个中文字符,直接用writeBytes方法转换会变为8位,直接致使高8位丢失。从而致使中文乱码。.net

解决方法:code

现转换为字节组,再write写入流。方法以下:字符串

原方法:get

out.writeBytes(string());string

新方法:it

out.write(string.getBytes());io

2.2 writeUTF()写出一个UTF-8编码的字符串前面会加上2个字节的长度标识,以标识接下来的多少个字节是属于本次方法所写入的字节数。

相关文章
相关标签/搜索