Java 通过Android应用程序查询我的AWS dynamoDB以查找具有特定哈希值的条目计数

Java 通过Android应用程序查询我的AWS dynamoDB以查找具有特定哈希值的条目计数,java,android,amazon-web-services,amazon-dynamodb,Java,Android,Amazon Web Services,Amazon Dynamodb,我的表格有两列,即country cities,其中3个条目具有相同的散列键,分别是India Mumbai、India Delhi、India Bangalore(在aws dynamodb中手动检查),其余的都不同,例如Japan Tokyo。我希望通过我的android应用程序获得计数3。 这是我的密码:- public class MainActivity extends AppCompatActivity { Button buttonRetrieve; @Override pro

我的表格有两列,即country cities,其中3个条目具有相同的散列键,分别是India Mumbai、India Delhi、India Bangalore(在aws dynamodb中手动检查),其余的都不同,例如Japan Tokyo。我希望通过我的android应用程序获得计数3。 这是我的密码:-

public class MainActivity extends AppCompatActivity {
Button buttonRetrieve;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonRetrieve = (Button) findViewById(R.id.btn_retrievedata);

    buttonRetrieve.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new RetrieveData().execute();
        }
    });
}
 private class RetrieveData extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        //Toast.makeText(MainActivity.this, "in do in background", Toast.LENGTH_SHORT).show();

        try {
            Manager manager = new Manager();
            CognitoCredentialsProvider credentialsProvider = manager.getCredentials(MainActivity.this);
            Mapper mapperClass = new Mapper();

            if (credentialsProvider != null && mapperClass != null) {
                DynamoDBMapper dynamoDBMapper = manager.initDynamoClient((CognitoCachingCredentialsProvider) credentialsProvider);
                DynamoDBQueryExpression dynamoDBQueryExpression = new DynamoDBQueryExpression().withLimit(5).withHashKeyValues("India")
                        .withRangeKeyCondition("City", new Condition()
                                .withComparisonOperator(ComparisonOperator.EQ)
                                .withAttributeValueList(new AttributeValue()
                                        .withS("Mumbai")));
                 int s = dynamoDBMapper.count(Mapper.class, dynamoDBQueryExpression);
                 Toast.makeText(MainActivity.this, "the entries are " + s, Toast.LENGTH_SHORT).show();
           } else {
                return "h";
            }
            return "m";
        } catch (DynamoDBMappingException ex)

        {
            ex.printStackTrace();
            return "k";
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result.equals("h")) {
            Toast.makeText(MainActivity.this, "Already Existing", Toast.LENGTH_LONG).show();
        } else if (result.equals("m")) {
            Toast.makeText(MainActivity.this, "Retrieved data successfully :)", Toast.LENGTH_SHORT).show();
            } else {
            Toast.makeText(MainActivity.this, "**Exception Caught**", Toast.LENGTH_SHORT).show();
        }
    }
}
}

提前谢谢

您需要提供一个散列键条件值。例如:

Book bookToFind = new Book();
bookToFind.setAuthor("Charles Dickens");

String queryString = "Great";

Condition rangeKeyCondition = new Condition()
        .withComparisonOperator(ComparisonOperator.BEGINS_WITH.toString())
        .withAttributeValueList(new AttributeValue().withS(queryString.toString()));

DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
        .withHashKeyValues(bookToFind)
        .withRangeKeyCondition("Title", rangeKeyCondition)
        .withConsistentRead(false);

PaginatedQueryList<Book> result = mapper.query(Book.class, queryExpression);
// Do something with result.
Book bookToFind=新书();
bookToFind.setAuthor(“查尔斯·狄更斯”);
字符串queryString=“Great”;
条件rangeKeyCondition=新条件()
.withComparisonOperator(ComparisonOperator.以.toString()开头)
.WithAttributeValue列表(新的AttributeValue().With(queryString.toString());
DynamoDBQueryExpression queryExpression=新建DynamoDBQueryExpression()
.withHashKeyValues(bookToFind)
.withRangeKeyCondition(“标题”,rangeKeyCondition)
.具有一致性读取(错误);
PaginatedQueryList结果=mapper.query(Book.class,queryExpression);
//做一些有结果的事情。

您需要创建mapper类对象并设置要查询的哈希键属性

Mapper mapperClass = new Mapper();
mapperClass.setCountry("India);
// And then pass it to .withHashKeyValues(mapperClass);

我已经通过创建一个condition对象
DynamoDBQueryExpression DynamoDBQueryExpression=new DynamoDBQueryExpression().withLimit(5).withHashKeyValues(“India”).withRangeKeyCondition(“City”,new condition().withComparisonOperator,在withRangeKeyCondition()方法中提供了条件(比较noperator.EQ.)与AttributeValue列表(新的AttributeValue().with(“孟买”));
@ShubhamJain@manveenkaur为什么不尝试传递更高级别的对象而不是字符串对象?例如:
Mapper mapperClass = new Mapper();
mapperClass.setCountry("India);
// And then pass it to .withHashKeyValues(mapperClass);