package com.slp.hdfs;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test; import java.io.IOException; /**
* @author sanglp
* @create 2017-12-08 11:26
* @desc hdfs测试
**/
public class TestHdfs { /**
* 正常输出
* i am a girl
* i want to be a super man
* but i cannot still now
* resource下的core-site.xml中s201如果没有在本地映射会报unknow host
* 如果读取的文件不存在会报 File not exists
*/
@Test
public void testSave(){
/**
* 加载操作源码
* static {
* deprecationContext = new AtomicReference(new Configuration.DeprecationContext((Configuration.DeprecationContext)null, defaultDeprecations));
* ClassLoader cL = Thread.currentThread().getContextClassLoader();
* if(cL == null) {
* cL = Configuration.class.getClassLoader();
* }
*
* if(cL.getResource("hadoop-site.xml") != null) {
* LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, mapred-site.xml and hdfs-site.xml to override properties of core-default.xml, mapred-default.xml and hdfs-default.xml respectively");
* }
*
* addDefaultResource("core-default.xml");
* addDefaultResource("core-site.xml");
* }
*/
Configuration configuration = new Configuration();//加载类路径下的文件
try{
FileSystem fs = FileSystem.get(configuration);
Path path = new Path("hdfs://192.168.181.201/user/sanglp/hadoop/hello.txt");//本地未配置s201解析 java.lang.IllegalArgumentException: java.net.UnknownHostException: s201 文件不存在java.io.FileNotFoundException: File does not exist: /user/sanglp/hadoop/hello.txt FSDataInputStream fis = fs.open(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copyBytes(fis,baos,1024);
fis.close();
System.out.print(new String(baos.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 权限配置:
* org.apache.hadoop.security.AccessControlException: Permission denied: user=hadoop, access=WRITE, inode="/user/sanglp/hadoop":sanglp:supergroup:drwxr-xr-x
* hdfs dfs -chmod o+w /user/sanglp/hadoop
*/
@Test
public void testWrite(){
Configuration configuration = new Configuration();
try {
FileSystem fs = FileSystem.get(configuration);
Path path = new Path("hdfs://192.168.181.201/user/sanglp/hello.txt");
FSDataOutputStream fsDataOutputStream = fs.create(new Path("/user/sanglp/hadoop/a.txt"));
fsDataOutputStream.write("how are you".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 定制副本数和blocksize
* 设置块过小
* org.apache.hadoop.ipc.RemoteException(java.io.IOException): Specified block size is less than configured minimum value (dfs.namenode.fs-limits.min-block-size): 5 < 1048576
* hdfs-site.xml
* <property>
* <name>dfs.namenode.fs-limits.min-block-size</name>
* <value>5</value>
* </property> */
@Test
public void testWrite2(){
Configuration configuration = new Configuration();
try {
FileSystem fs = FileSystem.get(configuration);
Path path = new Path("hdfs://192.168.181.201/user/sanglp/hello.txt");
//public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize, short replication, long blockSize)
FSDataOutputStream fsDataOutputStream = fs.create(new Path("/user/sanglp/hadoop/a.txt"),true,1024,(short)2,5);
fsDataOutputStream.write("how are you".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}

  

【大数据系列】使用api修改hadoop的副本数和块大小的更多相关文章

  1. 大数据系列(4)——Hadoop集群VSFTP和SecureCRT安装配置

    前言 经过前三篇文章的介绍,已经通过VMware安装了Hadoop的集群环境,当然,我相信安装的过程肯定遇到或多或少的问题,这些都需要自己解决,解决的过程就是学习的过程,本篇的来介绍几个Hadoop环 ...

  2. 大数据系列(3)——Hadoop集群完全分布式坏境搭建

    前言 上一篇我们讲解了Hadoop单节点的安装,并且已经通过VMware安装了一台CentOS 6.8的Linux系统,咱们本篇的目标就是要配置一个真正的完全分布式的Hadoop集群,闲言少叙,进入本 ...

  3. 大数据系列(2)——Hadoop集群坏境CentOS安装

    前言 前面我们主要分析了搭建Hadoop集群所需要准备的内容和一些提前规划好的项,本篇我们主要来分析如何安装CentOS操作系统,以及一些基础的设置,闲言少叙,我们进入本篇的正题. 技术准备 VMwa ...

  4. 大数据系列(5)——Hadoop集群MYSQL的安装

    前言 有一段时间没写文章了,最近事情挺多的,现在咱们回归正题,经过前面四篇文章的介绍,已经通过VMware安装了Hadoop的集群环境,相关的两款软件VSFTP和SecureCRT也已经正常安装了. ...

  5. 大数据系列(1)——Hadoop集群坏境搭建配置

    前言 关于时下最热的技术潮流,无疑大数据是首当其中最热的一个技术点,关于大数据的概念和方法论铺天盖地的到处宣扬,但其实很多公司或者技术人员也不能详细的讲解其真正的含义或者就没找到能被落地实施的可行性方 ...

  6. 【大数据系列】windows搭建hadoop开发环境

    一.安装JDK配置环境变量 已经安装略过 二.安装eclipse 已经安装略过 三.安装Ant 1.下载http://ant.apache.org/bindownload.cgi 2.解压 3.配置A ...

  7. 【大数据系列】hive修改默认的derby数据库

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml- ...

  8. 大数据系列之并行计算引擎Spark部署及应用

    相关博文: 大数据系列之并行计算引擎Spark介绍 之前介绍过关于Spark的程序运行模式有三种: 1.Local模式: 2.standalone(独立模式) 3.Yarn/mesos模式 本文将介绍 ...

  9. 大数据系列之并行计算引擎Spark介绍

    相关博文:大数据系列之并行计算引擎Spark部署及应用 Spark: Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎. Spark是UC Berkeley AMP lab ( ...

随机推荐

  1. CI框架 -- 核心文件 之 Lang.php(加载语言包)

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class CI_Lang { var $l ...

  2. nodejs基础 -- web模块

    什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL,与客 ...

  3. javapms部署之后首页不能正常显示问题

    今天在ligerui的技术群里看见了javapms,于是就到官网逛了逛 首先要做的就是了解了javapms使用到的技术 然后下载了程序安装包javapms_v1.1_beta(官网下载失败了,就bai ...

  4. php写文件操作

    function writeLog($file, $msg, $mode='a+') { $fp = fopen($file, $mode); if(flock($fp, LOCK_EX)) { fw ...

  5. Js参数值中含有单引号或双引号解决办法

    <script type="text/javascript"> function Display(LoginEmail, UserName, ID) {         ...

  6. 到底什么时候才需要在ObjC的Block中使用weakSelf/strongSelf

    转载,原文: http://blog.lessfun.com/blog/2014/11/22/when-should-use-weakself-and-strongself-in-objc-block ...

  7. vue-router2路由参数注意问题

    1.vue 路由 如果传递 params 定义路由的时候是 /路由名称:id 获取的时候 this.$route.params.id 最后形如 /路由名称/路由参数 传参的时候 params:{ st ...

  8. 关于android 内存的笔记

    原文 https://developer.android.com/training/articles/memory.html 1.慎重使用Service,最好的办法是使用IntentService,一 ...

  9. Centos修改时间显示的时区,将UTC修改为CST

    问题说明: 今天一同事反应,系统的时间不对和正常的时间差8个小时.就登录主机看了下时间 系统时间显示为: # date Fri Dec :: UTC # 备注:查看了下,正好和当前的时间差了8个小时. ...

  10. iOS 使用正则表达式库RegexKitLite的问题

    因为RegexKitLite使用ICU库,所以需要动态链接到/usr/lib/libicucore.dylib库当中去,否则你会得到错误.具体Dynamically linked to /usr/li ...