Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions examples/cli/src/main/java/io/unitycatalog/cli/TableCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void handle(CommandLine cmd, ApiClient apiClient, String authToken
output = getTable(tablesApi, json);
break;
case CliUtils.READ:
output = readTable(temporaryCredentialsApi, tablesApi, json);
output = readTable(temporaryCredentialsApi, tablesApi, json, cmd);
break;
case CliUtils.WRITE:
output = writeTable(temporaryCredentialsApi, tablesApi, json);
Expand Down Expand Up @@ -220,8 +220,11 @@ private static String getTable(TablesApi tablesApi, JSONObject json)
}

private static String readTable(
TemporaryCredentialsApi temporaryCredentialsApi, TablesApi tablesApi, JSONObject json)
throws ApiException {
TemporaryCredentialsApi temporaryCredentialsApi,
TablesApi tablesApi,
JSONObject json,
CommandLine cmd)
throws ApiException, JsonProcessingException {
String fullTableName = json.getString(CliParams.FULL_NAME.getServerParam());
TableInfo info =
tablesApi.getTable(
Expand All @@ -242,6 +245,15 @@ private static String readTable(
new GenerateTemporaryTableCredential()
.tableId(tableId)
.operation(TableOperation.READ));
boolean jsonOutput =
cmd.hasOption(CliUtils.OUTPUT)
&& ("json".equals(cmd.getOptionValue(CliUtils.OUTPUT))
|| "jsonPretty".equals(cmd.getOptionValue(CliUtils.OUTPUT)));
if (jsonOutput) {
return objectWriter.writeValueAsString(
DeltaKernelUtils.readDeltaTableAsRecords(
info.getStorageLocation(), temporaryCredentials, maxResults));
}
return DeltaKernelUtils.readDeltaTable(
info.getStorageLocation(), temporaryCredentials, maxResults);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import io.unitycatalog.client.model.ColumnInfo;
import io.unitycatalog.client.model.TemporaryCredentials;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -209,6 +211,32 @@ public static String readDeltaTable(
}
}

public static List<Map<String, String>> readDeltaTableAsRecords(
String tablePath, TemporaryCredentials temporaryCredentials, int maxResults) {
Engine engine = getEngine(URI.create(tablePath), temporaryCredentials);
try {
Table table = Table.forPath(engine, substituteSchemeForS3(tablePath));
Snapshot snapshot = table.getLatestSnapshot(engine);
StructType readSchema = snapshot.getSchema();
ScanBuilder scanBuilder = snapshot.getScanBuilder().withReadSchema(readSchema);
List<Row> rowData =
DeltaKernelReadUtils.readData(engine, readSchema, scanBuilder.build(), maxResults);

List<Map<String, String>> records = new ArrayList<>();
for (Row row : rowData) {
Map<String, String> record = new LinkedHashMap<>();
for (int colOrdinal = 0; colOrdinal < readSchema.length(); colOrdinal++) {
String colName = readSchema.at(colOrdinal).getName();
record.put(colName, DeltaKernelReadUtils.getValue(row, colOrdinal));
}
records.add(record);
}
return records;
} catch (Exception e) {
throw new IllegalArgumentException("Failed to read Delta table", e);
}
}

// TODO : INTERVAL, CHAR and NULL, ARRAY, MAP, STRUCT
public static StructType getSchema(List<ColumnInfo> columns) {
StructType structType = new StructType();
Expand Down
Loading