DdbTest.java 10.4 KB
Newer Older
shenggui.li committed
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
package com.reyun.service.impl;


import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.*;
import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import com.amazonaws.services.dynamodbv2.model.ReturnValue;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.reyun.util.DynamoDBUtil;
import com.reyun.util.NoSQLDBUtil;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;


/**
 * DynamoDB工具类
 * @author liruijie@reyun.com
 * @date 2016年11月15日
 */
public class DdbTest {
	public static final String TBL_TKIO_INSTALL = "tkio_install";
	public static final String TBL_TRACK_CLICK = "track_click";
	public static final String TBL_TRACK_CLICK_SUBCHANNEL = "track_click_subchannel";
	public static final String TBL_TABLE_SURL = "tkio_surl";
	public static final String TBL_TKIO_DEBUG = "tkio_debug";
	public static final String TBL_WX_APP = "tkio_wx_app";//存储微信的appid与secret

	private static DynamoDB dynamoDB;
	private static AmazonDynamoDBClient client ;

	private DdbTest() {
		//was
		/*String accessKey = "AKIAPMJ4QRXP6BECFUVQ";
		String secretKey = "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";
		String region = "cn-north-1";*/
		//test
		String accessKey = "AKIAOS2UEXIFNVER2O6A";
		String secretKey = "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";
		String region = "cn-north-1";

		if ((accessKey != null) && (secretKey != null)) {
			AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

			ClientConfiguration config = new ClientConfiguration();
			String proxyHost = System.getProperty("http.proxyHost");
			String proxyPort = System.getProperty("http.proxyPort");
			if (proxyHost != null && proxyPort != null) {
				config.setProxyHost(proxyHost);
				config.setProxyPort(Integer.valueOf(proxyPort));
			}

			if (client == null){
				client = new AmazonDynamoDBClient(credentials, config);
				//client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(region)));
				client.withRegion(Regions.fromName(region));
			}
			dynamoDB = new DynamoDB(client);
		}
	}



	public static DdbTest getInstance() {
		return DynamoDBUtilHandler.dynamoDBUtil;
	}

	public DynamoDB getDynamoDB() {
		return dynamoDB;
	}

	public Table getTable(String table) {
		return dynamoDB.getTable(table);
	}

	private static class DynamoDBUtilHandler {
		private static final DdbTest dynamoDBUtil = new DdbTest();
	}

