实际上这是个 HDFS 的工具类部分代码。 首先

public static Configuration configuration = null;
public static FileSystem fileSystem = null;

static {
try {
if (null == configuration) {
configuration = new Configuration();
}
if (null == fileSystem) {
fileSystem = FileSystem.get(URI.create(RockyConstants.HDFS_PATH), configuration);
}
}
catch (IOException e) {
e.printStackTrace()
;
}
}

/**
* 整文件存入 HDFS
*
* @throws Exception
*/
public static boolean putHDFS(String filePath, byte[] info) {
try {
FSDataOutputStream writer = fileSystem.create(
new Path(filePath), true);
writer.write(info);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}


/**
* 断点续传存入
* @throws IOException
*/
public static void continueUpload(String targetPath, byte[] info) throws IOException{
Path fsPath = new Path(targetPath);
// 第一次
if (!fileSystem.exists(fsPath)) {
putHDFS(targetPath,info);
} else {
// 续传
FSDataOutputStream writer = fileSystem.append(fsPath);
writer.write(info);
writer.flush();
writer.close();
}
}






相关文章:

  • 2021-07-27
  • 2022-02-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2021-10-05
  • 2021-08-01
相关资源
相似解决方案