Showing posts with label KB. Show all posts
Showing posts with label KB. Show all posts

Wednesday, April 15, 2020

Error vs Fault 的区别

Error: An understood failure mode that indicates the request was flawed (duplicate device name)
Fault: a failure mode where the request is valid but application failed anyway

Error对应4xx,表示client或者input有问题
Fault对应5xx,表示service或者output有问题

一般而言,应该分类,alarm只应该对fault,有时候不确定时候就先保守地归类为Fault,后面转为Error。

Friday, February 24, 2017

Mac快捷键

Mac Keyboard Win Keyboard(swap ctrl&command)
Control + Shift + Power Control + Alt + Scroll Lock 锁屏
Command+space Ctrl+Space 全局搜索Spot Search
Fn+Down Page Down 下一页
Fn+Delete Delete 删除游标后的字符





Ctrl+Space win+Space 切换输入法
Fn+F11 F11 显示桌面
Command+tab Ctrl+tab app切换
Command+win ctrl+win tab切换
Command+win+F ctrl+win+F 全屏/退出全屏
Command+M ctrl+M 最小化窗口
win+mouse scroll 放大word
Command+Plus ctrl+Plus 放大Chrome
command+shift+4 ctrl+shift+4 截屏
command+shift+r/shift
reload button

hard refresh无cache的加载
⌘command
⌥option
⇧shift
⇥tab
⌃control
命令行
Ctrl+R win+R 搜索历史命令
Ctrl+A win+A 游标回到头
Ctrl+b
上一页
Ctril+f
下一页
Ctril+d
删除改行
shift+g shift+g 打开的文件跳到末尾
shift+z+z shift+z+z vim保存且退出(=:wq)
shift+z+q shift+z+q vim不保存且退出(=:q)
dd vim删除一行(非插入状态)
p vim复制刚才dd剪切的一行(非插入状态)
IntelliJ
command+O Ctrl+O 查找文件
command+alt+右箭头 ctrl+alt+右箭头 后一步Navigate forward
command+alt+左箭头 ctrl+alt+左箭头 前一步Navigate backward
option+回车 加入import解决symbol

Wednesday, February 22, 2017

Linux常用命令

grep是查找某个关键字或正则表达式在某个文件某一行的匹配,结果为匹配的行及文件

返回含abc的匹配行:
zgrep "abc" application.log

在多个文件中查找:*.txt或者{}.txt
grep 'linux' {a,b}.txt

先返回含Exception的匹配行再返回含Hulu的匹配行,也就是同时含这两次的匹配行:
zgrep "Exception" */application*.log | grep "Hulu" 在application log中含Exception以及Hulu

按文件统计出现行数 -c
grep -c 'linux' *.txt

统计全部文件总行数=次数:
zgrep "Exception" */application*.log | grep "Hulu" | wc -l 数出现次数

关键词也可以为正则表达式:
vim中 /main 搜索main关键字,可以带正则表达式:如Time=[\d]{4,4}\.[\d]* ms
   按键盘n表示下一个搜索结果,按键盘N表示上一个搜索结果。
grep也可以带正则表达式:grep -c 'Time=[\d]{4,4}\.[\d]* ms' *service
grep 'linux\|win' a.txt

grep -c 'Time=[\d]{4,4}\.[\d]* ms' *service| sort | uniq -c | sort -rn |head 按文件统计词频且按大到小排序

grep是行匹配,而awk是词匹配(按空格)
打印所有行的第一个词(默认按空格划分),其他词不输出
awk '{print $1}' a.txt

自定义分隔符,如逗号
awk -F',' 'print $2' a.txt

排序
sort -r 递降排序结果
sort -u 去重且排序

