-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#27 Find all rows from a single table
- Loading branch information
Showing
5 changed files
with
120 additions
and
4 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
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> |
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
41 changes: 41 additions & 0 deletions
41
src/main/java/no/cantara/realestate/azure/storage/AzureTableClient.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,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(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/no/cantara/realestate/azure/storage/AzureTableClientTest.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,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")); | ||
} | ||
} | ||
|
||
} |
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