HDFS read and write
-
import java.io.OutputStream;
-
import java.net.URI;
-
import java.net.URL;
-
-
import org.apache.commons.io.IOUtils;
-
import org.apache.hadoop.conf.Configuration;
-
import org.apache.hadoop.fs.FSDataInputStream;
-
import org.apache.hadoop.fs.FileSystem;
-
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
-
import org.apache.hadoop.fs.Path;
-
-
public class HdfsTest {
-
-
public static void main(String[] args) throws Exception {
-
write();
-
}
-
-
public static void read1(String url) throws Exception {
-
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
-
System.out.println(IOUtils.toString(new URL(url).openStream()));
-
}
-
-
public static void read2(String url) throws Exception {
-
Configuration conf = new Configuration();
-
FileSystem fs = FileSystem.get(URI.create(url), conf);
-
FSDataInputStream is = fs.open(new Path(url));
-
System.out.println(IOUtils.toString(is));
-
is.close();
-
fs.close();
-
}
-
-
public static void write() throws Exception {
-
String hdfsFile = "hdfs://192.168.202.134:9000/test/text2.txt";
-
Configuration conf = new Configuration();
-
FileSystem fs = FileSystem.get(URI.create(hdfsFile), conf, "andy");
-
OutputStream out = fs.create(new Path(hdfsFile), true);
-
out.write("new file text2.txt".getBytes());
-
out.close();
-
fs.close();
-
read1(hdfsFile);
-
read2(hdfsFile);
-
}
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10742815/viewspace-2135204/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- write&read&open
- nodejs read/write fileNodeJS
- open physical standby 為read write
- CSS :read-write 選擇器CSS
- pread,pwrite,read,write區別
- direct path read/write等待的分析
- SRAM的Write Assist與Read Assist
- 當從READ ONLY到READ WRITE都做什麼了
- No read or write permission to ORACLE_HOME/.patch_storageOracle
- 表空間的狀態(二) - read/write
- Seed Database (pdb$seed) - Read Write OR Read Only Mode in Oracle Database 12cDatabaseOracle
- Oracle常見等待事件之direct path read/writeOracle事件
- Bypass McAfee Application Control--Write&Read ProtectionAPP
- MongoDB:Read and write access to data and configuration is unrestricted.MongoDBREST
- 【效能調整】等待事件(六) direct path read&write事件
- 解決direct path read 與 direct path write問題
- data file int write和db file sequential read個人想法
- Pure_C_Read and Write PPM、PGM、PBM Three Image File FormatsORM
- mysql 5.5引數--innodb_read(write)_io_threadsMySqlthread
- Linux核心同步機制之(五):Read Write spin lock【轉】Linux
- 證明socket是全雙工,read,write是相互獨立的
- ORA-16433 The database must be opened in read write mode故障解決Database
- mysql [ERROR] InnoDB: ./ibdata1 can't be opened in read-write modeMySqlError
- 如何不使用 trn log 將read only (且能使用trn 恢復)的庫設定為read/write
- Data Guard物理備庫read/write後,切換回備庫狀態
- ZT:oracle10g Data Guard新特性:物理備庫也可以read/writeOracle
- Oracle資料庫表空間READ ONLY、READ WRITE、ONLINE、OFFLINE狀態對應CHECKPOINT的變化Oracle資料庫
- I/O上的等待事件 —— control file sequential read/control file parallel write事件Parallel
- data gurad物理備份方式下以READ ONLY/WRITE模式開啟物理STANDBY模式
- linux系統程式設計之檔案與IO(二):系統呼叫read和writeLinux程式設計
- 關於Cache的write-through & write-back
- MySQL double writeMySql
- hio_write
- [Javascript] Write .call()JavaScript
- Sqoop import載入HBase過程中,遇到Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup:drwxr-xr-xOOPImport
- HDFS 05 - HDFS 常用的 Java API 操作JavaAPI
- hdfs命令
- HDFS原理