进程操作
netstat -tupln | grep 1066得到要终结进程的id
kill -9 16085删除进程by id
ps aux | grep 1066查看谁在用1066端口
查找操作
grep -color=auto "the" foo.txt[文件内容]在某一个文件查找某字符串并打印结果行
-i 大小写敏感
-A 打印后几行
-B 前几行
-C 周围几行
-P 匹配正则表达式 "url=.*?;" 问号表示分号不加入匹配结果
-o 只显示匹配string
-c 匹配结果行数 或|wc -l
-v 不含某字符串
-n 显示行号
grep -r "the" * 递归在所有文件中查找某字符串
grep -A 3 -i "moon" foo.txt 在某一个文件查找一个字符串并打印紧接的3行
  | head 查看前10行
  | sed "s/my/your/" 替换(用your替my)
find . -iname "odin-get"[文件名]搜索文件名通过名字
find ./Test -user kk 搜索文件名通过用户
文件操作
less foo.txt高效率打开大文件。CTRL+F下一页,CTRL+B上一页. SHIFT+G文件底部
cat > foo.txt创建文件。CTRL+D保存文件
cat foo.txt查看文件
vim foo.txt创建/编辑一个文件
/main 搜索main关键字
i/insert 插入模式,ESC退出插入模式
:q 或者Ctrl+z+q不保存退出
:wq 保存退出
rm foo.txt删除文件
mv -i foo.txt Test/foo.txt移动/重命名文件
cp -i foo.txt Test/foo.txt复制一个文件
scp ~/a.txt jiajicen@vtasters.com:~/a.txt复制一个文件到另一个host
scp -r jiajicen@vtasters.com:~/test ~/test复制一个文件夹到另一个host. test文件夹不需要预先创建
tail -f app.log即时打印最新的log,方便同步debug
目录操作
cd ../root/Docements 绝对路径
./path 当前目录
../path 父目录
~/Desktop 根目录/Users/kk/Desktop
~john/bin 用户目录/Users/john/bin
ls -ltr-ltr 按时间排序
-lh 显示大小权限
-alh 显示隐藏文件
pwd显示当前目录
mkdir Test创建目录
rm -r Test删除目录
环境变量
export PATH="/home/Application/Java/bin:$PATH"加入环境变量到内存,然后执行source加入到硬盘再重启命令行。
一次性操作为export PATH=$PATH:/home/Application/Java/bin >> ~/.bash_profile
source ~/.bash_profile (Mac)加入到硬盘。Linux为~/.zshrc
echo $HOME打印某环境变量
which java得到某个命令如java路径,也是查找在~/.zshrc此命令的路径
系统操作
clear清屏
sudo reboot重启
sh abc.sh执行一个程序,可能需要先提高权限chmod +x abc.sh
有时需要加点如./mvn才能执行,通过加入到.bash_profile可以避免
su Javasu=sudo, 用超级用户执行程序。
ssh vtasters.com远程登录
nslookup通过IP查找其hostname


agrep 文件每个block以EOF结束,agrep可以block查找


zshrc文件:
~/.zshrc
alias vm01="ssh ubuntu@172.31.29.246"

然后
echo "alias vps='ssh -X user@example.com'" >> ~/. zshrc
source ~/.zshrc
就可以输入vm01登录

Linux文件权限:
Linux权限为读写执行r, w, x.
Linux权限粒度为拥有者u,群组g,其他组o。
4,2,1表示读、写、执行权限。rwx=4+2+1=7, rw=4+2=6, rx=4+1=5

chmod 777 file.txt (<=> chmod u=rwx, g=rwx, o=rwx)
chmod 600 file.txt (<=> chmod u=rw, g=---, o=---)

查看字符
先copy到这个网站得到它的ascii码
然后查找

ssh问题
尝试ssh到某个host得到此错误Could not resolve host name但可以ping
尝试用ssh <host> -o ProxyCommand=none

Monday, February 2, 2015

KB: C#

AppSettingsReader:
Read app.config file

XmlDocument & XmlNode (Read XML file):
XmlDocument configDoc = new XmlDocument();
configDoc.Load(fileName);
XmlNode oneNode = configDoc.SelectSingleNode("//config/");
String abc = botNode.Attributes["proxy"];

