在队列触发器上的Azure函数应用中,我无法在存储表中插入记录

时间:2018-10-19 12:26:59

标签: java azure azure-functions

我是Azure函数应用程序的新手,我有一个需求,我需要在该队列触发器的基础上插入存储表记录。

     @FunctionName("echo")
     public void echo( @HttpTrigger(name = "req", methods = { "post" }, authLevel = AuthorizationLevel.ANONYMOUS,route="items") String in,
     @TableOutput(name = "$return", tableName = "people", partitionKey="lastName" ,rowKey = "firstName" , connection = "AzureWebJobsStorage") OutputBinding<CustomerEntity> obj,
   ExecutionContext context    ) {
    context.getLogger().info( "Hello, " + in );
    ObjectMapper objectMapper = new ObjectMapper();
    MyItem myItem = null;
    try {
        myItem = objectMapper.readValue(in, MyItem.class);
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            context.getLogger().info( "Hello, " + in + " and " + myItem.getNameOfValue() + ".");
    CustomerEntity customerEntity = new CustomerEntity(myItem.getNameOfValue(), myItem.getId());
    customerEntity.setEmail("Walter@contoso.com");
    customerEntity.setPhoneNumber("425-555-0101");
    obj.setValue(customerEntity);
}

CustomerEntity类         公共类CustomerEntity扩展了TableServiceEntity {

public CustomerEntity(String lastName, String firstName) {
    this.partitionKey = lastName;
    this.rowKey = firstName;
}

public CustomerEntity() { }

String email;
String phoneNumber;

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhoneNumber() {
    return this.phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}
}

在这种情况下,该函数被成功调用,但是它不会打印任何日志,也不会将数据放入存储表中。

我正在为此目的使用本地存储。

我已经测试过该应用程序可与Table客户端独立运行,但不适用于功能应用程序。

谢谢, 巴尔基

因此,经过大量搜索,我得到了工作代码

     public class QueueTriggerJava {
/**
 * This function will be invoked when a new message is received at the specified path. The message contents are provided as input to this function.
 */
@FunctionName("QueueTriggerJava")
public void run(
    @QueueTrigger(name = "message", queueName = "enqueue", connection = "AzureWebJobsStorage") String message,
    @TableOutput(connection="AzureWebJobsStorage",name="table",tableName="people",partitionKey="orders",rowKey="1") OutputBinding<People> out,
    final ExecutionContext context
) throws Exception {
    context.getLogger().info("Java Queue trigger function processed a message: " + message);
    People people = new People(String.valueOf(RandomStringUtils.randomAlphanumeric(10, 20)));
    ObjectMapper objectMapper = new ObjectMapper();
    People peoplew = objectMapper.readValue(message, People.class);
    people.setId(peoplew.getId());
    people.setNameOfValue(peoplew.getNameOfValue());
    out.setValue(people);
    context.getLogger().info("Java Queue trigger function processed a message: " + message);

}

}

我面临的挑战是,每当我将消息放在Queue上时,它都需要使用rowkey静态值相同的rowKey。

public class People{
private String PartitionKey;
private String RowKey;
private String nameOfValue;
private String id;
public People(){

}
 public People (final String rowKey){
     this.PartitionKey = "Orders";
     this.RowKey = rowKey;
 }

 /**
  * @param id the id to set
  */
 public void setId(String id) {
     this.id = id;
 }

 /**
  * @return the id
  */
 public String getId() {
     return id;
 }
 /**
  * @param nameOfValue the nameOfValue to set
  */
 public void setNameOfValue(String nameOfValue) {
     this.nameOfValue = nameOfValue;
 }

 /**
  * @return the nameOfValue
  */
 public String getNameOfValue() {
     return nameOfValue;
 }

}

0 个答案:

没有答案