	public static void main(String[] args) throws InterruptedException {
		//String url="https://campaignDetail.link.trackingio.com/ry/campaignDetail/path?download=http%3A%2F%2Fbaidu%2Ecom";
		/*String url="https://campaignDetail.link.trackingio.com/ry/campaignDetail/path?ry_dp=weixin%3A%2F%2Fdl%2Fprofile&download=http%3A%2F%2Fbaidu%2Ecom";
		String substring = url.substring(url.indexOf("download=")+"download=".length());
		System.out.println(UrlEncoded.decodeString(substring,0,substring.length(),"UTF-8"));
		if(url.contains("ry/")){
			String substring1 = url.substring(url.indexOf("ry/")+"ry/".length(), url.indexOf("?"));
			System.out.println(substring1);
		}
		if(url.contains("ry_dp=")){
			String substring2 = url.substring(url.indexOf("ry_dp=")+"ry_dp=".length(),url.indexOf("&download"));
			System.out.println(UrlEncoded.decodeString(substring2,0,substring2.length(),"UTF-8"));
		}*/

		Map<String, Object> debugItem = new HashMap<>();
		debugItem.put("appid", "123");
		//isDebug 改为字符串
		debugItem.put("isDebug", "true");

		debugItem.put("num", 0);

		Table tableModel = DdbTest.getInstance().getTable(NoSQLDBUtil.TBL_TKIO_DEBUG);

		//final PutItemOutcome putItemOutcome = tableModel.putItem(Item.fromMap(debugItem));

		UpdateItemSpec updateItemSpec = new UpdateItemSpec()
				.withPrimaryKey("appid", "123")
				.withUpdateExpression("set  isDebug=:idb")
				.withValueMap(new ValueMap().withString(":idb","true"))
				.withReturnValues(ReturnValue.UPDATED_NEW);
		tableModel.updateItem(updateItemSpec);

		/*List<String> strings = Arrays.asList("1,2".split(","));
		for (String string : strings) {
			System.out.println(string);
		}*/
		//Table tableModel = DdbTest.getInstance().getTable(DynamoDBUtil.TBL_TABLE_DEEPLINK);
        /*//System.out.println(tableModel.getTableName());
		DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
				.withPrimaryKey(new PrimaryKey("linkid", "741"));
		DeleteItemOutcome deleteItemOutcome = tableModel.deleteItem(deleteItemSpec);
		System.out.println(deleteItemOutcome.getDeleteItemResult());*/
		//创建表
		/*DynamoDB dynamoDB = DdbTest.getInstance().getDynamoDB();
		Table table = dynamoDB.createTable("tkio_deeplink", Arrays.asList(new KeySchemaElement("linkid", KeyType.HASH)), Arrays.asList(new AttributeDefinition("linkid", ScalarAttributeType.S)), new ProvisionedThroughput(200L, 200L));
		table.waitForActive();
		System.out.println(table.getDescription().getTableStatus());*/
		//创建安卓项目
		/*Table table = DdbTest.getInstance().getTable("tkio_deeplink");
		Item item = new Item()
				.withPrimaryKey("linkid","742")
				.withString("app_download","https://0x9.me/uieIO")
				.withString("sha256","82:2C:3F:E7:DC:25:13:26:FE:52:40:7D:09:67:94:44:DB:17:4F:40:7B:5A:25:07:B5:E8:09:15:D8:02:BA:F5")
				.withString("package_name","com.example.reyunsdkdemo")
				.withString("flag","android")
				.withString("mydomain","https://742.link.trackingio.com")
				.withString("url_scheme","https://123.link.trackingio.com/dp2");
		PutItemOutcome outcome = table.putItem(item);
		System.out.println(outcome.getPutItemResult());*/
		//创建ios项目
		/*Table table = DdbTest.getInstance().getTable("tkio_deeplink");
		Item item = new Item()
				.withPrimaryKey("linkid","741")
				.withString("app_download","https://0x9.me/uieIO")
				.withString("app_id_prefix","V7RCSA45T2")
				.withString("bundle_id","com.reyun.trackingio")
				.withString("flag","ios")
				.withString("mydomain","https://741.link.trackingio.com")
				.withList("paths", Arrays.asList("/ryios/testpath2","/ryios/testpath1/path2/"))
				.withString("url_scheme","great://testpath1/path2");
		PutItemOutcome outcome = table.putItem(item);
		System.out.println(outcome.getPutItemResult());*/
       /* //根据主键查找并修改数组
		GetItemSpec spec = new GetItemSpec().withPrimaryKey("testid","a3");
		System.out.println(tableModel.getItem(spec).getList("b"));
		UpdateItemSpec updateItemSpec = new UpdateItemSpec();
		updateItemSpec.withPrimaryKey("testid", "a3");
		updateItemSpec.withUpdateExpression("set b = :b");
		List<Object> b = tableModel.getItem(spec).getList("b");
		b.add("4");

		updateItemSpec.withValueMap(new ValueMap().withList(":b", b));
		updateItemSpec.withReturnValues(ReturnValue.UPDATED_NEW);
		UpdateItemOutcome outcome = tableModel.updateItem(updateItemSpec);
		System.out.println(outcome.getItem().toJSONPretty());*/
        /*//创建项目
		final Map<String, String> infoMap = new HashMap<>();
		infoMap.put("b","c");
		infoMap.put("c","55");
		PutItemOutcome outcome = tableModel.putItem(new Item().withPrimaryKey("testid", "a3").with("b", "c").with("c", "55"));
		System.out.println(outcome.getPutItemResult());*/
        /*UpdateItemSpec updateItemSpec = new UpdateItemSpec()
				.withPrimaryKey("linkid", "nihao_")
				.withUpdateExpression("REMOVE app_download,app_w")
				.withReturnValues(ReturnValue.UPDATED_NEW);
		tableModel.updateItem(updateItemSpec);*/
		//Map<String, Object> wxAppItem = new HashMap<>();
		//wxAppItem.put("id", "afbb574bc0e1d7326fc92140399509ef");
		//PutItemOutcome putItemOutcome = tableModel.putItem(Item.fromMap(wxAppItem));
		//System.out.println(putItemOutcome.getPutItemResult().getSdkHttpMetadata().getHttpStatusCode() == 200);
       /* String s = UrlEncoded.encodeString("weixin://dl/profile");
        s=UrlEncoded.decodeString("greatapp%3A%2F%2Ftestpath1%2Fpath2",0,"greatapp%3A%2F%2Ftestpath1%2Fpath2".length(),"UTF-8");
        System.out.println(s);*/
    }


	public static void signUrl() {
		//was
		/*String accessKey = "AKIAPMJ4QRXP6BECFUVQ";
		String secretKey = "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";
		String region = "cn-north-1";*/
		//test
		String accessKey = "AKIAOS2UEXIFNVER2O6A";
		String secretKey = "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";
		String region = "cn-north-1";

		String bucketName = "reyundownload-test";
		String objectKey = "trackingio/install/0543f163988c80f9142a9936ecc523d2_今日头条_2019-03-29_2019-03-29.tar.gz";

		try {
			AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

			ClientConfiguration config = new ClientConfiguration();
			String proxyHost = System.getProperty("http.proxyHost");
			String proxyPort = System.getProperty("http.proxyPort");
			if (proxyHost != null && proxyPort != null) {
				config.setProxyHost(proxyHost);
				config.setProxyPort(Integer.valueOf(proxyPort));
			}
			AmazonS3 s3 = new AmazonS3Client(credentials, config);

//            s3.setRegion(com.amazonaws.regions.Region.getRegion(Regions.CN_NORTH_1));
			s3.setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(region)));

			// Set the presigned URL to expire after one hour.
			java.util.Date expiration = new java.util.Date();
			long expTimeMillis = expiration.getTime();
			expTimeMillis += 1000 * 60 * 60;
			expiration.setTime(expTimeMillis);

			// Generate the presigned URL.
			System.out.println("Generating pre-signed URL.");
			GeneratePresignedUrlRequest generatePresignedUrlRequest =
					new GeneratePresignedUrlRequest(bucketName, objectKey)
							.withMethod(HttpMethod.GET)
							.withExpiration(expiration);
			URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);
			boolean b = s3.doesObjectExist(bucketName, objectKey);

			System.out.println("Pre-Signed URL: " + url.toString()+",文件是否存在:"+b);
		}
		catch(AmazonServiceException e) {
			// The call was transmitted successfully, but Amazon S3 couldn't process
			// it, so it returned an error response.
			e.printStackTrace();
		}
		catch(SdkClientException e) {
			// Amazon S3 couldn't be contacted for a response, or the client
			// couldn't parse the response from Amazon S3.
			e.printStackTrace();
		}
	}
}