In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Through the previous section, "memcached walkthrough (1) Building memcached Services", we have successfully installed the memcached service on the linux virtual machine and set up self-startup.
STAT version 1.4.29Mem: 1891
Main content
Use the telnet command line tool to operate the memcached service
Use java spymemcached tools to access memcached services
Monitor the data inserted with different amounts of data, response time
Monitor the comparison between busy and idle memcached hosts
Adjust the memory metrics of memcached and insert the same amount of data. When is the best performance?
1. Use the telnet command line tool to operate the memcached service
1.1 verify the set&add add command
[hadoop@hadoop1 ~] $ps-ef | grep memcachednobody 2047 11 07:27? 00:08:59 / usr/local/memcached/bin/memcached-d-p 11211-u nobody-m 64-c 1024-P / var/run/memcached/memcached.pidhadoop 9037 16:59 pts/0 00:00:00 grep memcached# connection [hadoop@hadoop1] $telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0. 0.1.Escape character is'^]'. # get the value of key1 get key1VALUE key1 0 4abcdEND# call the set command Successfully set key1 0 0 41234STOREDget key1VALUE key1 0 41234END# for an existing key assignment, call set command for an existing key assignment failed add key1 0 0 4abcdNOT_STOREDget key1VALUE key1 0 41234END
Conclusion: although both the add and set commands can add data, there are differences in their use. Set can assign values to the existing key, but the add command cannot.
1.2 verify the get&gets get command
# get 1 key value get key1VALUE key1 0 41234END# get 1 key value get key2VALUE key2 0 4wordEND# and get 2 key values get key1 key2VALUE key1 0 41234VALUE key2 0 4wordEND-gets key1VALUE key1 0 4 76950091234ENDgets key2VALUE key2 0 4 7695010wordENDgets key1 key2VALUE key1 0 4 76950091234VALUE key2 0 4 7695010wordEND#gets command returns the ratio get command One more "unknown number" # modify the value of key1 set key1 00 5worldSTORED# confirm change get key1VALUE key1 0 5worldEND# unknown number from 7695009 to 7695011gets key1VALUE key1 0 5 7695011worldEND# key2 value does not change, so unknown number does not change gets key2VALUE key2 0 4 7695010wordEND# again modify key1 value set key1 00 3oldSTORED# added 1gets key1VALUE key1 0 3 7695012oldEND
Conclusion the gets command returns more numbers than the get command, similar to "changing the version number" in the database, and this version number is shared among the key values.
1.3Test the incr & decr command
# A key does not exist incr seq 1NOT_FOUNDset seq 00 11STOREDget seq 1VALUE seq 0 11ENDincr seq 12...get seqVALUE seq 0 14ENDgets seqVALUE seq 01 76950164ENDincr seq calls the incr command, adding the change version number gets seqVALUE seq 01 76950175ENDincr seq 1015decr seq 11 does not allow negative decr seq-1CLIENT_ERROR invalid numeric delta argumentdecr seq 10 minimum 0decr seq 10 has a maximum limit decr seq 10000000000000000000000CLIENT_ERROR invalid numeric delta argumentset seq2 00 1aSTORED# if the key value is non-numeric Will report an error incr seq2 1CLIENT_ERROR cannot increment or decrement non-numeric value
Through the experimental verification: the incr&decr command affects the length of the key; the minimum value is 0; the increment can be changed; must act on the number; the command parameter is not allowed to be negative.
1.4 Update the command append&preppend&replace
Set updkey 0 0 5worldSTOREDget updkeyVALUE updkey 0 5worldEND# adds characters append updkey 0 0 5helloSTOREDget updkeyVALUE updkey 0 10worldhello# after adding characters prepend updkey 0 0 101234567890STOREDgets updkeyVALUE updkey 0 20 76950301234567890worldhelloEND# changes the valid time to 1prepend updkey 01 for a period of time (indicating that the update operation will not affect the valid time) get updkeyVALUE updkey 0 221u1234567890worldhelloEND# does not add anything prepend updkey 0 0 0STORED# although there is no change value But the change of the version number also changed the gets updkeyVALUE updkey 0 22 76950371u1234567890worldhello#replace command replace updkey 0 10 1mSTOREDget updkeyVALUE updkey 0 1mEND#replace command changed the effective time gets updkeyEND
Conclusion: the prepend command works similar to the append command, one adding characters from the front and the other appending characters after it. Each behavior affects the version number; it does not affect the original valid time; the replace command can not only modify the data, but also change the valid time.
If there are some other instructions, I won't play.
two。 Use java spymemcached tools to access memcached services
2.1 introducing dependencies
Net.spy spymemcached 2.11.5
2.2 client utility class
Package com.nap.memcachedexample.service;import net.spy.memcached.MemcachedClient;import java.io.IOException;import java.net.InetSocketAddress;/** * Created by Administrator on 2016-8-7. * / public class MemcachedUtil {private static MemcachedClient cachedClient = null; static {try {cachedClient = new MemcachedClient (new InetSocketAddress ("192.168.163.146", 11211));} catch (IOException e) {e.printStackTrace () }} public static MemcachedClient getSpyMemcachedClient () {return cachedClient;}}
2.2 Test object
Employee.java package com.nap;import org.apache.commons.lang.builder.ToStringBuilder;import java.io.Serializable;import java.util.*;public class Employee implements Serializable {private long empNo; private String name; private Date birthDate; private int age; private double salary; private List favoriteFoods; public String getName () {return name;} public void setName (String name) {this.name = name } public Date getBirthDate () {return birthDate;} public long getEmpNo () {return empNo;} public void setEmpNo (long empNo) {this.empNo = empNo;} public void setBirthDate (Date birthDate) {this.birthDate = birthDate;} public int getAge () {return age;} public void setAge (int age) {this.age = age } public double getSalary () {return salary;} public void setSalary (double salary) {this.salary = salary;} public List getFavoriteFoods () {return favoriteFoods;} public void setFavoriteFoods (List favoriteFoods) {this.favoriteFoods = favoriteFoods;} public String toString () {return ToStringBuilder.reflectionToString (this) }}-EmployeeFactory.java package com.nap;import com.nap.memcachedexample.service.MemcachedUtil;import net.spy.memcached.MemcachedClient;import net.spy.memcached.internal.OperationFuture Import org.apache.commons.lang.RandomStringUtils;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import java.util.Random;import java.util.concurrent.ExecutionException;public class EmployeeFactory {static MemcachedClient mcc = MemcachedUtil.getSpyMemcachedClient (); public static final String EMPNOSEQ = "empnoseq"; static {Object cacheSeq= mcc.get ("empnoseq"); if (cacheSeq==null) {OperationFuture seqFuture = mcc.set (EMPNOSEQ, 900,0 ") Try {if (seqFuture.get ()) {System.out.println ("set status: ok");} catch (InterruptedException e) {e.printStackTrace ();} catch (ExecutionException e) {e.printStackTrace () } / * construct a test object (an object is about 40B) * @ return * / public static Employee newEmployee () {long empNo = MemcachedUtil.getSpyMemcachedClient () .incr ("empnoseq", 1); Calendar cal = Calendar.getInstance (); Employee emp = newEmployee (); emp.setEmpNo (empNo) Emp.setAge (new Random (). NextInt (80)); emp.setName (RandomStringUtils.randomAlphabetic (15)); cal.set (Calendar.MONTH, new Random (). NextInt (30)); cal.set (Calendar.DAY_OF_YEAR, new Random (). NextInt (366)); emp.setBirthDate (cal.getTime ()); emp.setSalary (new Random (). NextFloat ()); List foods = new ArrayList () For (int juni0witj
< new Random().nextInt(10);j++){ foods.add("foods_"+RandomStringUtils.randomAlphabetic(10)); } emp.setFavoriteFoods(foods); return emp; }} package com.nap;import com.nap.memcachedexample.service.MemcachedUtil;import junit.framework.TestCase;import net.spy.memcached.MemcachedClient;import java.util.concurrent.ExecutionException;public class SpyMemcachedClientTest extends TestCase{ //测试set public void testAddBean() throws ExecutionException, InterruptedException { MemcachedClient mcc = MemcachedUtil.getSpyMemcachedClient(); Employee emp = EmployeeFactory.newEmployee(); System.out.println(emp); String key = "emp_" + emp.getEmpNo(); System.out.println(key); mcc.set(key, 19000, emp); // Shutdowns the memcached client mcc.shutdown(); } //测试get public void testGetBean() throws ExecutionException, InterruptedException { MemcachedClient mcc = MemcachedUtil.getSpyMemcachedClient(); Object o = mcc.get("emp_1"); System.out.println(o); // Shutdowns the memcached client mcc.shutdown(); }} telnet输出结果 get emp_1VALUE emp_1 1 345srcom.nap.EmployeefavoriteFoodstLjava/util/List;LnametLjava/lang/String;xpetLjava/util/Date;L ?佚U 通过结果分析,通过客户端已经成功保存了。 3.监控插入不同数据量的数据,响应时间 存储单位转换器 package com.nap; public enum SizeConverter { /** 转换任意单位的大小, 返回结果会包含两位小数但不包含单位. */ Arbitrary { @Override public String convert(float size) { while (size >1024) {size / = 1024;} return String.format (FORMAT_F, size) }}, / /-- / / has a unit / * * convert the size of the unit to B The returned result will contain two decimal places and units. For example, 1024B-> 1KB, (1024024) B-> 1MB * / B {@ Override public String convert (float B) {return converter (0, B);}}, / * * the conversion unit is the size of B, and the returned result will contain two decimal places and units. * / KB {@ Override public String convert (float KB) {return converter (1, KB);}}, / * * the conversion unit is the size of MB, and the returned result will contain two decimal places and units. * / MB {@ Override public String convert (float MB) {return converter (2, MB);}}, / * * the conversion unit is the size of GB, and the returned result will contain two decimal places and units. * / GB {@ Override public String convert (float GB) {return converter (3, GB);}}, / * * the conversion unit is the size of TB, and the returned result will contain two decimal places and units. * / TB {@ Override public String convert (float TB) {return converter (4, TB) }}, / /-/ / trim unitless / * * convert the size of any unit When the decimal part of the result is 0, two decimal places will be removed and no units will be included. * / ArbitraryTrim {@ Override public String convert (float size) {while (size > 1024) {size / = 1024;} int sizeInt = (int) size; boolean isfloat = size-sizeInt > 0.0F; if (isfloat) {return String.format (FORMAT_F, size) } return String.format (FORMAT_D, sizeInt) }}, / /-/ / trim has units / * * convert units to the size of B When the decimal part of the result is 0, two decimal places will be removed and units will be included. * / BTrim {@ Override public String convert (float B) {return trimConverter (0, B);}}, / * * the conversion unit is the size of KB. If the result decimal part is 0, two decimal places will be removed and the unit will be included. * / KBTrim {@ Override public String convert (float KB) {return trimConverter (1, KB);}}, / * * the conversion unit is the size of MB. If the decimal part of the result is 0, two decimal places will be removed and the unit will be included. * / MBTrim {@ Override public String convert (float MB) {return trimConverter (2, MB);}}, / * * the conversion unit is the size of GB. If the decimal part of the result is 0, two decimal places will be removed and the unit will be included. * / GBTrim {@ Override public String convert (float GB) {return trimConverter (3, GB)}}, / * * the conversion unit is the size of TB. If the decimal part of the result is 0, two decimal places will be removed and the unit will be included. * / TBTrim {@ Override public String convert (float TB) {return trimConverter (4, TB);}}; / *
Converts the specified size to a size within the 1024 range. Note that the maximum unit of this method is PB, the smallest unit is B, and * any unit outside this range will eventually be displayed as *.
* * @ param size to convert the size, pay attention to floating-point numbers, do not pass in the way of × ×, it is easy to cause overflow. * (for example, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, / /-/ Unit conversion private static final String [] UNITS = new String [] {"B", "KB", "MB" "GB", "TB", "PB", "*"} Private static final int LAST_IDX = UNITS.length-1; private static final String FORMAT_F = "1 $- 1.2f"; private static final String FORMAT_F_UNIT = "1 $- 1.2f%2$ s"; private static final String FORMAT_D = "1 $- 1D"; private static final String FORMAT_D_UNIT = "1 $- 1d%2$ s" / /-private static String converter (int unit, float size) {int unitIdx = unit; while (size > 1024) {unitIdx++ Size / = 1024;} int idx = unitIdx
< LAST_IDX ? unitIdx : LAST_IDX; return String.format(FORMAT_F_UNIT, size, UNITS[idx]); } private static String trimConverter(int unit, float size) { int unitIdx = unit; while (size >1024) {unitIdx++; size / = 1024;} int sizeInt = (int) size; boolean isfloat = size-sizeInt > 0.0F; int idx = unitIdx
< LAST_IDX ? unitIdx : LAST_IDX; if (isfloat) { return String.format(FORMAT_F_UNIT, size, UNITS[idx]); } return String.format(FORMAT_D_UNIT, sizeInt, UNITS[idx]); } // ----------------------------------------------------------------------- public static String convertBytes(float B, boolean trim) { return trim ? trimConvert(0, B, true) : convert(0, B, true); } public static String convertKB(float KB, boolean trim) { return trim ? trimConvert(1, KB, true) : convert(1, KB, true); } public static String convertMB(float MB, boolean trim) { return trim ? trimConvert(2, MB, true) : convert(2, MB, true); } /*** * 存储大小单位间的转换. 注意该方法的最大单位为PB, 最小单位为B, * 任何超出该范围的单位最终会显示为**. * * @param unit 从哪个单位开始 * @param size 存储大小, 注意是float, 不要以×××的形式传入, 否则会溢出(如:1024*1024这种, * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了, * 所以这么写1024.0F*1024.0F) * @param withUnit 返回的结果字符串是否带有对应的单位 * @return */ private static String convert(int unit, float size, boolean withUnit) { int unitIdx = unit; while (size >1024) {unitIdx++; size / = 1024;} if (withUnit) {int idx = unitIdx
< LAST_IDX ? unitIdx : LAST_IDX; return String.format(FORMAT_F_UNIT, size, UNITS[idx]); } return String.format(FORMAT_F, size); } /*** * 存储大小单位间的转换, 如果转换后小数部分为0, 则去除小数部分. * 注意该方法的最大单位为PB, 最小单位为B, 任何超出该范围的单位最终会显示为**. * * @param unit 从哪个单位开始 * @param size 存储大小, 注意是float, 不要以×××的形式传入, 否则会溢出(如:1024*1024这种, * 它是先将1024*1024作为int相乘再转换为float的, 如果值过大的话就会溢出了, * 所以这么写1024.0F*1024.0F) * @param withUnit 返回的结果字符串是否带有对应的单位 * @return */ private static String trimConvert(int unit, float size, boolean withUnit) { int unitIdx = unit; while (size >1024) {unitIdx++; size / = 1024;} int sizeInt = (int) size; boolean isfloat = size-sizeInt > 0.0F; if (withUnit) {int idx = unitIdx < LAST_IDX? UnitIdx: LAST_IDX; if (isfloat) {return String.format (FORMAT_F_UNIT, size, UNITS [idx]);} return String.format (FORMAT_D_UNIT, sizeInt, UNITS [idx]);} if (isfloat) {return String.format (FORMAT_F, size) } return String.format (FORMAT_D, sizeInt);} public static void main (String [] args) {System.out.println (SizeConverter.BTrim.convert (1029000f));}}
Public class SpyMemcachedClientTest extends TestCase {/ / logic is very simple, only bulk insert quantity data, after estimating an Employee object, it is generally 40B. Public void testBatchAddBean () throws ExecutionException, InterruptedException {MemcachedClient mcc = MemcachedUtil.getSpyMemcachedClient (); long startTime=System.currentTimeMillis (); int size=10000000; for (int item0)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.