Deleting a table
To delete the Series
table:
-
Create the
SeriesDeleteTable
project:mvn -B archetype:generate \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.mycompany.app \ -DartifactId=SeriesDeleteTable
As a result of running the command, the
SeriesDeleteTable
project folder is created in the current working folder with a structure of subfolders and thepom.xml
project description file. -
Go to the project folder:
cd SeriesDeleteTable
-
Edit the project description in the
pom.xml
file, for example, using the nano editor:nano pom.xml
Sample
pom.xml
file:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>SeriesDeleteTable</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SeriesDeleteTable</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.mycompany.app.SeriesDeleteTable</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> <finalName>release/SeriesDeleteTable</finalName> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/release/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-dynamodb</artifactId> <version>1.11.1012</version> </dependency> </dependencies> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project>
Check the current versions of junit
and aws-java-sdk-dynamodb . -
In the folder
src/main/java/com/mycompany/app/
, create theSeriesDeleteTable.java
file, for example, using the nano editor:nano src/main/java/com/mycompany/app/SeriesDeleteTable.java
Copy the following code to the created file:
Warning
Instead of
<Document_API_endpoint>
, specify the prepared value.package com.mycompany.app; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Table; public class SeriesDeleteTable { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("<Document_API_endpoint>", "ru-central1")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Series"); try { System.out.println("Trying to delete the table, wait..."); table.delete(); table.waitForDelete(); System.out.print("Series table deleted."); } catch (Exception e) { System.err.println("Couldn't delete the table: "); System.err.println(e.getMessage()); } } }
-
Build a project:
mvn package
As a result of running the command, the
SeriesDeleteTable.jar
file is generated in the foldertarget/release/
. -
Run the application:
java -jar target/release/SeriesDeleteTable.jar
Result:
Attempting to delete table, wait... Table Series deleted.
-
Create the
SeriesDeleteTable.py
file, for example, using the nano editor:nano SeriesDeleteTable.py
Copy the following code to the created file:
Warning
Instead of
<Document_API_endpoint>
, specify the prepared value.import boto3 def delete_serie_table(): ydb_docapi_client = boto3.resource('dynamodb', endpoint_url = "<Document_API_endpoint>") table = ydb_docapi_client.Table('Series') table.delete() if __name__ == '__main__': delete_serie_table() print("Table Series deleted")
-
Run the program:
python SeriesDeleteTable.py
Result:
Table Series deleted.
-
Create the
SeriesDeleteTable.php
file, for example, using the nano editor:nano SeriesDeleteTable.php
Copy the following code to the created file:
Warning
Instead of
<Document_API_endpoint>
, specify the prepared value.<?php require 'vendor/autoload.php'; date_default_timezone_set('UTC'); use Aws\DynamoDb\Exception\DynamoDbException; $sdk = new Aws\Sdk([ 'endpoint' => '<Document_API_endpoint>', 'region' => 'ru-central1', 'version' => 'latest' ]); $dynamodb = $sdk->createDynamoDb(); $params = [ 'TableName' => 'Series' ]; try { $result = $dynamodb->deleteTable($params); echo "Table deleted.\n"; } catch (DynamoDbException $e) { echo "Couldn't delete table:\n"; echo $e->getMessage() . "\n"; } ?>
-
Run the program:
php SeriesDeleteTable.php
Result:
Table deleted.
-
Create the
SeriesDeleteTable.js
file, for example, using the nano editor:nano SeriesDeleteTable.js
Copy the following code to the created file:
Warning
Instead of
<Document_API_endpoint>
, specify the prepared value.const AWS = require("@aws-sdk/client-dynamodb"); // Credentials should be defined via environment variables AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID const dynamodb = new AWS.DynamoDBClient({ region: "ru-central1", endpoint: "<Document_API_endpoint>", }); dynamodb.send(new AWS.DeleteTableCommand({ TableName: "Series" })) .then(data => { console.error("Couldn't delete table. JSON error:", JSON.stringify(err, null, 2)); }) .catch(err => { console.log("Table deleted. Description of JSON table:", JSON.stringify(data, null, 2)); })
-
Run the program:
node SeriesDeleteTable.js
Result:
Table deleted. Description of JSON table: { "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "series_id", "AttributeType": "N" }, { "AttributeName": "title", "AttributeType": "S" } ], "TableName": "Series", "KeySchema": [ { "AttributeName": "series_id", "KeyType": "HASH" }, { "AttributeName": "title", "KeyType": "RANGE" } ], "TableStatus": "DELETING", "CreationDateTime": "2021-06-14T19:34:32.000Z", "TableSizeBytes": 6064, "ItemCount": 8 } }
-
Create the
SeriesDeleteTable.rb
file, for example, using the nano editor:nano SeriesDeleteTable.rb
Copy the following code to the created file:
Warning
Instead of
<Document_API_endpoint>
, specify the prepared value.require 'aws-sdk-dynamodb' def table_deleted?(dynamodb_client, table_name) dynamodb_client.delete_table(table_name: table_name) true rescue StandardError => e puts "Error deleting table: #{e.message}" false end def run_me region = 'ru-central1' table_name = 'Series' Aws.config.update( endpoint: '<Document_API_endpoint>', region: region ) dynamodb_client = Aws::DynamoDB::Client.new puts "Deleting a table '#{table_name}'..." if table_deleted?(dynamodb_client, table_name) puts 'Table deleted.' else puts 'Couldn't delete table.' end end run_me if $PROGRAM_NAME == __FILE__
-
Run the program:
ruby SeriesDeleteTable.rb
Result:
Deleting table 'Series'... Table deleted.