首页 技术 正文
技术 2022年11月15日
0 收藏 919 点赞 3,735 浏览 1079 个字
jsonObject.toString():需要处理的json对象转成的字符串
"/testData/ExtractKWByOnce.txt":写入hdfs中的目标路径
try {
Configuration configuration=new Configuration();
InputStream inputStream=new BufferedInputStream(
new ByteArrayInputStream(jsonObject.toString().getBytes("")) );//打开一个BufferedInputStream字节输入流 FileSystem fs=FileSystem.get(URI.create("hdfs://localtion:8020"),configuration);
//这里是creat()方法表示新创建一个文件,如果想在一个文件上追加,请用append()方法。
FSDataOutputStream fsDataOutputStream=fs.create(new Path("/testData/ExtractKWByOnce.txt"));
IOUtils.copyBytes(inputStream,fsDataOutputStream,1024,true); fsDataOutputStream.close();
fs.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

用此方法可能会出现 中文乱码问题,hdfs上的文件和自己期待存入的内容有差异。

可以在 jsonObject.toString().getBytes(“”) 处将我们的编码格式指定为:

jsonObject.toString().getBytes("GBK")

基本可以解决问题

—————————————————————————————————————————-

今日发现,在GBK编码下,从浏览器直接打开hdfs文件不会出现乱码,但是在shell终端通过#>hdfs dfs -cat xxx 命令打开的hdfs文件出现了乱码问题。

将编码改回UTF-8,从浏览器直接打开hdfs文件会出现乱码,但是在shell终端通过#>hdfs dfs -cat xxx 命令打开的hdfs文件不出现乱码。

所以具体选择哪种编码,要看实际需求。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289