Attribute & CustomAttributeData:
FieldInfo[] fs = obj.GetType().GetFields();
foreach (FieldInfo f in fs)
{
IList<CustomAttributeData> attributes = CustomAttributeData.GetCustomAttributes(f);
        foreach (CustomAttributeData attribute in attributes)
        {
             //check if the field was marked as [ConfigValue]
             if (attribute.Constructor.DeclaringType.Equals(typeof(ConfigValue)))
    {}
}
}



    [AttributeUsage(AttributeTargets.Field)]
    public class ConfigValue : Attribute
    {
        private String prameName;
        public ConfigValue(String name)
        {
            prameName = name;
        }

    }

    [AttributeUsage(AttributeTargets.Field)]
    public class RegexFile : Attribute
    {
        private String fileName;
        public RegexFile(String name)
        {
            fileName = name;
        }
    }


Use:
        [ConfigValue]
        String proxy;

        [RegexFile("rabc.regex")]
        private Regex rabc;

Friday, January 16, 2015

KB: VB

VBScript:
aa.bat:
rem wscript abc.vbs male
cscript abc.vbs male

abc.vbs:
set wnet =CreateObject("WScript.Network")
uname=wnet.UserName
uage = InputBox("Please enter your age")
gender = WScript.Arguments(0)
MsgBox "Your user name is " & uname & "," & uage & "," & gender

Monday, September 15, 2014

KB: SQL Server DBA queries

Get the running queries:
select r.session_id,r.status,r.start_time,r.total_elapsed_time,r.command,s.text
from sys.dm_exec_requests r
cross apply  sys.dm_exec_sql_text(r.sql_handle) s
--where r.status='running'

Get linked servers:
exec sp_linkedservers

Saturday, April 26, 2014

KB: Java之反射

import java.lang.reflect.*;

