「本文已参与好文召集令活动,点击查看:后端、大前端双赛道投稿,2万元奖池等你挑战!」
1:DevOps
大厂的运维基本都是通过运维工具赋能开发。实际的开发过程中,运维极少参与一个项目的上线以及日常的管理。这样对于开发的要求就上升一个层次,线上遇到相关问题就需要立马清楚相关问题的解决方案,以及相关命令执行的意义。从而快速精准的定位线上问题。接下来梳理一下日常工作过程中的常用命令,以备急用。
2:组件的常用命令
1:线上问题常用命令
jsatck 处理线上问题
##查找进程里面cpu占用最高的线程
top -Hp pid
##top 线程
printf "%x\n" 21742
## 建议使用root权限
jstack pid | grep 54ee
##查找大于800m的文件
find . -type f -size +800M
##查找大文件夹
du -hm --max-depth=2 | sort -nr | head -12
##查看nginx 请求最多ip
cat /***/nginx/logs/access.log|awk '{print $1,$2}'sort|uniq -c|sort -rn -k1|head -3
## nohup 启动线上任务
nohup java -jar publish_online.jar --server.port=8106 &>/opt/data/xxx_8106.log &
## 查找/opt/shell目录下以及子目录包含redis关键词
grep -ril "redis" /opt/shell/*
2:hadoop常用命令
##查看集群状态
hdfs dfsadmin -report
##查看目录下的文件
hadoop dfs -ls /user/hive/
## hdfs删除快照
hdfs dfs -deleteSnapshot /user/hive/hive-snapshot-20210509
下载数据块
hadoop fs -get /user/hive/warehouse/ods.db/table_name/part-00021-d20b1d30-dce1-4a38-a0c1-2b4ef68b7da8-c000.snappy.orc ./
#下载Yarn日志
yarn logs -applicationId application_1606201254014_1003 > application_1606201254014_1003.txt
#查看正在写入文件
hdfs fsck /user/events -openforwrite
#恢复文件租约
hdfs debug recoverLease -path /user/hive/warehouse/app.db/test_order_sample1/order_sample -retries 3
## 将edtis目录下二进制文件改为xml,可以伪装相关命令处理
hdfs oev -i edits_0000000001177152591-0000000001177162768 -o edits_0000000001177152591-0000000001177162768.xml
## 将xml改为 二进制文件,可以伪装相关命令处理
hdfs oev -i edits_0000000001177152591-0000000001177162768.xml -o edits_0000000001177152591-0000000001177162768 -p binary
##检查hdfs坏的数据块
hadoop fsck /user -files -blocks -locations
##删除坏的block
hadoop fsck -rm /user/spark/sparkstreamingcheckpoint/checkpoint-1517487380000
##统计文件下数据块
hdfs dfs -count /user/hive/warehouse/dws.db/*/
## hadoop 跨集群同步
hadoop distcp -D dfs.replication=2 hdfs://src-hadoop-address:9000/data/tmp/ hdfs://dst-hadoop-address:9000/data/hfile/ >> /home/data/logs/hadoop-${date}.log 2>&1
3:kafka常用命令
##518400000 为毫秒,转化一下即为天数
kafka-configs --zookeeper zkhost:2181 --alter --entity-name topic_name --entity-type topics --add-config retention.ms=518400000
##设置某一个kafka 对应的topic 永久存储
kafka-configs --zookeeper zkhost:2181 --alter --entity-name topic_name --entity-type topics --add-config retention.ms=-1
## 重置kafka的消费时间节点(注意时区)
kafka-consumer-groups --bootstrap-server kafkahost:9092 --group event_tag --reset-offsets --topic topic_name --execute --to-datetime 2021-04-23T08:20:00.000
## 重置kakfa的 offset
kafka-consumer-groups --bootstrap-server kafkahost:9092 --group event_tag --topic topic_name --execute --reset-offsets --to-offset 5425016603
## 查看所有的topic
kafka-topics --list --zookeeper zkhost:2181
## 删除指定的 topic
kafka-topics --delete --zookeeper zkhost:2181 --topic topic_name
## 调整kafka的分区以及副本数
kafka-topics --alter --zookeeper zkhost:2181 --topic topic_name --partitions 10 --replication-factor 2
##重置offset 最早
kafka-consumer-groups --bootstrap-server kafkahost:9092 --group group_name --reset-offsets --topic topic_name --to-earliest --execute
##重置offset 最近
kafka-consumer-groups --bootstrap-server kafkahost:9092 --group group_name --reset-offsets --topic topic_name --to-latest --execute
4:elasticsearch常用命令
## elasticsearch index 添加字段
POST /ex_index/_mapping
{
"properties" : {
"last_buy_time": {
"type" : "date",
"format" : "epoch_millis"
}
}
}
##装插件
bin/elasticsearch-plugin install analysis-icu
## 安装elasticsearch-sql 通过 curl elasticsearch 查询的结果 (直接返回结果)
curl -s -H "Content-Type: application/json" -XGET ip:9200/_sql -d "select sum(install_device_count) as total from table_name where hour=9 and staticdate='2021-03-09' and channel_number='oppo'" |jq '.aggregations.total.value'
##elasticsearch 根据 id 删除数据
DELETE /es_index/_doc/id
##修改 eleaticsearch index 副本数
curl -XPUT -H "Content-Type:application/json" http://ip:9200/ex_index/_settings -d '{ "index" : { "number_of_replicas" : "0" }}'
## 刷新os_cache里面的segment 写入文件的时间
curl -XPUT -H "Content-Type:application/json" http://ip:9200/ex_index/_settings -d '{ "index" : { "refresh_interval" : "60s" }}'
##elasticsearch 写入translog 异步
curl -XPUT -H "Content-Type:application/json" http://ip:9200/ex_index/_settings -d '{ "index" : { "translog": { "durability" : "async" }}}'
## 查看elasticsearch es_index mapping
GET es_index/_mapping
## 查看elasticsearch es_index _settings
GET es_index/_settings
## 查询某一个范围内的数据
POST ex_index/_search
{
"query":{
"range" : {
"staticdate" : {
"from" : "2020-12-06",
"to" : "2020-12-06",
"include_lower" : true,
"include_upper" : true,
"boost" : 1.0
}
}
}
,
"size":20
}
## bluk load 写入
POST _bulk
{ "create" : { "_index" : "bulk_index", "_id" : "1" } }
{ "Job" : "Linux Ops" }
{ "delete" : { "_index" : "bulk_index", "_id" : "2" } }
{ "update" : { "_id": "1", "_index" : "bulk_index" } }
{ "doc" : {"name" : "xuweiliang"} }
{ "index" : {"_index" : "bulk_index", "_id" : "1" } }
{ "name" : "xuweiliang" }
{ "create" : { "_index" : "bulk_index", "_id" : "2" } }
{ "name" : "xuweiliang" , "Age" : 25 }
{ "delete" : { "_index" : "bulk_index", "_id" : "2" } }
## 通过function_score 查询打分
POST es_score_test/_search
{
"query": {
"function_score": {
"query": {
"match": {
"category_id":1
}
},
"script_score": {
"script": "return doc ['second_nature_id'].value == 20 ? 1.1 : 0.5"
}
}
}
}
5:hbase常用命令
## hbase bulk 导入数据
hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /data/hfile/${hbaseFileDir} "${tableName}" >> /home/bigdata/logs/hbase-${date}.log 2>&1
## hive 创建hbase 外表
create table `dws.table_name`(
`rowkey` string COMMENT 'rowkey',
`userid` string COMMENT 'userid',
`deviceid` string COMMENT 'deviceid' ,
`last_login_time` string COMMENT 'last_login_time',
`use_count` INT COMMENT 'use_count',
`reserve1` string COMMENT '预留字段1',
`reserve2` string COMMENT '预留字段2'
) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH
SERDEPROPERTIES ("hbase.columns.mapping"=
":key,
cf:userid,
cf:deviceid,
cf:last_login_time,
cf:use_count,
cf:reserve1,
cf:reserve2) TBLPROPERTIES ("hbase.table.name" = "table_name");
##创建hbase表,指定压缩方式,预分区
create 'ads:table_name', {NAME => 'cf', COMPRESSION => 'SNAPPY' , VERSIONS => 3},SPLITS => ['10','15','20','25','30','35','40','45','50']
6:flink常用命令
nohup flink run -m yarn-cluster -yjm 1024m -ytm 2048m -ynm SyncStream -ys 1 -yqu queueName -c com.xxx.SyncStream /data/sync-datapretreatment.jar $1 > /data/SyncStream.log 2>&1 &
7:spark常用命令
## 设置执行队列
set spark.yarn.queue=root.default;
set spark.app.name=merge;
##动态资源
set spark.dynamicAllocation.maxExecutors=10;
set spark.dynamicAllocation.enabled=false;
##分区设置
set hive.exec.max.dynamic.partitions=10000;
set hive.exec.max.dynamic.partitions.pernode=10000;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
##执行资源
set spark.executor.instances=5;
set spark.executor.memory=6g;
set spark.driver.memory=3g;
set spark.executor.cores=4;
set spark.memory.offHeap.size=2g;
#文件合并自适应
set spark.sql.adaptive.shuffle.targetPostShuffleInputSize=134217728;
set spark.sql.adaptive.enabled=true;
## 后台常驻提交spark 任务
nohup spark-submit --master yarn --deploy-mode cluster --driver-memory 3g /opt/product/sparkjob/datacalculate.jar >> /opt/product/sparkjob/log/datacalculate.log 2>&1 &
8:kylin常用命令(小文件清理)
##检查哪些资源可以清理,(--delete false)不会删除任何文件:
export KYLIN_HOME=/path/to/kylin_home
${KYLIN_HOME}/bin/kylin.sh org.apache.kylin.tool.StorageCleanupJob --delete false
##可以抽查相关文件来检查它们是否还在使用,确认没有问题以后,加上'–delete true'选项进行清理。
${KYLIN_HOME}/bin/kylin.sh org.apache.kylin.tool.StorageCleanupJob --delete true
完成后,Hive 里的中间表, HDFS 上的中间文件及 HBase 中的 HTables 都会被移除。
## 如果您想要删除所有资源;可添加 '–force true' 选项:
${KYLIN_HOME}/bin/kylin.sh org.apache.kylin.tool.StorageCleanupJob --force true --delete true
完成后,Hive 中所有的中间表, HDFS 上所有的中间文件及 HBase 中的 HTables 都会被移除。
9:clickhouse常用命令
##令行连接
clickhouse-client -m --port 9002 -u default --password xxx -h xxx
##表添加字段
alter table table_name on cluster cluser_name modify column user_name Nullable(String);
#设置本地表 TTL
ALTER TABLE default.tablename on cluster cluser_name modify TTL staticdate + INTERVAL 1 Year;
##删除集群数据
clickhouse-client --port 9002 -u default --password xxx -h 127.0.0.1 --query "alter table default.table_name on cluster cluser_name delete where staticdate >= '${1}'"
## 创建本地表
CREATE TABLE default.tablename on cluster cluser_name
(
distinct_id String,
event String,
staticdate Date,
project String,
event_date_time Nullable(DateTime)
)
ENGINE = MergeTree
PARTITION BY formatDateTime(staticdate, '%Y-%m')
ORDER BY (event, staticdate,project, javaHash(distinct_id))
SETTINGS index_granularity = 8192
,storage_policy = 'hdd_in_order';
## 创建分布式表
CREATE TABLE default.tablename_all on cluster cluser_name
(
distinct_id String,
event String,
staticdate Date,
project String,
event_date_time Nullable(DateTime)
) ENGINE = Distributed('cluser_name', 'default', local_table_name, rand());
10:redis常用命令
##redis 清除缓存(慎用)
redis-cli -p 6379 -a '密码' -n 0 keys "*" | xargs redis-cli -p 6379 -a '删除xxx开头key' -n 0 del
## 查询bigkey
redis-cli -h ip -p port -a 密码 --bigkeys -i 0.01
3:总结
熟记线上常用命令只能加深你对组件的认识,线上的问题处理也需要你深入理解相关业务场景。把业务场景的理解与技术解决方案结合起来,相信你在处理相关问题的时候会游刃有余。 临时整理,可能存在一定的遗漏,后续持续不断补充。