CDH中Kafka基本指令总结——topic使⽤与测试producer产⽣数
据、consu。。。
⼀、相关基础内容
Kafka集中的每个主机都运⾏⼀个称为代理的服务器,该服务器存储发送到主题的消息并服务于消费者请求。
⾸先先看服务器安装kafka的实例信息:
注意:
然后正常kafka的指令是:  ./bin/kafka-topics.sh --zookeeper cluster2-4:2181 .......
但是使⽤CDH安装的kafka则不需要全写出此  ./bin/kafka-topics.sh  部分。只许直接写 kafka-topics 即可,这是很重要的⼀个区别,使⽤CDH安装的kafka时候要特别注意⼀下。
具体有哪些指令可以看此路径下:
/opt/cloudera/parcels/KAFKA-4.1.0-1.4.1.0.p0.4/bin
⼆、topic主题使⽤
接下来测试topic指令,这⾥我们要先看CDH中配置的这个ZooKeeper Root的Kafka服务范围为: " /kafka "。
所以我们使⽤topic的指令格式应该都类似:
kafka-topics --zookeeper cluster2-4:2181/kafka ......
A.创建⼀个名为 test 的主题(Topic):
kafka-topics --zookeeper cluster2-4:2181/kafka --create -replication-factor 1 --partitions 3 --topic test
Or
若是上述中的ZooKeeper Root 的Kafka服务范围为: " / "。则这⾥的创建主题指令改为:
kafka-topics --zookeeper cluster2-4:2181 --create --replication-factor 1 --partitions 3 --topic test
B.查询现在已经存在的topic:
kafka-topics --zookeeper localhost:2181/kafka --list
C.删除创建的topic:
kafka-topics --zookeeper localhost:2181/kafka --delete --topic test2
拓展:
这⾥如果直接删除,则会输出    Topic *** is marked for deletion  如上图,如果我们topic中消息堆积的太多,或者kafka所在磁盘空间满了等等,则会需要彻底清理⼀下kafka topic。
⽅法⼀:修改kafaka配置⽂件server.properties,添加 able=true,重启kafka,之后通过kafka命令⾏就可以直接删除topic。
⽅法⼆:通过命令⾏删除topic:  ./bin/kafka-topics.sh --delete --zookeeper {zookeeper server} --topic {topic name}
  因为kafaka配置⽂件中server.properties没有配置able=true,此时的删除并不是真正的删除,只是把topic标记为:marked for deletion    你可以通过命令:./bin/kafka-topics --zookeeper {zookeeper server} --list 来查看所有topic
