Create and Manage Streams

Guidelines to name a resource

An HStream resource name uniquely identifies an HStream resource, such as a stream, a subscription or a reader. The resource name must fit the following requirements:

  • Start with a letter
  • Length must be no longer than 255 characters
  • Contain only the following characters: Letters [A-Za-z], numbers [0-9], dashes -, underscores _

*For the cases where the resource name is used as a part of a SQL statement, such as in HStream SQL Shell, there will be situations where the resource name cannot be parsed properly (such as conflicts with Keywords etc.), enclose the resource name with backticks ` . With the enhancements of the SQL parser, the constriction may be removed in the future.

Attributes of a Stream

  • Replication factor

    For fault tolerance and higher availability, every stream can be replicated across nodes in the cluster. A typical production setting is a replication factor of 3, i.e., there will always be three copies of your data, which is helpful just in case things go wrong or you want to do maintenance on the brokers. This replication is performed at the level of the stream.

  • Backlog retention

    The configuration controls how long streams of HStreamDB retain records after being appended. HStreamDB will discard the message regardless of whether it is consumed when it exceeds the backlog retention duration.

    • Default = 7 days
    • Minimum value = 1 seconds
    • Maximum value = 21 days
  • Shard Count

    The number of shards that a stream will have.

Create a Stream

Create a stream before you write records or create a subscription.

  1. // CreateStreamExample.java
  2. package docs.code.examples;
  3. import io.hstream.HStreamClient;
  4. public class CreateStreamExample {
  5. public static void main(String[] args) throws Exception {
  6. // TODO(developer): Replace these variables before running the sample.
  7. String serviceUrl = "127.0.0.1:6570";
  8. if (System.getenv("serviceUrl") != null) {
  9. serviceUrl = System.getenv("serviceUrl");
  10. }
  11. String streamName1 = "your_h_records_stream_name";
  12. String streamName2 = "your_raw_records_stream_name";
  13. HStreamClient client = HStreamClient.builder().serviceUrl(serviceUrl).build();
  14. createStreamExample(client, streamName1);
  15. createStreamWithAttrsExample(client, streamName2);
  16. client.close();
  17. }
  18. public static void createStreamExample(HStreamClient client, String streamName) {
  19. client.createStream(streamName);
  20. }
  21. public static void createStreamWithAttrsExample(HStreamClient client, String streamName) {
  22. client.createStream(
  23. streamName,
  24. (short) 1 // replication factor
  25. ,
  26. 10 // Number of shards
  27. ,
  28. 7 * 24 * 3600 // backlog retention time in seconds
  29. );
  30. }
  31. }
  1. // ExampleCreateStream.go
  2. package examples
  3. import (
  4. "log"
  5. "github.com/hstreamdb/hstreamdb-go/hstream"
  6. )
  7. func ExampleCreateStream() error {
  8. client, err := hstream.NewHStreamClient(YourHStreamServiceUrl)
  9. if err != nil {
  10. log.Fatalf("Creating client error: %s", err)
  11. }
  12. defer client.Close()
  13. // Create a stream, only specific streamName
  14. if err = client.CreateStream("testDefaultStream"); err != nil {
  15. log.Fatalf("Creating stream error: %s", err)
  16. }
  17. // Create a new stream with 1 replica, 5 shards, set the data retention to 1800s.
  18. err = client.CreateStream("testStream",
  19. hstream.WithReplicationFactor(1),
  20. hstream.EnableBacklog(1800),
  21. hstream.WithShardCount(5))
  22. if err != nil {
  23. log.Fatalf("Creating stream error: %s", err)
  24. }
  25. return nil
  26. }
  1. # https://github.com/hstreamdb/hstreamdb-py/blob/main/examples/snippets/guides.py
  2. import asyncio
  3. import hstreamdb
  4. import os
  5. # NOTE: Replace with your own host and port
  6. host = os.getenv("GUIDE_HOST", "127.0.0.1")
  7. port = os.getenv("GUIDE_PORT", 6570)
  8. stream_name = "your_stream"
  9. subscription = "your_subscription"
  10. # Run: asyncio.run(main(your_async_function))
  11. async def main(*funcs):
  12. async with await hstreamdb.insecure_client(host=host, port=port) as client:
  13. for f in funcs:
  14. await f(client)
  15. async def create_stream(client):
  16. await client.create_stream(
  17. stream_name, replication_factor=1, backlog=24 * 60 * 60, shard_count=1
  18. )

Delete a Stream

Deletion is only allowed when a stream has no subsequent subscriptions unless the force flag is set.

Delete a stream with the force flag

If you need to delete a stream with subscriptions, enable force deletion. Existing stream subscriptions can still read from the backlog after deleting a stream with the force flag enabled. However, these subscriptions will have stream name __deleted_stream__, no new subscription creation on the deleted stream would be allowed, nor new records would be allowed to be written to the stream.

  1. // DeleteStreamExample.java
  2. package docs.code.examples;
  3. import io.hstream.HStreamClient;
  4. public class DeleteStreamExample {
  5. public static void main(String[] args) throws Exception {
  6. // TODO(developer): Replace these variables before running the sample.
  7. // String serviceUrl = "your-service-url-address";
  8. String serviceUrl = "127.0.0.1:6570";
  9. if (System.getenv("serviceUrl") != null) {
  10. serviceUrl = System.getenv("serviceUrl");
  11. }
  12. String streamName1 = "your_h_records_stream_name";
  13. String streamName2 = "your_raw_records_stream_name";
  14. HStreamClient client = HStreamClient.builder().serviceUrl(serviceUrl).build();
  15. deleteStreamExample(client, streamName1);
  16. deleteStreamForceExample(client, streamName2);
  17. client.close();
  18. }
  19. public static void deleteStreamExample(HStreamClient client, String streamName) {
  20. client.deleteStream(streamName);
  21. }
  22. public static void deleteStreamForceExample(HStreamClient client, String streamName) {
  23. client.deleteStream(streamName, true);
  24. }
  25. }
  1. // ExampleDeleteStream.go
  2. package examples
  3. import (
  4. "github.com/hstreamdb/hstreamdb-go/hstream"
  5. "log"
  6. )
  7. func ExampleDeleteStream() error {
  8. client, err := hstream.NewHStreamClient(YourHStreamServiceUrl)
  9. if err != nil {
  10. log.Fatalf("Creating client error: %s", err)
  11. }
  12. defer client.Close()
  13. // force delete stream and ignore none exist stream
  14. if err := client.DeleteStream("testStream",
  15. hstream.EnableForceDelete,
  16. hstream.EnableIgnoreNoneExist); err != nil {
  17. log.Fatalf("Deleting stream error: %s", err)
  18. }
  19. if err := client.DeleteStream("testDefaultStream"); err != nil {
  20. log.Fatalf("Deleting stream error: %s", err)
  21. }
  22. return nil
  23. }
  1. # https://github.com/hstreamdb/hstreamdb-py/blob/main/examples/snippets/guides.py
  2. import asyncio
  3. import hstreamdb
  4. import os
  5. # NOTE: Replace with your own host and port
  6. host = os.getenv("GUIDE_HOST", "127.0.0.1")
  7. port = os.getenv("GUIDE_PORT", 6570)
  8. stream_name = "your_stream"
  9. subscription = "your_subscription"
  10. # Run: asyncio.run(main(your_async_function))
  11. async def main(*funcs):
  12. async with await hstreamdb.insecure_client(host=host, port=port) as client:
  13. for f in funcs:
  14. await f(client)
  15. async def delete_stream(client):
  16. await client.delete_stream(stream_name, ignore_non_exist=True, force=True)

List Streams

To get all streams in HStreamDB:

  1. // ListStreamsExample.java
  2. package docs.code.examples;
  3. import io.hstream.HStreamClient;
  4. import io.hstream.Stream;
  5. import java.util.List;
  6. public class ListStreamsExample {
  7. public static void main(String[] args) throws Exception {
  8. // TODO(developer): Replace these variables before running the sample.
  9. String serviceUrl = "127.0.0.1:6570";
  10. if (System.getenv("serviceUrl") != null) {
  11. serviceUrl = System.getenv("serviceUrl");
  12. }
  13. HStreamClient client = HStreamClient.builder().serviceUrl(serviceUrl).build();
  14. listStreamExample(client);
  15. client.close();
  16. }
  17. public static void listStreamExample(HStreamClient client) {
  18. List<Stream> streams = client.listStreams();
  19. for (Stream stream : streams) {
  20. System.out.println(stream.getStreamName());
  21. }
  22. }
  23. }

s

  1. // ExampleListStreams.go
  2. package examples
  3. import (
  4. "fmt"
  5. "github.com/hstreamdb/hstreamdb-go/hstream"
  6. "log"
  7. )
  8. func ExampleListStreams() error {
  9. client, err := hstream.NewHStreamClient(YourHStreamServiceUrl)
  10. if err != nil {
  11. log.Fatalf("Creating client error: %s", err)
  12. }
  13. defer client.Close()
  14. streams, err := client.ListStreams()
  15. if err != nil {
  16. log.Fatalf("Listing streams error: %s", err)
  17. }
  18. for _, stream := range streams {
  19. fmt.Printf("%+v\n", stream)
  20. }
  21. return nil
  22. }
  1. # https://github.com/hstreamdb/hstreamdb-py/blob/main/examples/snippets/guides.py
  2. import asyncio
  3. import hstreamdb
  4. import os
  5. # NOTE: Replace with your own host and port
  6. host = os.getenv("GUIDE_HOST", "127.0.0.1")
  7. port = os.getenv("GUIDE_PORT", 6570)
  8. stream_name = "your_stream"
  9. subscription = "your_subscription"
  10. # Run: asyncio.run(main(your_async_function))
  11. async def main(*funcs):
  12. async with await hstreamdb.insecure_client(host=host, port=port) as client:
  13. for f in funcs:
  14. await f(client)
  15. async def list_streams(client):
  16. ss = await client.list_streams()
  17. for s in ss:
  18. print(s)