我做了一個springboot的專案,但是學校要求系統基于hadoop,但是復雜的我又不會。所以我就想能不能只用hdfs來備份資料?我用的是springboot+Mybatis+MySQL。
(1)springboot如何整合hdfs。
(2)如何將資料備份。
對于一個自學能力很差的小白來說,在家一個人寫畢設真的很痛苦。求求各位大佬們支支招,隨便幫幫小弟度過難關。
uj5u.com熱心網友回復:
hdfs是按照檔案存盤的,無法實作類似于Mysql那樣表與表之間的關聯關系,再者hdfs本身就不適用于更新,洗掉單條記錄的操作;你在Mysql環境下 更新表洗掉記錄,
是可以的,hdfs無法實作;就要看你要用hdfs的那些功能來代替mysql,不過從使用場景
上2者完全不同,不存在可替代性.
uj5u.com熱心網友回復:
嗯,我的系統是用excel上傳學生資訊到mysql的,我現在只是想讓我的springboot專案能夠給hdfs上傳檔案,不用mysql導到hdfs中,上傳excel檔案也可以。我只是想把一部分資料保存在hdfs,并不需要其他操作。
uj5u.com熱心網友回復:
(1)springboot如何整合hdfs。首先,在pom.xml添加hadoop-client依賴
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.current.version}</version>
version寫上你安裝的Hadoop版本。
然后,在你定義Bean的xml(建議是xml,最簡單),添加如下Bean定義:
<bean id="hdfsConf" class="org.apache.hadoop.conf.Configuration">
<constructor-arg value="https://bbs.csdn.net/topics/true"/>
</bean>
<bean id="hdfsBaseURL" class="java.lang.String">
<constructor-arg value="hdfs://hadoop master的ip:8020"/>
</bean>
<bean id="hdfsURI" class="java.net.URI">
<constructor-arg ref="hdfsBaseURL"/>
</bean>
<bean id="hdfs" class="org.apache.hadoop.fs.FileSystem" factory-method="get">
<constructor-arg ref="hdfsURI"/>
<constructor-arg ref="hdfsConf"/>
</bean>
然后,在任意Bean(service/dao/controller)就可以依賴hdfs bean了。HDFS寫檔案見下:
@Resource(name = "hdfs")
private FileSystem hdfs;
public void uploadToHDFS(File file) {
try {
byte buffer[] = new byte[1024];
int len;
FSDataOutputStream hdfsFile = hdfs.create(new Path("/user/hdfs/"+file.getName()));
FileInputStream fis = new FileInputStream(file);
while ( (len = fis.read(buffer,0,1024) ) != -1 ) {
hdfsFile.write(buffer,0,len);
hdfsFile.flush();
}
fis.close();hdfsFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
(2)如何將資料備份。
定時任務,把檔案往hdfs丟就是了
uj5u.com熱心網友回復:
依賴寫錯了,是
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.current.version}</version>
</dependency>
uj5u.com熱心網友回復:
謝謝大佬們,已解決轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/71959.html
標籤:分布式計算/Hadoop
