diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java index 20cfbef..183e2a0 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/HColumnDescriptor.java @@ -706,6 +706,15 @@ public class HColumnDescriptor implements Comparable { } /** + * @param timeToLive Time to live of cell contents, in human readable format + * @see org.apache.hadoop.hbase.util.PrettyPrinter#format(String, Unit) + * @return this (for chained invocation) + */ + public HColumnDescriptor setTimeToLive(String timeToLive) { + return setValue(TTL, PrettyPrinter.valueOf(timeToLive, Unit.TIME_INTERVAL)); + } + + /** * @return The minimum number of versions to keep. */ public int getMinVersions() { diff --git hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java index 7728112..368474b 100644 --- hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java +++ hbase-common/src/main/java/org/apache/hadoop/hbase/util/PrettyPrinter.java @@ -19,6 +19,9 @@ package org.apache.hadoop.hbase.util; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.classification.InterfaceAudience; @@ -42,6 +45,25 @@ public class PrettyPrinter { return human.toString(); } + /** + * Convert a human readable string to its value. + * @see org.apache.hadoop.hbase.util.PrettyPrinter#format(String, Unit) + * @param pretty + * @param unit + * @return the value corresponding to the human readable string + */ + public static String valueOf(final String pretty, final Unit unit) { + StringBuilder value = new StringBuilder(); + switch (unit) { + case TIME_INTERVAL: + value.append(humanReadableIntervalToSec(pretty)); + break; + default: + value.append(pretty); + } + return value.toString(); + } + @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ICAST_INTEGER_MULTIPLY_CAST_TO_LONG", justification="Will not overflow") private static String humanReadableTTL(final long interval){ @@ -97,4 +119,46 @@ public class PrettyPrinter { return sb.toString(); } + /** + * Convert a human readable time interval to seconds. Examples of the human readable + * time intervals are: 50 DAYS 1 HOUR 30 MINUTES , 25000 SECONDS etc. + * The units of time specified can be in uppercase as well as lowercase. Also, if a + * single number is specified without any time unit, it is assumed to be in seconds. + * @param humanReadableInterval + * @return value in seconds + */ + private static long humanReadableIntervalToSec(final String humanReadableInterval) { + if(humanReadableInterval == null || humanReadableInterval.equalsIgnoreCase("FOREVER")) { + return HConstants.FOREVER; + } + + try { + return Long.parseLong(humanReadableInterval); + } catch(NumberFormatException ex) { + // The passed value of time period is in human readable format, + } + + String days, hours, minutes, seconds; + long ttl; + days = hours = minutes = seconds = null; + + String regex = "((\\d+)\\s*DAYS?)?\\s*((\\d+)\\s*HOURS?)?\\s*((\\d+)\\s*MINUTES?)?" + + "\\s*((\\d+)\\s*SECONDS?)?"; + Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(humanReadableInterval); + if(matcher.matches()) { + days = matcher.group(2); + hours = matcher.group(4); + minutes = matcher.group(6); + seconds = matcher.group(8); + } + ttl = 0; + ttl += days != null ? Long.parseLong(days)*HConstants.DAY_IN_SECONDS:0; + ttl += hours != null ? Long.parseLong(hours)*HConstants.HOUR_IN_SECONDS:0; + ttl += minutes != null ? Long.parseLong(minutes)*HConstants.MINUTE_IN_SECONDS:0; + ttl += seconds != null ? Long.parseLong(seconds):0; + + return ttl; + } + } diff --git hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestPrettyPrinter.java hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestPrettyPrinter.java new file mode 100644 index 0000000..72acbfb --- /dev/null +++ hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestPrettyPrinter.java @@ -0,0 +1,51 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.Assert; +import org.junit.experimental.categories.Category; +import org.junit.Test; + +@Category({MiscTests.class, SmallTests.class}) +public class TestPrettyPrinter { + @Test + public void testValueOf() { + String pretty; + PrettyPrinter.Unit unit = PrettyPrinter.Unit.TIME_INTERVAL; + + pretty = "50000"; + Assert.assertEquals("50000", PrettyPrinter.valueOf(pretty, unit)); + + pretty = "50000 seconds"; + Assert.assertEquals("50000", PrettyPrinter.valueOf(pretty, unit)); + + pretty = ""; + Assert.assertEquals("0", PrettyPrinter.valueOf(pretty, unit)); + + pretty = "FOREVER"; + Assert.assertEquals(HConstants.FOREVER + "", PrettyPrinter.valueOf(pretty, unit)); + + pretty = "25 DAYS 4 HOURS"; + Assert.assertEquals("2174400", PrettyPrinter.valueOf(pretty, unit)); + } +} diff --git hbase-shell/src/main/ruby/hbase/admin.rb hbase-shell/src/main/ruby/hbase/admin.rb index 9cdfe66..76f2004 100644 --- hbase-shell/src/main/ruby/hbase/admin.rb +++ hbase-shell/src/main/ruby/hbase/admin.rb @@ -750,7 +750,7 @@ module Hbase family.setScope(JInteger.valueOf(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::REPLICATION_SCOPE))) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::REPLICATION_SCOPE) family.setCacheDataOnWrite(JBoolean.valueOf(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::CACHE_DATA_ON_WRITE))) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::CACHE_DATA_ON_WRITE) family.setInMemory(JBoolean.valueOf(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::IN_MEMORY))) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::IN_MEMORY) - family.setTimeToLive(JInteger.valueOf(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::TTL))) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::TTL) + family.setTimeToLive(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::TTL)) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::TTL) family.setDataBlockEncoding(org.apache.hadoop.hbase.io.encoding.DataBlockEncoding.valueOf(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::DATA_BLOCK_ENCODING))) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::DATA_BLOCK_ENCODING) family.setBlocksize(JInteger.valueOf(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::BLOCKSIZE))) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::BLOCKSIZE) family.setMaxVersions(JInteger.valueOf(arg.delete(org.apache.hadoop.hbase.HColumnDescriptor::VERSIONS))) if arg.include?(org.apache.hadoop.hbase.HColumnDescriptor::VERSIONS)