Skip to content

Commit

Permalink
#27 Find all rows from a single table
Browse files Browse the repository at this point in the history
  • Loading branch information
baardl committed Oct 17, 2023
1 parent f3d8509 commit aec5f07
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
4 changes: 3 additions & 1 deletion local_override.properties_template
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
distribution.azure.connectionString=<insert from Azure Console>
distribution.azure.connectionString=<insert from Azure Console>
sensormappings.azure.connectionString=<insert from Azure Console>
sensormappings.azure.tableName=<insert from Azure Console>
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
</dependency>

<!-- Azure -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.43.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-device-client</artifactId>
Expand All @@ -63,6 +68,11 @@
<artifactId>iot-service-client</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-data-tables</artifactId>
<version>12.3.15</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand Down Expand Up @@ -279,8 +289,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>15</source>
<target>15</target>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package no.cantara.realestate.azure.storage;

import com.azure.data.tables.TableClient;
import com.azure.data.tables.TableClientBuilder;
import com.azure.data.tables.models.ListEntitiesOptions;
import org.slf4j.Logger;

import java.util.List;
import java.util.Map;

import static org.slf4j.LoggerFactory.getLogger;

public class AzureTableClient {
private static final Logger log = getLogger(AzureTableClient.class);

private final TableClient tableClient;

protected AzureTableClient(TableClient tableClient) {
this.tableClient = tableClient;
}

public AzureTableClient(String connectionString, String tableName) {
TableClient tableClient = new TableClientBuilder()
.connectionString(connectionString)
.tableName(tableName)
.buildClient();
this.tableClient = tableClient;
}

public TableClient getTableClient() {
return tableClient;
}

public List<Map<String,Object>> findRows(String partitionKey) {
ListEntitiesOptions options = new ListEntitiesOptions()
.setFilter(String.format("PartitionKey eq '%s'", partitionKey));
return tableClient.listEntities(options, null, null).stream()
.map(tableEntity -> tableEntity.getProperties())
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package no.cantara.realestate.azure.storage;

import com.azure.data.tables.TableClient;
import com.azure.data.tables.models.ListEntitiesOptions;
import com.azure.data.tables.models.TableEntity;
import no.cantara.config.ApplicationProperties;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.slf4j.LoggerFactory.getLogger;

class AzureTableClientTest {
private static final Logger log = getLogger(AzureTableClientTest.class);

public static void main(String[] args) {
boolean useConfig = true;
String connectionString = null;

if (useConfig) {
ApplicationProperties config = ApplicationProperties.builder().defaults().buildAndSetStaticSingleton();
connectionString = config.get(AzureStorageTablesClient.CONNECTIONSTRING_KEY);
log.debug("ConnectionString {}", connectionString);

} else {
if (args.length < 1) {
log.error("You need to provide \"primary connection string\" from a Storage Account Table registered in Azure IoTHub.\n" +
"Enter this string as the first argument when running this class.");
System.exit(0);
}
connectionString = args[0];
log.debug("ConnectionString from commandLine {}", connectionString);
}
AzureTableClient azureTableClient = new AzureTableClient(connectionString, "Metasys");
assertNotNull(azureTableClient);
assertNotNull(azureTableClient.getTableClient());

findNative(azureTableClient);
List<Map<String, Object>> rows = azureTableClient.findRows("1");
log.info("Found {} rows", rows.size());
}

private static void findNative(AzureTableClient azureTableClient) {
TableClient tableClient = azureTableClient.getTableClient();
List<String> propertiesToSelect = new ArrayList<>();
propertiesToSelect.add("RealEstate");
propertiesToSelect.add("Tfm");
String partitionKey = "1";
ListEntitiesOptions options = new ListEntitiesOptions()
.setFilter(String.format("PartitionKey eq '%s'", partitionKey));
// .setSelect(propertiesToSelect);

for (TableEntity entity : tableClient.listEntities(options, null, null)) {
Map<String, Object> properties = entity.getProperties();
log.trace("Properties found. RealEstate {}, TFM {}",properties.get("RealEstate"), properties.get("Tfm"));
}
}

}
3 changes: 2 additions & 1 deletion src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</encoder>
</appender>

<root level="trace">
<logger name="no.cantara" level="trace" />
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit aec5f07

Please sign in to comment.