Elasticsearch配置ik分词器

假定你已经安装了elasticsearch2.1.0和maven, 下面的步骤针对elasticsearch2.1.0有效, 其它版本可能不使用

下载ik

因为我的elasticsearch是2.1.0,所以去这里下载https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v1.6.1

其它版本的elasticsearch对应的ik去https://github.com/medcl/elasticsearch-analysis-ik 下载

解压ik,并进入ik

执行 mvn package 编译ik

复制文件

将elasticsearch-analysis-ik-master/target/releases/elasticsearch-analysis-ik-1.6.1.zip 拷贝到es的plugin/ik目录下并解压

同时将elasticsearch-analysis-ik-master/config/ik文件夹拷贝到es的config下

然后重启es,就可以了.

Java发送邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.google.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
*/
public class EmailUtil {

private static Properties properties = null;

public static void init(Properties properties)
{
EmailUtil.properties = properties;
}

public static void send(String subject, String message) {
try {
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String userName = properties.getProperty("mail.user");
String password = properties.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, authenticator);
MimeMessage mime = new MimeMessage(session);
InternetAddress form = new InternetAddress(properties.getProperty("mail.user"));
mime.setFrom(form);
InternetAddress to = new InternetAddress(properties.getProperty("main.to"));
mime.setRecipient(RecipientType.TO, to);

mime.setSubject(subject);
mime.setContent(message, "text/html;charset=UTF-8");

Transport.send(mime);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "smtp.exmail.qq.com");
properties.put("mail.user", "user1@example.com");
properties.put("mail.password", "password");
properties.put("main.to","user2@qq.com");

EmailUtil.init(properties);
EmailUtil.send("hello", "这是来自大洋彼岸的声音!");
}

}

将启动命令中包含某个字符串的进行杀死

1
2
3
4
[root@dn01 testbash]# ps -ef | grep hello
root 24794 7457 0 11:09 pts/2 00:00:00 sh hello.sh
root 24910 7457 0 11:09 pts/2 00:00:00 sh hello.sh
root 25295 7457 0 11:11 pts/2 00:00:00 grep hello

使用

1
ps aux | grep hello | grep -v grep |awk '{print $2}'| xargs kill -9

可以将上述的前两个进程杀死

Swift - 使用NSURLSession同步获取数据(通过添加信号量)

首先非常感谢代码的原作者,为了查询方便我就在这里又留了一份,请见谅.

原文http://www.hangge.com/blog/cache/detail_816.html

过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据。从iOS9起,苹果建议废除 NSURLConnection,使用 NSURLSession 代替 NSURLConnection。

如果想要 NSURLSession 也能够同步请求,即数据获取后才继续执行下面的代码,使用信号、信号量就可以实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//创建NSURL对象
let urlString:String="http://www.hangge.com"
let url:NSURL! = NSURL(string:urlString)
//创建请求对象
let request:NSURLRequest = NSURLRequest(URL: url)

let session = NSURLSession.sharedSession()

let semaphore = dispatch_semaphore_create(0)

let dataTask = session.dataTaskWithRequest(request,
completionHandler: {(data, response, error) -> Void in
if error != nil{
print(error?.code)
print(error?.description)
}else{
let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(str)
}

dispatch_semaphore_signal(semaphore)
}) as NSURLSessionTask

//使用resume方法启动任务
dataTask.resume()

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
print("数据加载完毕!")
//继续执行其他代码.......

使用Swift解析json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let jsonObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
print(jsonObject)

let array = jsonObject as! NSArray

//读取数组中某个key所对应的所有值
print(array.valueForKey("text"))

//读取第一个元素
print(array[0])

//读取第一个元素的key对应的值
let text = array[0].valueForKey("text")
print(text)

//在使用if let语句的时候,swift会自动进行拆包
if let state = array[0].objectForKey("state") {
print(state)
}

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(
{
id = 1;
state = closed;
text = "Node 1";
},
{
id = 2;
state = open;
text = "Node 2";
},
{
id = 3;
state = open;
text = "Node 3";
},
{
id = 4;
state = open;
text = "Node 4";
}
)
(
"Node 1",
"Node 2",
"Node 3",
"Node 4"
)
{
id = 1;
state = closed;
text = "Node 1";
}
Optional(Node 1)
closed