1.得到类名:Class c = Class.forName(this.getClass().getName().toString()); this为instance,如果这句在基类,类,getClass会得到给子类。class name是包括包名的,比如com.vtasters.entity.Car Car是类名
2. 得到某类(this)的某属性:c.getField("location").get(this),如果该属性是static,就如下c.getField("location").get(null)
3. 设置某类(this)的某属性:c.getField("location").set(this, "Jersey city");
4. 获得某类某属性的类型:
Field fields[] = c.getDeclaredFields();
for(Field f : fields)
{
            f.getType()
5. 调用方法:
    Class cls=null;
    cls=Class.forName("com.vtasters.Dish");
    Object obj=cls.newInstance();
    Method method=cls.getMethod("Test",null);
    method.invoke(obj);
    这里调用一个没有参数叫Test的方法

     Class cls=null;
  cls = Class.forName("com.framework.Action");

  Object obj = cls.newInstance();

  Class[] cArg = new Class[2];
  cArg[0] = int.class;
  cArg[1] = String.class;
  Method method2 = cls.getMethod("bbb", cArg);
  Object returnType = method2.getReturnType();
  Object value = method2.invoke(obj,5,"6");
  System.out.println(returnType.toString()+":"+value.toString());

    这里调用一个带参数和返回值叫bbb的方法,定义如下
    int bbb(int a, String b){}

    注意,有时一些内部类带的属性或方法,不能在被调用的方法(e.g. bbb)中使用,要作为参数输入,否则会出现Reflection异常

如果参数个数是动态,就这样写:
       String[] mainArgs = new String[5];
      method.invoke(null, (Object)mainArgs);
getDeclaredMethod可以得到protected的方法,但getMethod只能得到公共方法

6. 返回值
Object value = method.invoke(this, x);
                d = (Double) value;

7. 调用构造函数
            cls = Class.forName("com.itg.generic.Events");
            Class[] constructorArg = new Class[1];
            constructorArg[0] = Operator.class;
            Object obj = cls.getDeclaredConstructor(constructorArg).newInstance(this);

8. 判断一个数据是否实例
    Num param instanceof Double

a=="mystr"不对,要用a.equals("mystr")

Wednesday, April 23, 2014

KB: c#之反射

有时为了使相似度较高的代码智能化,我们会使用到反射。
这是Reflexion的命名空间using System.Reflection;

前面的blog说到C#之Entity framework,提到这段代码:

using (ModelEntities context = new ModelEntities())
{
     context.stocks_EF.Add(new stocks_EF() { Symbol = "CAT", Price = 98.42, PricePct = 2.5, Volume = 29389, AvgVolume = 39392, MktCap = "56.5B", Yield = 2.2, PE = 10.2, TradeDate = Convert.ToDateTime("2013-06-17 00:00:00.000") });
     context.SaveChanges();
}


    public partial class ModelEntities : DbContext
    {
        public DbSet<stocks_EF> stocks_EF { get; set; }

    }

下面我说说怎么用反射写这段代码。
1.已知道类型的Type:Type contextType = typeof(ModelEntities);如果是某instance的type是    
  obj.GetType()
2. 创建某类型的Instance:object context = Activator.CreateInstance(contextType);
3. 获得某Instance的某属性的值:
  object dbSetInstance = contextType.GetProperty(propName).GetValue(context);
4. 获得某Instance的某属性的类型:
    Type dbSetType = contextType.GetProperty(propName).PropertyType;
5.获得某类型的某方法:MethodInfo objAddMethod = dbSetType.GetMethod("Add");
6.Call某方法: objAddMethod.Invoke(dbSetInstance, new object[]{obj});
(eg2)7.获得泛型的参数类型
                if (propType.IsGenericType)
                {
                    Type[] paraList = propType.GetGenericArguments();
                    propertyName = paraList[0].Name;

                }
8.设置某属性的值:t.GetProperty(name).SetValue(obj, "abd")


Type contextType = typeof(ModelEntities);
object context = Activator.CreateInstance(contextType);
Type objType = obj.GetType();

var propNames = from p in contextType.GetProperties() select p.Name;

foreach (var propName in propNames)
{
    if (objType.ToString() == contextType.Namespace+"."+propName)
    {
         object dbSetInstance = contextType.GetProperty(propName).GetValue(context);
         Type dbSetType = contextType.GetProperty(propName).PropertyType;

         MethodInfo objAddMethod = dbSetType.GetMethod("Add");
         objAddMethod.Invoke(dbSetInstance, new object[] { obj });

         MethodInfo objSaveMethod = contextType.GetMethod("SaveChanges");
         objSaveMethod.Invoke(context, null);
     }
}

再训练一个:
    public partial class stocks_EF
    {
        public int ID { get; set; }
        public string Symbol { get; set; }
        public Nullable<double> Price { get; set; }
        public Nullable<double> PricePct { get; set; }
        public Nullable<int> Volume { get; set; }
        public Nullable<int> AvgVolume { get; set; }
        public string MktCap { get; set; }
        public Nullable<double> Yield { get; set; }
        public Nullable<double> PE { get; set; }
        public Nullable<System.DateTime> TradeDate { get; set; }
        public Nullable<System.DateTime> InsertTime { get; set; }

    }


code如下:
public static void Fill(string[] words, object obj)
{
    Type t = obj.GetType();
    var propNames = from p in t.GetProperties() select p.Name;
    int i=0;
    foreach (var name in propNames)
    {
        Type propType = t.GetProperty(name).PropertyType;
        string propertyName = propType.Name;

        if (propType.IsGenericType)
        {
            Type[] paraList = propType.GetGenericArguments();
            propertyName = paraList[0].Name;
        }
        if (propertyName == "DateTime")
            t.GetProperty(name).SetValue(obj, Convert.ToDateTime(words[i]));
        else if (propertyName == "Int32")
            t.GetProperty(name).SetValue(obj, Convert.ToInt32(words[i]));
        else if (propertyName == "Double")
            t.GetProperty(name).SetValue(obj, Convert.ToDouble(words[i]));
        else t.GetProperty(name).SetValue(obj, words[i]);
        i++;
    }


}

main函数:
string[] words = { "1", "CAT", "98.43", "2.5", "29389", "39392", "56.5B", "2.2", "10.2", "2013-06-17 00:00:00.000", "2014-04-23 00:00:00.000" };
 stocks_EF s = new stocks_EF();
 Fill(words, s);