未能在GridDB中使用存储过程。

huangapple go评论116阅读模式
英文:

Failure to Use a Stored Procedure in GridDB

问题

由于我之前曾与关系型数据库一起工作,使用存储过程是一种常见的做法,但相当长一段时间以来,我已经转向使用GridDB,一个NoSQL数据库。我最近遇到的问题是使用存储过程时出现了以下错误:

  1. [错误代码]: 151001
  2. [描述](语法错误)

我认为在GridDB中存储过程是不支持的操作。是否有一种方法可以在GridDB中创建用户定义的函数(UDF),有人可以帮助提供下面我的代码示例的编辑,以在查询期间执行自定义数据处理操作吗?

  1. import com.toshiba.mwcloud.gs.Collection;
  2. import com.toshiba.mwcloud.gs.GSException;
  3. import com.toshiba.mwcloud.gs.GridStore;
  4. import com.toshiba.mwcloud.gs.GridStoreFactory;
  5. import com.toshiba.mwcloud.gs.Query;
  6. import com.toshiba.mwcloud.gs.RowKey;
  7. import com.toshiba.mwcloud.gs.RowSet;
  8. static class Order{
  9. int order_id;
  10. int total_amount;
  11. }
  12. Properties props = new Properties();
  13. props.setProperty("notificationAddress", "239.0.0.1");
  14. props.setProperty("notificationPort", "31999");
  15. props.setProperty("clusterName", "defaultCluster");
  16. props.setProperty("user", "admin");
  17. props.setProperty("password", "admin");
  18. GridStore store = GridStoreFactory.getInstance().getGridStore(props);
  19. CREATE PROCEDURE CalculateOrderTotal(IN orderId INT, OUT totalAmount DECIMAL(10, 2))
  20. BEGIN
  21. DECLARE itemPrice DECIMAL(10, 2);
  22. SET totalAmount = 0;
  23. DECLARE done INT DEFAULT FALSE;
  24. SELECT price FROM OrderItems WHERE order_id = orderId;
  25. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  26. END
  27. query = f"SELECT order_id, total_amount {CalculateOrderTotal}(quantity, price) AS total_price FROM {myCluster}"
  28. results = container.query(query)
英文:

Since I have previously worked with relational databases, using stored procedures is a common practice, but for quite a long time, I have shifted to using GridDB, a NoSQL database. The problem that I recently faced was the use of a stored procedure which gave me the following error:

  1. [Error code]: 151001
  2. [Description] (Syntax error)

I think the stored procedure is an unsupported operation in GridDB. Is there a way of creating a user-defined function (UDF) in GridDB, and can anyone help provide edits in my code example below on how to use it to perform a custom data processing operation during the query?

  1. import com.toshiba.mwcloud.gs.Collection;
  2. import com.toshiba.mwcloud.gs.GSException;
  3. import com.toshiba.mwcloud.gs.GridStore;
  4. import com.toshiba.mwcloud.gs.GridStoreFactory;
  5. import com.toshiba.mwcloud.gs.Query;
  6. import com.toshiba.mwcloud.gs.RowKey;
  7. import com.toshiba.mwcloud.gs.RowSet;
  8. static class Order{
  9. int order_id;
  10. int total_amount;
  11. }
  12. Properties props = new Properties();
  13. props.setProperty("notificationAddress", "239.0.0.1");
  14. props.setProperty("notificationPort", "31999");
  15. props.setProperty("clusterName", "defaultCluster");
  16. props.setProperty("user", "admin");
  17. props.setProperty("password", "admin");
  18. GridStore store = GridStoreFactory.getInstance().getGridStore(props);
  19. CREATE PROCEDURE CalculateOrderTotal(IN orderId INT, OUT totalAmount DECIMAL(10, 2))
  20. BEGIN
  21. DECLARE itemPrice DECIMAL(10, 2);
  22. SET totalAmount = 0;
  23. DECLARE done INT DEFAULT FALSE;
  24. SELECT price FROM OrderItems WHERE order_id = orderId;
  25. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  26. END
  27. query = f"SELECT order_id, total_amount {CalculateOrderTotal}(quantity, price) AS total_price FROM {myCluster}"
  28. results = container.query(query)