⽅法三:若需要真正删除它,需要登录zookeeper客户端:
zookeeper-client
到topic所在的⽬录:
ls /kafka/brokers/topics
执⾏命令,即可,此时topic被彻底删除:
rmr /kafka/brokers/topics/{topic name}
D.修改topic的分区数:
kafka-topics --zookeeper localhost:2181/kafka --alter --topic test \ partitions 5
F.我们还可以在这⾥测试分布式是否连接正常:
kafka-topics --zookeeper cluster2-4:2181/kafka --list
kafka-topics --zookeeper cluster2-3:2181/kafka --list
可以看到在2-4这台服务器中,我们后⾯输⼊cluster2-4:2181/kafka  与  cluster2-3:2181/kafka  均可得到统⼀的信息。
topic指令参数:
Option                                  Description
------                                  -----------
--alter                                  Alter the number of partitions,
replica assignment, and/or
configuration for the topic.
--bootstrap-server <String: server to    REQUIRED: The Kafka server to connect
connect to>                              to. In case of providing this, a
direct Zookeeper connection won't be
required.
--command-config <String: command        Property file containing configs to be
config property file>                    passed to Admin Client. This is used
only with --bootstrap-server option
for describing and altering broker
configs.
--config <String: name=value>            A topic configuration override for the
topic being created or altered.The
following is a list of valid
configurations:
cleanup.policy
file.delete.delay.ms
flush.ms
replicas
index.interval.bytes
able
message.format.version
message.timestamp.difference.max.ms
pe
min.cleanable.dirty.ratio
minpaction.lag.ms
plicas
preallocate
retention.bytes
retention.ms
segment.bytes
segment.index.bytes
segment.jitter.ms
segment.ms
unclean.able
See the Kafka documentation for full
details on the topic configs.It is
supported only in combination with --
create if --bootstrap-server option
is used.
--create                                Create a new topic.
--delete                                Delete a topic
--delete-config <String: name>          A topic configuration override to be
removed for an existing topic (see
the list of configurations under the
-
-config option). Not supported with
the --bootstrap-server option.
--describe                              List details for the given topics.
--disable-rack-aware                    Disable rack aware replica assignment
--exclude-internal                      exclude internal topics when running
list or describe command. The
internal topics will be listed by
default
--force                                  Suppress console prompts
--help                                  Print usage information.
--if-exists                              if set when altering or deleting or
describing topics, the action will
only execute if the topic exists.
Not supported with the --bootstrap-
server option.
--if-not-exists                          if set when creating topics, the
action will only execute if the
topic does not already exist. Not
supported with the --bootstrap-
server option.
--list                                  List all available topics.
--partitions <Integer: # of partitions>  The number of partitions for the topic
being created or altered (WARNING:
If partitions are increased for a
topic that has a key, the partition
logic or ordering of the messages
will be affected
--replica-assignment <String:            A list of manual partition-to-broker
broker_id_for_part1_replica1 :          assignments for the topic being
broker_id_for_part1_replica2 ,          created or altered.
broker_id_for_part2_replica1 :
broker_id_for_part2_replica2 , ...>
--replication-factor <Integer:          The replication factor for each
replication factor>                      partition in the topic being created.
--topic <String: topic>                  The topic to create, alter, describe
or delete. It also accepts a regular
expression, except for --create
option. Put topic name in double
quotes and use the '\' prefix to
escape regular expression symbols; e.
g. "test\.topic".
--topics-with-overrides                  if set when describing topics, only
show topics that have overridden
configs
-
-unavailable-partitions                if set when describing topics, only
show partitions whose leader is not
available
--under-replicated-partitions            if set when describing topics, only
show under replicated partitions
--zookeeper <String: hosts>              DEPRECATED, The connection string for
the zookeeper connection in the form
host:port. Multiple hosts can be
given to allow fail-over.
三、测试producer产⽣数据、consumer消费数据
之前我们创建好topic以后,这⾥测试⼀下如何使⽤kafka中的kafka-console-producer与kafka-console-consumer来⽣产数据、另⼀端消费数据。
还需先了解这⾥发布-订阅系统中的代理结构:
producer产⽣数据到Topic中,然后consumer从要消费的Topic中消费数据。
⾸先启动producer:
kafka-console-producer --broker-list cluster2-4:9092 --topic test
在这⾥输⼊数据,这些数据会上传到zookeeper中的 /kafka/broker/test 主题中。
kafka-console-producer ⽣产者的指令参数:
Option                                  Description
------                                  -----------
--batch-size <Integer: size>            Number of messages to send in a single
batch if they are not being sent
synchronously. (default: 200)
-
-broker-list <String: broker-list>      REQUIRED: The broker list string in
the form HOST1:PORT1,HOST2:PORT2.
--compression-codec [String:            The compression codec: either 'none',
compression-codec]                      'gzip', 'snappy', 'lz4', or 'zstd'.
If specified without value, then it
defaults to 'gzip'
kafka命令--help                                  Print usage information.
--line-reader <String: reader_class>    The class name of the class to use for
reading lines from standard in. By
default each line is read as a
separate message. (default: kafka.
tools.
ConsoleProducer$LineMessageReader)
--max-block-ms <Long: max block on      The max time that the producer will
send>                                    block for during a send request
(default: 60000)
--max-memory-bytes <Long: total memory  The total memory used by the producer
in bytes>                                to buffer records waiting to be sent
to the server. (default: 33554432)
--max-partition-memory-bytes <Long:      The buffer size allocated for a
memory in bytes per partition>          partition. When records are received
which are smaller than this size the
producer will attempt to
optimistically group them together
until this size is reached.
(default: 16384)
--message-send-max-retries <Integer>    Brokers can fail receiving the message
for multiple reasons, and being
unavailable transiently is just one
of them. This property specifies the
number of retires before the
producer give up and drop this
message. (default: 3)
-
-metadata-expiry-ms <Long: metadata    The period of time in milliseconds
expiration interval>                    after which we force a refresh of
metadata even if we haven't seen any
leadership changes. (default: 300000)
--producer-property <String:            A mechanism to pass user-defined
producer_prop>                          properties in the form key=value to
the producer.
--fig <String: config file>  Producer config properties file. Note
that [producer-property] takes
precedence over this config.
--property <String: prop>                A mechanism to pass user-defined
properties in the form key=value to
the message reader. This allows
custom configuration for a user-
defined message reader.
--request-required-acks <String:        The required acks of the producer
request required acks>                  requests (default: 1)
--request-timeout-ms <Integer: request  The ack timeout of the producer
timeout ms>                              requests. Value must be non-negative
and non-zero (default: 1500)
--retry-backoff-ms <Integer>            Before each retry, the producer
refreshes the metadata of relevant
topics. Since leader election takes
a bit of time, this property
specifies the amount of time that
the producer waits before refreshing
the metadata. (default: 100)
--socket-buffer-size <Integer: size>    The size of the tcp RECV size.
(default: 102400)
--sync                                  If set message send requests to the
brokers are synchronously, one at a
time as they arrive.
--timeout <Integer: timeout_ms>          If set and the producer is running in
asynchronous mode, this gives the
maximum amount of time a message
will queue awaiting sufficient batch
size. The value is given in ms.
(default: 1000)
--topic <String: topic>                  REQUIRED: The topic id to produce
messages to.
接着启动消费者:
kafka-console-consumer --bootstrap-server cluster2-3:9092 --topic test --from-beginning
后⾯的--from-beginning  表⽰从指定主题中有效的起始位移位置开始消费所有分区的消息。
消费者消费到topic的数据:
kafka-console-consumer 消费者的指令参数:
Option                                  Description
------                                  -----------
--bootstrap-server <String: server to    REQUIRED: The server(s) to connect to.
connect to>
--consumer-property <String:            A mechanism to pass user-defined
consumer_prop>                          properties in the form key=value to
the consumer.
--fig <String: config file>  Consumer config properties file. Note
that [consumer-property] takes
precedence over this config.
-
-enable-systest-events                  Log lifecycle events of the consumer
in addition to logging consumed
messages. (This is specific for
system tests.)
--formatter <String: class>              The name of a class to use for
formatting kafka messages for
display. (default: ls.
DefaultMessageFormatter)
--from-beginning                        If the consumer does not already have
an established offset to consume
from, start with the earliest
message present in the log rather
than the latest message.
--group <String: consumer group id>      The consumer group id of the consumer.
--help                                  Print usage information.
--isolation-level <String>              Set to read_committed in order to
filter out transactional messages
which are not committed. Set to
read_uncommittedto read all
messages. (default: read_uncommitted)
--key-deserializer <String:
deserializer for key>
-
-max-messages <Integer: num_messages>  The maximum number of messages to
consume before exiting. If not set,
consumption is continual.
--offset <String: consume offset>        The offset id to consume from (a non-
negative number), or 'earliest'
which means from beginning, or
'latest' which means from end
(default: latest)
--partition <Integer: partition>        The partition to consume from.
Consumption starts from the end of
the partition unless '--offset' is
specified.
--property <String: prop>                The properties to initialize the

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。