Chris Lu
6 years ago
5 changed files with 152 additions and 23 deletions
-
5other/java/hdfs/pom.xml
-
22other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedFileSystem.java
-
41other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedFileSystemStore.java
-
23other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedOutputStream.java
-
78other/java/hdfs/src/main/java/seaweed/hdfs/SeaweedWrite.java
@ -1,18 +1,84 @@ |
|||||
package seaweed.hdfs; |
package seaweed.hdfs; |
||||
|
|
||||
|
import org.apache.hadoop.fs.Path; |
||||
|
import org.apache.http.HttpResponse; |
||||
|
import org.apache.http.client.HttpClient; |
||||
|
import org.apache.http.client.methods.HttpPost; |
||||
|
import org.apache.http.entity.mime.HttpMultipartMode; |
||||
|
import org.apache.http.entity.mime.MultipartEntityBuilder; |
||||
|
import org.apache.http.impl.client.HttpClientBuilder; |
||||
import seaweedfs.client.FilerGrpcClient; |
import seaweedfs.client.FilerGrpcClient; |
||||
import seaweedfs.client.FilerProto; |
import seaweedfs.client.FilerProto; |
||||
|
|
||||
import java.util.List; |
|
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
|
||||
public class SeaweedWrite { |
public class SeaweedWrite { |
||||
public static FilerProto.Entry writeData(final FilerGrpcClient filerGrpcClient, final long offset, |
|
||||
final byte[] bytes, final long bytesOffset, final long bytesLength) { |
|
||||
return null; |
|
||||
|
|
||||
|
public static void writeData(FilerProto.Entry.Builder entry, |
||||
|
final String replication, |
||||
|
final FilerGrpcClient filerGrpcClient, |
||||
|
final long offset, |
||||
|
final byte[] bytes, |
||||
|
final long bytesOffset, final long bytesLength) throws IOException { |
||||
|
FilerProto.AssignVolumeResponse response = filerGrpcClient.getBlockingStub().assignVolume( |
||||
|
FilerProto.AssignVolumeRequest.newBuilder() |
||||
|
.setCollection("") |
||||
|
.setReplication(replication) |
||||
|
.setDataCenter("") |
||||
|
.setReplication("") |
||||
|
.setTtlSec(0) |
||||
|
.build()); |
||||
|
String fileId = response.getFileId(); |
||||
|
String url = response.getUrl(); |
||||
|
String targetUrl = String.format("http://%s/%s", url, fileId); |
||||
|
|
||||
|
String etag = multipartUpload(targetUrl, bytes, bytesOffset, bytesLength); |
||||
|
|
||||
|
entry.addChunks(FilerProto.FileChunk.newBuilder() |
||||
|
.setFileId(fileId) |
||||
|
.setOffset(offset) |
||||
|
.setSize(bytesLength) |
||||
|
.setMtime(System.currentTimeMillis() / 10000L) |
||||
|
.setETag(etag) |
||||
|
); |
||||
|
|
||||
} |
} |
||||
|
|
||||
public static void writeMeta(final FilerGrpcClient filerGrpcClient, |
public static void writeMeta(final FilerGrpcClient filerGrpcClient, |
||||
final String path, final List<FilerProto.Entry> entries) { |
|
||||
return; |
|
||||
|
final Path path, final FilerProto.Entry.Builder entry) { |
||||
|
filerGrpcClient.getBlockingStub().createEntry( |
||||
|
FilerProto.CreateEntryRequest.newBuilder() |
||||
|
.setDirectory(path.getParent().toUri().getPath()) |
||||
|
.setEntry(entry) |
||||
|
.build() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
private static String multipartUpload(String targetUrl, |
||||
|
final byte[] bytes, |
||||
|
final long bytesOffset, final long bytesLength) throws IOException { |
||||
|
|
||||
|
HttpClient client = HttpClientBuilder.create().setUserAgent("hdfs-client").build(); |
||||
|
|
||||
|
InputStream inputStream = new ByteArrayInputStream(bytes, (int) bytesOffset, (int) bytesLength); |
||||
|
|
||||
|
HttpPost post = new HttpPost(targetUrl); |
||||
|
|
||||
|
post.setEntity(MultipartEntityBuilder.create() |
||||
|
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE) |
||||
|
.addBinaryBody("upload", inputStream) |
||||
|
.build()); |
||||
|
|
||||
|
HttpResponse response = client.execute(post); |
||||
|
|
||||
|
String etag = response.getLastHeader("ETag").getValue(); |
||||
|
|
||||
|
if (etag != null && etag.startsWith("\"") && etag.endsWith("\"")) { |
||||
|
etag = etag.substring(1, etag.length() - 1); |
||||
|
} |
||||
|
|
||||
|
return etag; |
||||
} |
} |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue