-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
databricks-sdk-java/src/main/java/com/databricks/sdk/core/HeaderSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.databricks.sdk.core; | ||
|
||
import com.databricks.sdk.support.QueryParam; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class HeaderSerializer { | ||
public static Map<String, Object> serialize(Object o) { | ||
Map<String, Object> flattened = flattenObject(o); | ||
for (Field f : o.getClass().getDeclaredFields()) { | ||
QueryParam queryParam = f.getAnnotation(QueryParam.class); | ||
if (queryParam == null) { | ||
flattened.remove(getFieldName(f)); | ||
} | ||
} | ||
|
||
return flattened; | ||
} | ||
|
||
private static final List<Class<?>> primitiveTypes = Arrays.asList( | ||
boolean.class, | ||
Boolean.class, | ||
byte.class, | ||
Byte.class, | ||
char.class, | ||
Character.class, | ||
short.class, | ||
Short.class, | ||
int.class, | ||
Integer.class, | ||
long.class, | ||
Long.class, | ||
float.class, | ||
Float.class, | ||
double.class, | ||
Double.class, | ||
String.class | ||
); | ||
|
||
private static String getFieldName(Field f) { | ||
JsonProperty jsonProperty = f.getAnnotation(JsonProperty.class); | ||
if (jsonProperty != null) { | ||
return jsonProperty.value(); | ||
} else { | ||
return f.getName(); | ||
} | ||
} | ||
|
||
private static Map<String, Object> flattenObject(Object o) { | ||
Map<String, Object> result = new HashMap<>(); | ||
Field[] fields = o.getClass().getDeclaredFields(); | ||
for (Field f : fields) { | ||
f.setAccessible(true); | ||
try { | ||
String name = getFieldName(f); | ||
Object value = f.get(o); | ||
if (value == null) { | ||
continue; | ||
} | ||
// check if object is a primitive type | ||
if (primitiveTypes.contains(f.getType()) || Iterable.class.isAssignableFrom(f.getType())) { | ||
result.put(name, value); | ||
} else { | ||
// recursively flatten the object | ||
Map<String, Object> flattened = flattenObject(value); | ||
for (Map.Entry<String, Object> entry : flattened.entrySet()) { | ||
result.put(name + "." + entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
f.setAccessible(false); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
databricks-sdk-java/src/test/java/com/databricks/sdk/integration/SqlIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.databricks.sdk.integration; | ||
|
||
import com.databricks.sdk.WorkspaceClient; | ||
import com.databricks.sdk.integration.framework.EnvContext; | ||
import com.databricks.sdk.integration.framework.EnvTest; | ||
import com.databricks.sdk.service.sql.ListQueryHistoryRequest; | ||
import com.databricks.sdk.service.sql.QueryFilter; | ||
import com.databricks.sdk.service.sql.QueryInfo; | ||
import com.databricks.sdk.service.sql.TimeRange; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@EnvContext("workspace") | ||
@ExtendWith(EnvTest.class) | ||
public class SqlIT { | ||
@Test | ||
void listQueryHistory(WorkspaceClient w) { | ||
TimeRange timeRange = new TimeRange() | ||
.setStartTimeMs(1690243200000L) | ||
.setEndTimeMs(1690329600000L); | ||
ListQueryHistoryRequest request = new ListQueryHistoryRequest() | ||
.setFilterBy(new QueryFilter().setQueryStartTimeRange(timeRange)); | ||
Iterable<QueryInfo> queries = w.queryHistory().list(request); | ||
for (QueryInfo query : queries) { | ||
System.out.println(query); | ||
} | ||
} | ||
} |