启动YARN后,将会启动ResourceManager以及NodeManager进程,可以通过jps命令进行查看。
当YARN启动完毕后,可以访问http://localhost:8088进入YARN的可视化管理界面,可以在此页面中查看任务的执行情况以及资源的分配。
3.5 使用Shell命令操作HDFS
HDFS中的文件系统与Linux类似,由/代表根目录。
如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。
- hadoop fs -cat <src>:显示文件中的内容。
- hadoop fs -copyFromLocal <localsrc> <dst>:将本地中的文件上传到HDFS。
- hadoop fs -copyToLocal <src> <localdst>:将HDFS中的文件下载到本地。
- hadoop fs -count <path>:查询指定路径下文件的个数。
- hadoop fs -cp <src> <dst>:在HDFS内对文件进行复制。
- hadoop fs -get <src> <localdst>:将HDFS中的文件下载到本地。
- hadoop fs -ls <path>:显示指定目录下的内容。
- hadoop fs -mkdir <path>:创建目录。
- hadoop fs -moveFromLocal <localsrc> <dst>:将本地中的文件剪切到HDFS中。
- hadoop fs -moveToLocal <src> <localdst> :将HDFS中的文件剪切到本地中。
- hadoop fs -mv <src> <dst> :在HDFS内对文件进行移动。
- hadoop fs -put <localsrc> <dst>:将本地中的文件上传到HDFS。
- hadoop fs -rm <src>:删除HDFS中的文件。
3.6 JAVA中操作HDFS
- /**
- * @Auther: ZHUANGHAOTANG
- * @Date: 2018/11/6 11:49
- * @Description:
- */
- public class HDFSUtils {
- private static Logger logger = LoggerFactory.getLogger(HDFSUtils.class);
- /**
- * NameNode URL
- */
- private static final String NAMENODE_URL = "192.168.1.80:8020";
- /**
- * HDFS文件系统连接对象
- */
- private static FileSystem fs = null;
- static {
- Configuration conf = new Configuration();
- try {
- fs = FileSystem.get(URI.create(NAMENODE_URL), conf);
- } catch (IOException e) {
- logger.info("初始化HDFS连接失败:{}", e);
- }
- }
- /**
- * 创建目录
- */
- public static void mkdir(String dir) throws Exception {
- dir = NAMENODE_URL + dir;
- if (!fs.exists(new Path(dir))) {
- fs.mkdirs(new Path(dir));
- }
- }
- /**
- * 删除目录或文件
- */
- public static void delete(String dir) throws Exception {
- dir = NAMENODE_URL + dir;
- fs.delete(new Path(dir), true);
- }
- /**
- * 遍历指定路径下的目录和文件
- */
- public static List<String> listAll(String dir) throws Exception {
- List<String> names = new ArrayList<>();
- dir = NAMENODE_URL + dir;
- FileStatus[] files = fs.listStatus(new Path(dir));
- for (FileStatus file : files) {
- if (file.isFile()) { //文件
- names.add(file.getPath().toString());
- } else if (file.isDirectory()) { //目录
- names.add(file.getPath().toString());
- } else if (file.isSymlink()) { //软或硬链接
- names.add(file.getPath().toString());
- }
- }
- return names;
- }
- /**
- * 上传当前服务器的文件到HDFS中
- */
- public static void uploadLocalFileToHDFS(String localFile, String hdfsFile) throws Exception {
- hdfsFile = NAMENODE_URL + hdfsFile;
- Path src = new Path(localFile);
- Path dst = new Path(hdfsFile);
- fs.copyFromLocalFile(src, dst);
- }
- /**
- * 通过流上传文件
- */
- public static void uploadFile(String hdfsPath, InputStream inputStream) throws Exception {
- hdfsPath = NAMENODE_URL + hdfsPath;
- FSDataOutputStream os = fs.create(new Path(hdfsPath));
- BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
- byte[] data = new byte[1024];
- int len;
- while ((len = bufferedInputStream.read(data)) != -1) {
- if (len == data.length) {
- os.write(data);
- } else { //最后一次读取
- byte[] lastData = new byte[len];
- System.arraycopy(data, 0, lastData, 0, len);
- os.write(lastData);
- }
- }
- inputStream.close();
- bufferedInputStream.close();
- os.close();
- }
- /**
- * 从HDFS中下载文件
- */
- public static byte[] readFile(String hdfsFile) throws Exception {
- hdfsFile = NAMENODE_URL + hdfsFile;
- Path path = new Path(hdfsFile);
- if (fs.exists(path)) {
- FSDataInputStream is = fs.open(path);
- FileStatus stat = fs.getFileStatus(path);
- byte[] data = new byte[(int) stat.getLen()];
- is.readFully(0, data);
- is.close();
- return data;
- } else {
- throw new Exception("File Not Found In HDFS");
- }
- }
- }
3.7 执行一个MapReduce任务 (编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|