答案1

得分: 1

我将为您创建一个UDF,该UDF根据订单中每个商品的数量和价格计算总价格:

  1. import com.toshiba.mwcloud.gs.GridStore;
  2. import com.toshiba.mwcloud.gs.GridStoreFactory;
  3. import com.toshiba.mwcloud.gs.GridStoreFactoryFactory;
  4. import com.toshiba.mwcloud.gs.Row;
  5. import com.toshiba.mwcloud.gs.RowSet;
  6. import com.toshiba.mwcloud.gs.TimeSeries;
  7. String totalPriceFunction = "function calculateTotalPrice(quantity, price) { return quantity * price; }";
  8. GridStore store = GridStoreFactory.getInstance().getGridStore(props);
  9. store.putFunction("calculateTotalPrice", totalPriceFunction, null);
  10. String containerName = "OrderItems";
  11. TimeSeries<Row> container = store.putTimeSeries(containerName, Row.class);

然后,在您的查询中调用它以计算总价格:

  1. import com.toshiba.mwcloud.gs.Query;
  2. import com.toshiba.mwcloud.gs.RowKey;
  3. import com.toshiba.mwcloud.gs.RowSet;
  4. String queryStr = String.format("SELECT order_id, calculateTotalPrice(quantity, price) AS total_price FROM %s", containerName);
  5. Query<Row> query = store.query(queryStr, Row.class);
  6. RowSet<Row> rs = query.fetch();
  7. while (rs.hasNext()) {
  8. Row row = rs.next();
  9. int orderId = row.getInteger("order_id");
  10. double totalPrice = row.getDouble("total_price");
  11. // 使用 orderId 和 totalPrice 进行其他操作
  12. }

请注意,上述代码是用Java编写的,并且在查询中使用了创建的UDF函数来计算总价格。

英文:

I would create a udf that calculates the total price based on the quantity and price of each item in the order:

  1. import com.toshiba.mwcloud.gs.GridStore;
  2. import com.toshiba.mwcloud.gs.GridStoreFactory;
  3. import com.toshiba.mwcloud.gs.GridStoreFactoryFactory;
  4. import com.toshiba.mwcloud.gs.Row;
  5. import com.toshiba.mwcloud.gs.RowSet;
  6. import com.toshiba.mwcloud.gs.TimeSeries;
  7. String totalPriceFunction = &quot;function calculateTotalPrice(quantity, price) { return quantity * price; }&quot;;
  8. GridStore store = GridStoreFactory.getInstance().getGridStore(props);
  9. store.putFunction(&quot;calculateTotalPrice&quot;, totalPriceFunction, null);
  10. String containerName = &quot;OrderItems&quot;;
  11. TimeSeries&lt;Row&gt; container = store.putTimeSeries(containerName, Row.class);

then call it in your query to calculate total price:

  1. import com.toshiba.mwcloud.gs.Query;
  2. import com.toshiba.mwcloud.gs.RowKey;
  3. import com.toshiba.mwcloud.gs.RowSet;
  4. String queryStr = String.format(&quot;SELECT order_id, calculateTotalPrice(quantity, price) AS total_price FROM %s&quot;, containerName);
  5. Query&lt;Row&gt; query = store.query(queryStr, Row.class);
  6. RowSet&lt;Row&gt; rs = query.fetch();
  7. while (rs.hasNext()) {
  8. Row row = rs.next();
  9. int orderId = row.getInteger(&quot;order_id&quot;);
  10. double totalPrice = row.getDouble(&quot;total_price&quot;);
  11. // do something with orderId and totalPrice
  12. }

huangapple
  • 本文由 发表于 2023年7月23日 17:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定