Skip to content

Commit 8f351bb

Browse files
authored
Add simple example (#77)
1 parent 94c2d88 commit 8f351bb

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ then you can run the benchmarks with
8686
cargo bench -p hdfs-native --features benchmark
8787
```
8888

89-
The `benchmark` feature is required to expose `minidfs` and the internal erasure coding functions to benchmark.
89+
The `benchmark` feature is required to expose `minidfs` and the internal erasure coding functions to benchmark.
90+
91+
## Running examples
92+
The examples make use of the `minidfs` module to create a simple HDFS cluster to run the example. This requires including the `integration-test` feature to enable the `minidfs` module. Alternatively, if you want to run the example against an existing HDFS cluster you can exclude the `integration-test` feature and make sure your `HADOOP_CONF_DIR` points to a directory with HDFS configs for talking to your cluster.
93+
94+
```bash
95+
cargo run --example simple --features integration-test
96+
```

crates/hdfs-native/examples/simple.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::collections::HashSet;
2+
3+
#[cfg(feature = "integration-test")]
4+
use hdfs_native::minidfs::MiniDfs;
5+
use hdfs_native::{Client, WriteOptions};
6+
7+
#[tokio::main]
8+
async fn main() {
9+
let _ = env_logger::builder().format_timestamp_millis().try_init();
10+
11+
// If using the integration-test feature, create a MiniDFS cluster. Otherwise
12+
// assume the environment has configs pointing to an existing HDFS cluster.
13+
#[cfg(feature = "integration-test")]
14+
let _dfs = MiniDfs::with_features(&HashSet::new());
15+
16+
let client = Client::default();
17+
18+
// Create an empty file
19+
client
20+
.create("/hdfs-native-test", WriteOptions::default())
21+
.await
22+
.unwrap()
23+
.close()
24+
.await
25+
.unwrap();
26+
27+
// List files
28+
let listing = client.list_status("/", false).await.unwrap();
29+
println!("{:?}", listing);
30+
31+
// Get info on a specific file
32+
let status = client.get_file_info("/hdfs-native-test").await.unwrap();
33+
println!("{:?}", status);
34+
35+
// Rename a file
36+
client
37+
.rename("/hdfs-native-test", "/hdfs-native-test2", false)
38+
.await
39+
.unwrap();
40+
41+
// Delete a file
42+
client.delete("/hdfs-native-test2", false).await.unwrap();
43+
44+
// Write to a new file
45+
let mut writer = client
46+
.create("/hdfs-native-write", WriteOptions::default())
47+
.await
48+
.unwrap();
49+
50+
writer.write(vec![1, 2, 3, 4].into()).await.unwrap();
51+
writer.close().await.unwrap();
52+
53+
// Append to an existing file
54+
let mut writer = client.append("/hdfs-native-write").await.unwrap();
55+
56+
writer.write(vec![5, 6, 7, 8].into()).await.unwrap();
57+
writer.close().await.unwrap();
58+
59+
// Read a file
60+
let reader = client.read("/hdfs-native-write").await.unwrap();
61+
let content = reader.read_range(0, reader.file_length()).await.unwrap();
62+
println!("{:?}", content);
63+
64+
client.delete("/hdfs-native-write", false).await.unwrap();
65+
}

0 commit comments

Comments
 (0)