Skip to content

Instantly share code, notes, and snippets.

@bijukunjummen
Last active March 9, 2023 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijukunjummen/f07fdd36e3aba5134a62a7a94ac69127 to your computer and use it in GitHub Desktop.
Save bijukunjummen/f07fdd36e3aba5134a62a7a94ac69127 to your computer and use it in GitHub Desktop.
package org.bk.gcs;
import com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.List;
class WriteToGcsTest {
@Test
void testWriteToGcsUsingHadoopConnector() throws Exception {
var fileName1 = "someroot/somepath1.txt";
var fileName2 = "someroot/somepath2.txt";
var fileName3 = "someroot/somepath3.txt";
List<String> lines = List.of("one", "two", "three");
Path path = new Path("gs://root-bucket/");
Configuration configuration = getConfiguration();
GoogleHadoopFileSystem fileSystem = createGhfs(path.toUri(), configuration);
writeToFile(fileName1, lines, fileSystem);
writeToFile(fileName2, lines, fileSystem);
writeToFile(fileName3, lines, fileSystem);
}
private void writeToFile(String fileName, List<String> lines, GoogleHadoopFileSystem fileSystem) throws Exception {
Path pathRem = new Path(fileName);
try (FSDataOutputStream fsDataOutputStream = fileSystem.create(pathRem)) {
for (String line : lines) {
fsDataOutputStream.writeBytes(line + "\n");
}
}
}
private GoogleHadoopFileSystem createGhfs(URI path, Configuration config) throws Exception {
GoogleHadoopFileSystem ghfs = new GoogleHadoopFileSystem();
ghfs.initialize(path, config);
return ghfs;
}
private Configuration getConfiguration() {
Configuration configuration = new Configuration();
configuration.set("fs.gs.auth.type", "SERVICE_ACCOUNT_JSON_KEYFILE");
configuration.set("fs.gs.auth.service.account.json.keyfile", "/somecreds.json");
configuration.set("fs.gs.create.items.conflict.check.enable", "false");
return configuration;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment