更多資料點擊此處
1 基礎知識回顧
1.1 IO
在程式中IO流是阻塞的 , 支持隨機讀取資料 , 不支持修改資料
1 復習 檔案復制
2 隨機讀取資料
3 不能隨機寫資料
/**
* FileName: TestIo
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description: 測驗IO的隨機讀取資料
* 1 作業 復制檔案
*/
public class TestIo {
public static void main(String[] args) throws Exception {
// 1 獲取一個檔案的輸入流
FileInputStream fis = new FileInputStream("d://word.txt");
// 輸出流沒有類似于skip的方法 不能隨機寫資料
FileOutputStream fout = new FileOutputStream("");
// 2 讀取資料
// int read1 = fis.read(); // a 97
// int read2 = fis.read(); // 98
// 跳過指定的位元組
fis.skip(2L); // 2k 1024*2*1024 1k=1024byte
int read3 = fis.read();
// 3 列印
// System.out.println(read1);
// System.out.println(read2);
System.out.println(read3); //101
}
}
1.2 序列化
將記憶體中的物件資料存盤在磁盤上 ,或者是將物件通過網路傳輸 ! 需要物件實作序列化 ,
本質:就是物件轉二進制的規則 , 反序列化 怎么將二進制轉換成對像規則
java中有自己的序列化機制實作介面
將物件持久化到磁盤 持久化 鈍化
將磁盤上的物件資料反序列化成java物件 活化
1.2.1 java的序列化
/**
* FileName: TestSer
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description:
* 思考 : User類能不能不直接實作序列化介面
* 但是能將資料存盤在磁盤上 : 保證存盤的資料比Serializable方式存盤的資料少
* 網路傳輸節省資源
*/
public class TestSer {
public static void main(String[] args) throws Exception {
// 將記憶體物件持久化到磁盤
// 寫出物件流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d://user.user"));
User user = new User();
user.set(1,"王奔",20000);
// 在磁盤中存盤的任何資料都是二進制
oos.writeObject(user);// 110長度 1 王奔 20000
/**
* java中的Serializable介面的序列化在磁盤中存盤的資料有
* 包名 類名 屬性名 資料型別 ... 方便通過反射反序列化方便
* 有很多的冗余資料
*/
oos.close();
// 將磁盤中的物件資料 反序列化成記憶體java物件
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d://user.user"));
User u = (User)ois.readObject();
System.out.println(u);
}
1.2.2 將物件轉換成JSON (String)
https://mvnrepository.com/search maven倉庫下載jar包
/**
* FileName: JsonToDisc
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description:
* 將java物件轉換成Json串 --->寫到磁盤
*/
public class JsonToDisc {
public static void main(String[] args) throws Exception {
User user = new User();
user.set(1,"benB",20000);
String str = JSON.toJSONString(user);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d://user.json"));
oos.writeUTF(str);
oos.close();
}
}
1.2.3 直接寫屬性到磁盤(簡單的型別)
/**
* FileName: FieldsToDisc
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description:
* 靈活
* 方便
* 資料小
* 注意 寫和讀的順序
*/
public class FieldsToDisc {
public static void main(String[] args) throws Exception {
/*User user = new User();
user.set(1,"benB",20000);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d://user.f"));
// 寫出屬性
oos.writeInt(user.getId());
oos.writeUTF(user.getName());
oos.writeDouble(user.getSal());
oos.close();*/
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d://user.f"));
// 讀取資料 按照寫的順序讀 Int UTF Double
int id = ois.readInt();
String name = ois.readUTF();
double sal = ois.readDouble();
System.out.println();
User user = new User();
user.set(id,name,sal) ;
System.out.println(user);
}
}
1.2.4 自定義序列化規則
1 定義一個介面 有讀寫方法
2 要序列化的java類實作介面 重寫讀寫規則(序列化規則)
3 測驗使用 呼叫讀寫方法實作序列化和反序列化
介面
/**
* FileName: Writable
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description: 介面
* 定義兩個方法 寫 讀
* 作用 : 以后 有類要序列化 實作這個介面
* 重寫里面的讀寫方法 (指定了序列化和反序列化的規則)
*/
public interface Writable {
public void write(ObjectOutputStream oos) throws Exception;
public void read(ObjectInputStream ois)throws Exception;
}
實作類
/**
* FileName: Teacher
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description: 要進行序列化和反序列化
* 實作介面 重寫方法
*/
public class Teacher implements Writable{
private int tid ;
private String name ;
private String gender ;
private double faceValue ;
public void set (int tid, String name, String gender, double faceValue) {
this.tid = tid;
this.name = name;
this.gender = gender;
this.faceValue = faceValue;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getFaceValue() {
return faceValue;
}
public void setFaceValue(double faceValue) {
this.faceValue = faceValue;
}
@Override
public String toString() {
return "Teacher{" +
"tid=" + tid +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", faceValue=" + faceValue +
'}';
}
/**
* 序列化
* @param oos
* @throws Exception
*/
@Override
public void write(ObjectOutputStream oos) throws Exception {
oos.writeInt(this.tid);
oos.writeUTF(this.name);
oos.writeUTF(this.gender);
oos.writeDouble(this.faceValue);
oos.close();
}
/**
* 反序列化
* @param ois
* @throws Exception
*/
@Override
public void read(ObjectInputStream ois) throws Exception {
this.tid = ois.readInt();
this.name = ois.readUTF() ;
this.gender = ois.readUTF() ;
this.faceValue = ois.readDouble() ;
ois.close();
}
}
測驗
/**
* FileName: TestMyWrite
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description:
*/
public class TestMyWrite {
public static void main(String[] args) throws Exception {
Teacher teacher = new Teacher();
// teacher.set(1,"wangben","M",99.99);
// 寫出去 序列化
//teacher.write(new ObjectOutputStream(new FileOutputStream("d://teacher2.txt")));
// 讀回來 反序列化
teacher.read(new ObjectInputStream(new FileInputStream("d://teacher2.txt"))) ;
System.out.println(teacher);
}
}
1.3 迭代器
在不知道資料結構和資料條數的情況下使用
比如: 公司中有自己的資料 1#zss:23@M ,提供迭代器 hasNext -- next --> User
package com._51doit.cn.hdp.day01.iter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
/**
* FileName: MyIteratable
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description:
*/
public class MyIteratable implements Iterator<User> {
BufferedReader br;
String line = null;
User user = new User();
{
try {
br = new BufferedReader(new FileReader("d://user.txt"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 當這個方法回傳true的時候 才會執行next方法
*
* @return
*/
@Override
public boolean hasNext() {
boolean flag = false;
try {
line = br.readLine();
if (line != null) {
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
@Override
public User next() {
// 處理每行資料 封裝結果到User中 回傳
//8#fengjie:53@F
String uid = line.split("#")[0];
String name = line.split("#")[1].split(":")[0];
String age = line.split("#")[1].split(":")[1].split("@")[0];
String gender = line.split("#")[1].split(":")[1].split("@")[1];
user.set(uid,name,age,gender);
return user;
}
}
package com._51doit.cn.hdp.day01.iter;
/**
* FileName: User
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description:
*/
public class User {
private String uid ;
private String name ;
private String age ;
private String gender ;
public void set(String uid, String name, String age, String gender) {
this.uid = uid;
this.name = name;
this.age = age;
this.gender = gender;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User{" +
"uid='" + uid + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
/**
* FileName: Test1
* Author: 多易教育-DOIT
* Date: 2020/11/12 0012
* Description:
*/
public class Test1 {
public static void main(String[] args) {
MyIteratable mi = new MyIteratable();
while(mi.hasNext()){
// 回傳的資料始終是一個物件來接收的
System.out.println(mi.next());
}
}
}
大資料背景
hadoop簡介
hadoop是一個大資料的技術(框架)
主要大資料的問題 1) 海量資料的存盤 2) 海量的資料的運算 3)多臺機器的資源調配(存盤資源 , 運算資源)
1 海量資料的存盤 HDFS hadoop distribute filesystem
2 海量的資料的運算 MapReduce運算框架
3 運算資源調度和任務監控平臺 Yarn
4 工具包 Commons
特點
1 高容錯 高可用
2 極易擴容 集群規模擴展 增強存盤能力和計算能力
3 廉價性
HDFS 簡介
分布式檔案系統 hadoop distribute filesystem
檔案系統: 讀寫 讀取資料 上傳資料 洗掉資料 創建檔案夾 移動 復制..... 提供虛擬的訪問目錄 類似于百度云盤
mysql是檔案系統 關系型資料 條資料單位
linuxwindows作業系統中的檔案系統
分布式檔案系統 :
將資料存盤在不同的機器上 ,提供資料的操作
基本功能:
HDFS安裝
1 上傳
[root@linux01 apps]# pwd
/opt/apps

2 解壓
tar -zxvf hadoop-3.1.1.tar.gz

3 配置
vi /opt/apps/hadoop-3.1.1/etc/hadoop/hadoop-env.sh
# variable is REQUIRED on ALL platforms except OS X!
export JAVA_HOME=/opt/apps/jdk1.8.0_141/
vi /opt/apps/hadoop-3.1.1/etc/hadoop/hdfs-site.xml
<!-- 集群的namenode的位置 datanode能通過這個地址注冊-->
<property>
<name>dfs.namenode.rpc-address</name>
<value>linux01:8020</value>
</property>
<!-- namenode存盤元資料的位置 -->
<property>
<name>dfs.namenode.name.dir</name>
<value>/opt/hdpdata/name</value>
</property>
<!-- datanode存盤資料的位置 -->
<property>
<name>dfs.datanode.data.dir</name>
<value>/opt/hdpdata/data</value>
</property>
<!-- secondary namenode機器的位置-->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>linux02:50090</value>
</property>
vi /opt/apps/hadoop-3.1.1/etc/hadoop/core-site.xml
<property>
<name>fs.defaultFS</name>
<value>hdfs://linux01:8020</value>
</property>
4 分發
scp -r hadoop-3.1.1 linux02:$PWD
scp -r hadoop-3.1.1 linux03:$PWD

5 初始化(bin)
在bin目錄下執行
./hadoop namenode -format
在/opt/hdpdata/name
6 啟動(sbin)
在sbin目錄下執行
./hadoop-daemon.sh start namenode
jps 出現Namenode行程
訪問頁面 http://linux01:9870

分別在linux01 linux02 linux03 的sbin目錄下執行
./hadoop-daemon.sh start datanode

7 配置系統環境變數
vi /etc/profile
export JAVA_HOME=/opt/apps/jdk1.8.0_141
export HADOOP_HOME=/opt/apps/hadoop-3.1.1
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
source /etc/profile
8 一鍵啟停

在 etc/hadoop/workers 配置 需要啟動DataNode的機器名
linux01
linux02
linux03
在啟停腳本中宣告用戶 sbin/start-dfs.sh sbin/stop-dfs.sh
#!/usr/bin/env bash
HDFS_DATANODE_USER=root
HADOOP_SECURE_DN_USER=hdfs
HDFS_NAMENODE_USER=root
HDFS_SECONDARYNAMENODE_USER=root
start-dfs.sh
stop-dfs.sh
HDFS客戶端
hdfs dfs -
[root@linux01 bin]# hdfs dfs
Usage: hadoop fs [generic options]
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-checksum <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] [-l] [-d] [-t <thread count>] <localsrc> ... <dst>]
[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] [-e] <path> ...]
[-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] [-v] [-x] <path> ...]
[-expunge]
[-find <path> ... <expression> ...]
[-get [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-getfacl [-R] <path>]
[-getfattr [-R] {-n name | -d} [-e en] <path>]
[-getmerge [-nl] [-skip-empty-file] <src> <localdst>]
[-head <file>]
[-help [cmd ...]]
[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>]
[-renameSnapshot <snapshotDir> <oldName> <newName>]
[-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
[-setfattr {-n name [-v value] | -x name} <path>]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
[-touchz <path> ...]
[-truncate [-w] <length> <path> ...]
[-usage [cmd ...]]
HDFS原理
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/216997.html
標籤:其他
下一篇:ES6陣列面試題
