1、hadoop默認(rèn)臨時(shí)數(shù)據(jù)文件是存儲(chǔ)于Unix的tmp目錄下(cd /tmp 包含hadoop-root等文件),如果不進(jìn)行修改,linux系統(tǒng)重啟后hadoop有可能出現(xiàn)不正?,F(xiàn)象;故需要修改hadoop的臨時(shí)文件存放目錄
2、vim core-site.xml 配置如下,然后重啟hadoop集群,不要對(duì)namenode重新進(jìn)行格式化操作
修改datanode /var/hadoop/dfs/data/current 目錄下VERSION文件的clusterid與namenode一致;然后啟動(dòng)集群正常
在namenode執(zhí)行格式化操作后,會(huì)導(dǎo)致namenode重新生成clusterid,而datanode的clusterID值沒變,
namenode與datanode clusterid不一致導(dǎo)致datanode啟動(dòng)異常;需要手動(dòng)改成與namenode一致
3、測(cè)試時(shí),可以關(guān)閉權(quán)限檢查(否則沒有權(quán)限訪問(wèn)),在namenode節(jié)點(diǎn)添加如下配置
vim hdfs-site.xml
UTF-8
2.7.3
junit
junit
4.12
org.apache.hadoop
hadoop-client
${hadoop.version}
org.apache.hadoop
hadoop-common
${hadoop.version}
org.apache.hadoop
hadoop-hdfs
${hadoop.version}
package com.skcc.hadoop;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.NumberFormat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class HelloHDFS {
public HelloHDFS() {
// TODO Auto-generated constructor stub
}
public static FileSystem getFileSystemInstance() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://172.26.19.40:9000");
FileSystem fileSystem = null;
try {
fileSystem = FileSystem.get(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileSystem;
}
public static void getFileFromHDFS() throws Exception {
//URL 默認(rèn)處理http協(xié)議, FsUrlStreamHandlerFactory 處理hdfs協(xié)議
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
URL url=new URL("hdfs://172.26.19.40:9000/10803060234.txt");
InputStream inputStream= url.openStream();
IOUtils.copyBytes(inputStream, System.out, 4096,true);
}
public static void getFileFromBaiDu() throws IOException {
URL url=new URL("http://skynet.skhynix-cq.com.cn/plusWare/Main.aspx");
InputStream inputStream= url.openStream();
IOUtils.copyBytes(inputStream, System.out, 4096,true);
}
public static void testHadoop() throws Exception {
FileSystem fileSystem = getFileSystemInstance();
Boolean success = fileSystem.mkdirs(new Path("/skcc"));
System.out.println("mkdirs is " + success);
success = fileSystem.exists(new Path("/10803060234.txt"));
System.out.println("file exists is " + success);
success = fileSystem.delete(new Path("/test2.data"),true);
System.out.println("delete dirs is " + success);
success = fileSystem.exists(new Path("/skcc"));
System.out.println("dirs exists is "+ success);
}
public static void uploadFileToHDFS() throws Exception {
FileSystem fileSystem = getFileSystemInstance();
String filename = "/test2.data";
// overwrite ==true
FSDataOutputStream outputStream = fileSystem.create(new Path(filename), true);
FileInputStream fis = new FileInputStream("D:\\2018\\u001.zip");
// IOUtils.copyBytes(fis, outputStream, 4096, true);
long totalLen = fis.getChannel().size();
long tmpSize = 0;
double readPercent = 0;
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(0);
System.out.println("totalLen : " + totalLen + " available : " + fis.available());
byte[] buf = new byte[4096];
int len = fis.read(buf);
while (len != -1) {
tmpSize = tmpSize + len;
String result = numberFormat.format((float)tmpSize / (float)totalLen * 100 );
outputStream.write(buf,0,len);
System.out.println("Upload Percent : " + result + "%");
len = fis.read(buf);
}
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。