简体   繁体   中英

Getting items from Amazon DynamoDB using 'where' condition

How to get items from dynamoDB using ' where ' condition?

For example,

select 'names' from Employee where dept="billing"

(I want to get all the names from Employee table whose department is "billing")

How to write a query for this in Java code? Explained with example code is much appreciated. Thank you.

Select-query could be something like that:

SELECT `names`  FROM `Employee` WHERE `dept` LIKE 'billing'

With some java stuff:

try {
        String url = "jdbc:msql://***.***.***.***:****/Demo";
        Connection conn = DriverManager.getConnection(url, "", "");
        Statement stmt = conn.createStatement();
        ResultSet rs;

        rs = stmt.executeQuery("SELECT `names`  FROM `Employee` WHERE `dept` LIKE 'billing'");
        while (rs.next()) {
            String name = rs.getString("names");
            System.out.println(name);
        }
        conn.close();
    } catch (Exception e) {
        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }

You can search the Dynamo DB using the Primary Key (hash key) or a Composite key using (hash+range key)

If any day you are trying to search any attribute of an item you would have to do a complete Scan of the database which is pretty inefficient.

So, try to design your database in a way that you DO NOT end up doing Scans and encourage doing searches based on indexes for which Dynamo is designed.

For the original question : you may use ProjectionExpression for the searches.

For further reference - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html#QueryAndScan.Scan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM