Apache Geode中Region属性的来源及含指定属性数据写入实例咨询
Hey there! Let's clear up your confusion step by step—Apache Geode works quite differently from relational databases, which is why you don't see a "create table" style schema definition here.
Why There's No Explicit Schema for status
Unlike relational databases that require you to define columns upfront, Apache Geode is schemaless (or schema-on-write). That means you don't need to pre-declare attributes like status before writing data. The status property simply comes from the data you store in the exampleRegion—you define it when you add records to the region.
How to Write Data with the status Attribute
Here are two common ways to insert data containing the status attribute into exampleRegion:
1. Using GFSH (Geode Command Line Interface)
GFSH is great for quick testing. First, make sure your Geode cluster is running, then:
- Create the
exampleRegion(if it doesn't exist):create region --name=exampleRegion --type=REPLICATE - Insert data with the
statusfield using JSON-formatted values:# Add an active record put --region=exampleRegion --key=user1 --value='{"id": "user1", "name": "Alice Smith", "status": "active"}' # Add an inactive record (to test your query later) put --region=exampleRegion --key=user2 --value='{"id": "user2", "name": "Bob Jones", "status": "inactive"}'
2. Using Java API (For Application Integration)
If you're building an application, you can use Geode's Java client to write objects with the status attribute. First, define a simple class for your data:
import java.io.Serializable; public class User implements Serializable { private String id; private String name; private String status; // Constructor public User(String id, String name, String status) { this.id = id; this.name = name; this.status = status; } // Getters and setters (required for Geode to access fields) public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } }
Then write data to the region:
import org.apache.geode.cache.ClientCache; import org.apache.geode.cache.ClientCacheFactory; import org.apache.geode.cache.Region; public class GeodeDataWriter { public static void main(String[] args) { // Connect to your Geode cluster (adjust locator host/port as needed) ClientCache cache = new ClientCacheFactory() .addPoolLocator("localhost", 10334) .create(); // Get the exampleRegion Region<String, User> exampleRegion = cache.getRegion("exampleRegion"); // Write records with status attribute exampleRegion.put("user1", new User("user1", "Alice Smith", "active")); exampleRegion.put("user2", new User("user2", "Bob Jones", "inactive")); // Clean up cache.close(); } }
Testing Your Query
Once you've written the data, you can run your query in GFSH:
query --query="SELECT DISTINCT * FROM /exampleRegion p WHERE p.status = 'active'"
This will return the record for Alice Smith, since her status is set to active.
内容的提问来源于stack exchange,提问作者Shengxin Huang

