Skip to content

Commit

Permalink
Encourage try-with-resource (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego authored Jan 31, 2023
1 parent a6877ef commit 4611f0c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 158 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ import org.msgpack.value.ArrayValue;
...

// Create a new TD client by using configurations in $HOME/.td/td.conf
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()){

// Retrieve database and table names
List<TDDatabase> databaseNames = client.listDatabases();
Expand Down Expand Up @@ -148,10 +147,6 @@ client.jobResult(jobId, TDResultFormat.MESSAGE_PACK_GZ, new Function<InputStream

...

}
finally {
// Never forget to close the TDClient.
client.close();
}
```

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/treasuredata/client/model/TDQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public static TDQuery fromString(String s)
public static TDQuery fromObject(JsonNode value)
{
// embulk job have nested json object
try {
StringWriter s = new StringWriter();
try (StringWriter s = new StringWriter()) {
new ObjectMapper().writeValue(s, value);
s.close();
return new TDQuery(s.toString());
}
catch (java.io.IOException e) {
Expand Down
192 changes: 75 additions & 117 deletions src/test/java/com/treasuredata/client/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,40 +98,29 @@ public static void main(String[] args)

public static void createDatabaseExample(String databaseName)
{
TDClient client = TDClient.newClient();

try {
try (TDClient client = TDClient.newClient()) {
client.createDatabase(databaseName);
System.out.print("Database " + databaseName + " is created!");
}
catch (TDClientException e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void deleteDatabaseExample(String databaseName)
{
TDClient client = TDClient.newClient();

try {
try (TDClient client = TDClient.newClient()) {
client.deleteDatabase(databaseName);
System.out.print("Database " + databaseName + " is deleted!");
}
catch (TDClientException e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void submitJobExample()
{
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()) {
// Submit a new Presto query
String jobId = client.submit(TDJobRequest.newPrestoQuery("sample_datasets", "select count(1) cnt from www_access"));

Expand All @@ -154,15 +143,13 @@ public static void submitJobExample()
public Integer apply(InputStream input)
{
int count = 0;
try {
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new GZIPInputStream(input));
try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new GZIPInputStream(input))) {
while (unpacker.hasNext()) {
// Each row of the query result is array type value (e.g., [1, "name", ...])
ArrayValue array = unpacker.unpackValue().asArrayValue();
System.out.println(array);
count++;
}
unpacker.close();
}
catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -174,15 +161,11 @@ public Integer apply(InputStream input)
catch (Exception e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void listDatabasesExample()
{
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()) {
// Retrieve database and table names
List<TDDatabase> databases = client.listDatabases();
TDDatabase db = databases.get(0);
Expand All @@ -194,60 +177,44 @@ public static void listDatabasesExample()
catch (TDClientException e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void createTableExample(String databaseName, String tableName)
{
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()) {
client.createTable(databaseName, tableName);
System.out.println("Table " + tableName + " is created in database " + databaseName);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void deleteTableExample(String databaseName, String tableName)
{
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()) {
client.deleteTable(databaseName, tableName);
System.out.println("Table " + tableName + " is deleted from database " + databaseName);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void updateSchemaExample(String databaseName, String tableName, List<TDColumn> columns)
{
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()) {
client.updateTableSchema(databaseName, tableName, columns);
System.out.println("Updated schema for table " + tableName);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void importDataExample(String databaseName, String tableName)
{
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()) {
File file = File.createTempFile("data", ".msgpack.gz");
System.out.println("File path: " + file.getAbsolutePath());
file.deleteOnExit();
Expand All @@ -266,19 +233,16 @@ public static void importDataExample(String databaseName, String tableName)

ImmutableMapValue mapValue = ValueFactory.newMap(sampleData);

MessagePacker packer = MessagePack.newDefaultPacker(new GZIPOutputStream(new FileOutputStream(file)));
packer.packValue(mapValue);
packer.close();
try (MessagePacker packer = MessagePack.newDefaultPacker(new GZIPOutputStream(new FileOutputStream(file)))) {
packer.packValue(mapValue);
}

client.importFile(databaseName, tableName, file);
System.out.println("Done importing data into table " + tableName);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static File createBulkImportData() throws IOException
Expand All @@ -287,33 +251,30 @@ public static File createBulkImportData() throws IOException
file.deleteOnExit();

Map<Value, Value> mapData = new HashMap<>();

MessagePacker packer = MessagePack.newDefaultPacker(new GZIPOutputStream(new FileOutputStream(file)));
int numberOfRecords = 100;
for (int i = 1; i <= numberOfRecords; i++) {
StringValue timeCol = ValueFactory.newString("time");
StringValue timeColValue = ValueFactory.newString(i + "");
StringValue col1 = ValueFactory.newString("col1");
StringValue col1Value = ValueFactory.newString("value" + i);
StringValue col2 = ValueFactory.newString("col2");
StringValue col2Value = ValueFactory.newString("value2_" + i);

mapData.put(timeCol, timeColValue);
mapData.put(col1, col1Value);
mapData.put(col2, col2Value);

ImmutableMapValue mapValue = ValueFactory.newMap(mapData);
packer.packValue(mapValue);
try (MessagePacker packer = MessagePack.newDefaultPacker(new GZIPOutputStream(new FileOutputStream(file)))) {
int numberOfRecords = 100;
for (int i = 1; i <= numberOfRecords; i++) {
StringValue timeCol = ValueFactory.newString("time");
StringValue timeColValue = ValueFactory.newString(i + "");
StringValue col1 = ValueFactory.newString("col1");
StringValue col1Value = ValueFactory.newString("value" + i);
StringValue col2 = ValueFactory.newString("col2");
StringValue col2Value = ValueFactory.newString("value2_" + i);

mapData.put(timeCol, timeColValue);
mapData.put(col1, col1Value);
mapData.put(col2, col2Value);

ImmutableMapValue mapValue = ValueFactory.newMap(mapData);
packer.packValue(mapValue);
}
}
packer.close();

return file;
}

public static void bulkImportExample(String bulkName, String databaseName, String tableName)
{
TDClient client = TDClient.newClient();
try {
try (TDClient client = TDClient.newClient()) {
File msgpackFile = Example.createBulkImportData();

client.createBulkImportSession(bulkName, databaseName, tableName);
Expand All @@ -336,57 +297,54 @@ public static void bulkImportExample(String bulkName, String databaseName, Strin
catch (Exception e) {
e.printStackTrace();
}
finally {
client.close();
}
}

public static void saveQueryExample()
{
TDClient client = TDClient.newClient();

// Register a new scheduled query
TDSaveQueryRequest query =
TDSavedQuery.newBuilder(
"my_saved_query",
TDJob.Type.PRESTO,
"testdb",
"select 1",
"Asia/Tokyo")
.setCron("40 * * * *")
.setResult("mysql://testuser:[email protected]/somedb/sometable")
.build();

client.saveQuery(query);

// List saved queries
List<TDSavedQuery> savedQueries = client.listSavedQueries();

// Run a saved query
Date scheduledTime = new Date(System.currentTimeMillis());
client.startSavedQuery(query.getName(), scheduledTime);

// Get saved query job history (first page)
TDSavedQueryHistory firstPage = client.getSavedQueryHistory(query.getName());

// Get second page
long from = firstPage.getTo().get();
long to = from + 20;
TDSavedQueryHistory secondPage = client.getSavedQueryHistory(query.getName(), from, to);

// Get result of last job
TDJob lastJob = firstPage.getHistory().get(0);
System.out.println("Last job:" + lastJob);

// Update a saved query
TDSavedQueryUpdateRequest updateRequest =
TDSavedQuery.newUpdateRequestBuilder()
.setQuery("select 2")
.setDelay(3600)
.build();
client.updateSavedQuery("my_saved_query", updateRequest);

// Delete a saved query
client.deleteSavedQuery(query.getName());
try (TDClient client = TDClient.newClient()) {
// Register a new scheduled query
TDSaveQueryRequest query =
TDSavedQuery.newBuilder(
"my_saved_query",
TDJob.Type.PRESTO,
"testdb",
"select 1",
"Asia/Tokyo")
.setCron("40 * * * *")
.setResult("mysql://testuser:[email protected]/somedb/sometable")
.build();

client.saveQuery(query);

// List saved queries
List<TDSavedQuery> savedQueries = client.listSavedQueries();

// Run a saved query
Date scheduledTime = new Date(System.currentTimeMillis());
client.startSavedQuery(query.getName(), scheduledTime);

// Get saved query job history (first page)
TDSavedQueryHistory firstPage = client.getSavedQueryHistory(query.getName());

// Get second page
long from = firstPage.getTo().get();
long to = from + 20;
TDSavedQueryHistory secondPage = client.getSavedQueryHistory(query.getName(), from, to);

// Get result of last job
TDJob lastJob = firstPage.getHistory().get(0);
System.out.println("Last job:" + lastJob);

// Update a saved query
TDSavedQueryUpdateRequest updateRequest =
TDSavedQuery.newUpdateRequestBuilder()
.setQuery("select 2")
.setDelay(3600)
.build();
client.updateSavedQuery("my_saved_query", updateRequest);

// Delete a saved query
client.deleteSavedQuery(query.getName());
}
}
}
6 changes: 1 addition & 5 deletions src/test/java/com/treasuredata/client/TestProxyAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,10 @@ public class TestProxyAccess
static int findAvailablePort()
throws IOException
{
ServerSocket socket = new ServerSocket(0);
try {
try (ServerSocket socket = new ServerSocket(0)) {
int port = socket.getLocalPort();
return port;
}
finally {
socket.close();
}
}

private static final String PROXY_USER = "test";
Expand Down
Loading

0 comments on commit 4611f0c

Please sign in to comment.