-
Notifications
You must be signed in to change notification settings - Fork 10
/
UploadDownloadApp.java
129 lines (109 loc) · 5.22 KB
/
UploadDownloadApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.contoso.sample;
import com.microsoft.azure.datalake.store.ADLException;
import com.microsoft.azure.datalake.store.ADLStoreClient;
import com.microsoft.azure.datalake.store.DirectoryEntry;
import com.microsoft.azure.datalake.store.IfExists;
import com.microsoft.azure.datalake.store.oauth2.AccessTokenProvider;
import com.microsoft.azure.datalake.store.oauth2.ClientCredsTokenProvider;
import java.io.*;
import java.util.Arrays;
import java.util.List;
public class UploadDownloadApp {
// This needs to be filled in for the app to work
private static String accountFQDN = "FILL-IN-HERE"; // full account FQDN, not just the account name
private static String clientId = "FILL-IN-HERE";
private static String authTokenEndpoint = "FILL-IN-HERE";
private static String clientKey = "FILL-IN-HERE";
public static void main(String[] args) {
try {
// Create client object using client creds
AccessTokenProvider provider = new ClientCredsTokenProvider(authTokenEndpoint, clientId, clientKey);
ADLStoreClient client = ADLStoreClient.createClient(accountFQDN, provider);
// create directory
client.createDirectory("/a/b/w");
// create file and write some content
String filename = "/a/b/c.txt";
OutputStream stream = client.createFile(filename, IfExists.OVERWRITE );
PrintStream out = new PrintStream(stream);
for (int i = 1; i <= 10; i++) {
out.println("This is line #" + i);
out.format("This is the same line (%d), but using formatted output. %n", i);
}
out.close();
// set file permission
client.setPermission(filename, "744");
// append to file
stream = client.getAppendStream(filename);
stream.write(getSampleContent());
stream.close();
// Read File
InputStream in = client.getReadStream(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ( (line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
System.out.println();
// get file metadata
DirectoryEntry ent = client.getDirectoryEntry(filename);
printDirectoryInfo(ent);
// create another file - this time using a byte array
stream = client.createFile("/a/b/d.txt", IfExists.OVERWRITE);
byte[] buf = getSampleContent();
stream.write(buf);
stream.close();
// concatenate the two files into one
List<String> fileList = Arrays.asList("/a/b/c.txt", "/a/b/d.txt");
client.concatenateFiles("/a/b/f.txt", fileList);
//rename the file
client.rename("/a/b/f.txt", "/a/b/g.txt");
// list directory contents
List<DirectoryEntry> list = client.enumerateDirectory("/a/b", 2000);
System.out.println("Directory listing for directory /a/b:");
for (DirectoryEntry entry : list) {
printDirectoryInfo(entry);
}
// delete directory along with all the subdirectories and files in it
client.deleteRecursive("/a");
} catch (ADLException ex) {
printExceptionDetails(ex);
} catch (Exception ex) {
System.out.format(" Exception: %s%n Message: %s%n", ex.getClass().getName(), ex.getMessage());
}
}
private static void printExceptionDetails(ADLException ex) {
System.out.println("ADLException:");
System.out.format(" Message: %s%n", ex.getMessage());
System.out.format(" HTTP Response code: %s%n", ex.httpResponseCode);
System.out.format(" Remote Exception Name: %s%n", ex.remoteExceptionName);
System.out.format(" Remote Exception Message: %s%n", ex.remoteExceptionMessage);
System.out.format(" Server Request ID: %s%n", ex.requestId);
System.out.println();
}
private static void printDirectoryInfo(DirectoryEntry ent) {
System.out.format("Name: %s%n", ent.name);
System.out.format(" FullName: %s%n", ent.fullName);
System.out.format(" Length: %d%n", ent.length);
System.out.format(" Type: %s%n", ent.type.toString());
System.out.format(" Group: %s%n", ent.group);
System.out.format(" User: %s%n", ent.user);
System.out.format(" Permission: %s%n", ent.permission);
System.out.format(" mtime: %s%n", ent.lastModifiedTime.toString());
System.out.format(" atime: %s%n", ent.lastAccessTime.toString());
System.out.println();
}
private static byte[] getSampleContent() {
ByteArrayOutputStream s = new ByteArrayOutputStream();
PrintStream out = new PrintStream(s);
out.println("This is a line");
out.println("This is another line");
out.println("This is yet another line");
out.println("This is yet yet another line");
out.println("This is yet yet yet another line");
out.println("... and so on, ad infinitum");
out.println();
out.close();
return s.toByteArray();
}
}