-
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
4 changed files
with
115 additions
and
3 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
63 changes: 63 additions & 0 deletions
63
src/main/java/no/cantara/realestate/azure/storage/AzureStorageTablesClient.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,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; | ||
} | ||
|
||
|
||
|
||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/no/cantara/realestate/azure/storage/ManualAzureStorageTablesClientTest.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,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())); | ||
} | ||
} |
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