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 aec5f07 commit 6f27a81
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>no.cantara.realestate</groupId>
<artifactId>azure-client-java</artifactId>
<version>0.3.1-SNAPSHOT</version>
<version>0.4.0-SNAPSHOT</version>

<name>azure-client-java</name>
<url>https://github.com/Cantara/realestate-azure-client-lib</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package no.cantara.realestate.azure.storage;

import com.azure.core.credential.AzureNamedKeyCredential;
import com.azure.data.tables.TableClient;
import com.azure.data.tables.TableServiceClient;
import com.azure.data.tables.TableServiceClientBuilder;
import org.slf4j.Logger;

import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

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

private final TableServiceClient tableServiceClient;

public static final String CONNECTIONSTRING_KEY = "sensormappings.azure.connectionString";


protected AzureStorageTablesClient(TableServiceClient tableClient) {
this.tableServiceClient = tableClient;
}

public AzureStorageTablesClient(String connectionString) {
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
this.tableServiceClient = tableServiceClient;
}

public AzureStorageTablesClient(String storageaccountName, String accountKey, String storageTableUrl) {
AzureNamedKeyCredential credential = new AzureNamedKeyCredential(storageaccountName, accountKey);
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.endpoint(storageTableUrl)
.credential(credential)
.buildClient();
this.tableServiceClient = tableServiceClient;
}

public TableServiceClient getTableServiceClient() {
return tableServiceClient;
}

public List<String> listTables() {
List<String> tableNames = tableServiceClient.listTables().stream()
.map(tableItem -> tableItem.getName())
.toList();
return tableNames;
}

public String getAllInTable(String tableName) {
//return all rows in table as json
TableClient tableClient = tableServiceClient.getTableClient(tableName);
tableClient.listEntities().stream().forEach(tableEntity -> log.info("RowKey: {}, PartitionKey: {}, Properties: {}",
tableEntity.getRowKey(), tableEntity.getPartitionKey(), tableEntity.getProperties()));
return null;
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package no.cantara.realestate.azure.storage;

import com.azure.data.tables.TableServiceClient;
import com.azure.data.tables.TableServiceClientBuilder;
import no.cantara.config.ApplicationProperties;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

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

public static void main(String[] args) {
boolean useConfig = true;
AzureStorageTablesClient storageTablesClient;
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);
}
buildNative(connectionString);

storageTablesClient = new AzureStorageTablesClient(connectionString);
log.info("Found client: {}", storageTablesClient);

log.info("Listing tables");
storageTablesClient.listTables().stream().forEach(tableItem -> log.info("Table name: {}", tableItem));

}

private static void buildNative(String connectionString) {
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
log.info("Found tableServiceClient: {}", tableServiceClient);
tableServiceClient.listTables().forEach(tableItem -> log.info("Table name: {}", tableItem.getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.slf4j.LoggerFactory.getLogger;

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

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

0 comments on commit 6f27a81

Please sign in to comment.