Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 31 章 Software Development Kit

目錄

31.1. Google
31.1.1. com.google.gson
31.1.1.1. map 處理
31.1.1.2. POJO
31.1.1.3. toJson
31.1.1.4. fromJson
31.1.1.5. JsonParser
31.1.1.6. Exmaple 範例
31.1.1.6.1. Map to Json
31.1.1.7. Exmaple 範例
31.1.1.7.1. Map to Json
31.1.1.8. 處理複雜的類型
31.1.2. Guava
31.1.2.1. maven
31.1.2.2. 刪除不可顯示的字元
31.2. Mahout
31.2.1. 推薦系統
31.2.1.1. Maven pom.xml
31.2.1.2. 推薦程序
31.2.1.3. 數據檔案
31.3. Hessian
31.4. quartz-scheduler
31.5. Redisson

31.1. Google

31.1.1. com.google.gson

https://github.com/google/gson

		
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>sender</groupId>
	<artifactId>sender</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Sender</name>
	<description>EDM</description>

	<dependencies>
		<dependency>
			<groupId>com.rabbitmq</groupId>
			<artifactId>amqp-client</artifactId>
			<version>3.6.0</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.6.2</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

</project>		
		
		

31.1.1.1. map 處理

Example

			
package io.github.netkiller;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.*;

public class GsonTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Gson gson = new Gson();
		String json = "{\"k1\":\"v1\",\"k2\":\"v2\"}";
		Map<String, String> map = new HashMap<String, String>();
		map = (Map<String, String>) gson.fromJson(json, map.getClass());
		System.out.println(map.get("k1"));
	}

}
			
			

多層 Map 剝離

			
		Gson gson = new Gson();
		String inf= "{\"0\":{\"id\":\"2\",\"category_id\":\"1\",\"title\":\"Test2\",\"author\":\"\",\"ctime\":\"2016-03-05 11:59:56\"},\"1\":{\"id\":\"1\",\"category_id\":\"1\",\"title\":\"Test1\",\"author\":\"\u6d4b\u8bd5\",\"ctime\":\"2016-03-05 11:57:30\"},\"pages\":{\"count\":2,\"first\":0,\"last\":0,\"before\":0,\"current\":0,\"next\":0,\"total\":0}}";
		Map<String, Map> map = new HashMap<String, Map>();
		
		map = (Map<String, Map>) gson.fromJson(inf, map.getClass());
		System.out.println(map.get("1").get("title"));
		System.out.println(map.get("pages").get("count"));
			
			

31.1.1.2. POJO

			
package cn.netkiller.gson;

import java.util.ArrayList;
import java.util.List;

public class Personal {
	private int age = 30;
	private String name = "neo";
	private List<String> telphone = new ArrayList<String>() {
		{
			add("13113668890");
			add("13322993040");
			add("29812080");
		}
	};

	// getter and setter methods

	@Override
	public String toString() {
		return "Personal [age=" + age + ", name=" + name + ", telphone=" + telphone + "]";
	}
}
			
			

31.1.1.3. toJson

			
package cn.netkiller.gson;

import com.google.gson.Gson;

public class GsonExampleToJson {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Personal obj = new Personal();
		Gson gson = new Gson();

		// convert java object to JSON format, and returned as JSON formatted string
		String json = gson.toJson(obj);
		System.out.println(json);
	}

}
			
			

31.1.1.4. fromJson

			
package cn.netkiller.gson;

import com.google.gson.Gson;

public class GsonExampleFromJson {
	public static void main(String[] args) {

		Personal obj = new Personal();
		Gson gson = new Gson();

		// convert the json string back to object
		obj = gson.fromJson("{\"age\":30,\"name\":\"neo\",\"telphone\":[\"13113668890\",\"13322993040\",\"29812080\"]}", Personal.class);

		System.out.println(obj);
	}
}
			
			

31.1.1.5. JsonParser

			
package cn.netkiller.gson;

import java.util.Map.Entry;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

public class GsonJsonParser {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String jsonString = "{\"age\":30,\"name\":\"neo\",\"telphone\":[\"13113668890\",\"13322993040\",\"29812080\"],\"address\":{\"province\":\"Guangdong\",\"city\":\"Shenzhen\"}}";

		JsonElement root = new JsonParser().parse(jsonString);
		System.out.println(root.toString());
		System.out.println(root.getAsJsonObject().get("age").getAsInt());
		System.out.println(root.getAsJsonObject().get("name").getAsString());

		// Get the content of the first map
		JsonArray jsonArray = root.getAsJsonObject().get("telphone").getAsJsonArray();

		for (JsonElement tel : jsonArray) {
			System.out.println(tel);
		}

		JsonObject object = root.getAsJsonObject().get("address").getAsJsonObject();
		for (Entry<String, JsonElement> entry : object.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
	}

}
			
			

31.1.1.6. Exmaple 範例

31.1.1.6.1. Map to Json
				
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
String jsonString = gson.toJson(map);
				
				

31.1.1.7. Exmaple 範例

31.1.1.7.1. Map to Json
				
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
Map<String, String> source = gson.fromJson(output, HashMap.class);
				
				

31.1.1.8. 處理複雜的類型

通過 TypeToken 定義複雜資料庫類型。

			
package cn.netkiller.ipo.process.json;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import cn.netkiller.ipo.process.ProcessInterface;

public class JsonValueLength implements ProcessInterface {
	private final static Logger logger = LoggerFactory.getLogger(JsonValueLength.class);
	private Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
	private int maxLength = 0;

	public JsonValueLength(int maxLength) {
		this.maxLength = maxLength;
	}

	@Override
	public String run(String line) {

		Map<String, String> map = gson.fromJson(line, new TypeToken<Map<String, String>>() {
		}.getType());
		for (String key : map.keySet()) {
			if (map.get(key).length() > this.maxLength) {
				map.put(key, map.get(key).substring(0, this.maxLength));
			}
		}
		logger.debug("{}", map);
		return gson.toJson(map);
	}

}

			
			

31.1.2. Guava

https://github.com/google/guava/wiki

31.1.2.1. maven

			
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>23.0</version>
		</dependency>			
			
			

27 版本需要指定 27.1-jre,如果是安卓系統 27.1-android

			
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>27.1-jre</version>
		</dependency>			
			
			

31.1.2.2. 刪除不可顯示的字元

			
package cn.netkiller.test;

import com.google.common.base.CharMatcher;

public class Test {

	public static void main(String[] args) {

		String string = "佛山市南海區123華泰ABC精密abc機械有限公司消防維保􁵪􁻠􁴹􄲀􀞜􀨨,";

		// 版本 23.0
		// String printable = CharMatcher.INVISIBLE.removeFrom(string);
		// String clean = CharMatcher.ASCII.retainFrom(printable);

		String printable = CharMatcher.invisible().removeFrom(string);
		String clean = CharMatcher.ascii().retainFrom(printable);
		System.out.println(printable);
		System.out.println(clean);


	}

}