Advertisement
Guest User

Test program for long flushes

a guest
Aug 10th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.25 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.apache.commons.lang.RandomStringUtils;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.hbase.HBaseConfiguration;
  8. import org.apache.hadoop.hbase.HColumnDescriptor;
  9. import org.apache.hadoop.hbase.HTableDescriptor;
  10. import org.apache.hadoop.hbase.client.Get;
  11. import org.apache.hadoop.hbase.client.HBaseAdmin;
  12. import org.apache.hadoop.hbase.client.HTable;
  13. import org.apache.hadoop.hbase.client.Put;
  14. import org.apache.hadoop.hbase.client.Result;
  15. import org.apache.hadoop.hbase.util.Bytes;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18.  
  19. public class TestTable implements Runnable {
  20.     HTable table = null;
  21.     Configuration conf = null;
  22.     String rowKey = null;
  23.     int iterations = 0;
  24.     private static final String TEST_TABLE_NAME = "MyTestTable";
  25.     private static final String LARGE_ROW = "ThisIsTheRowYoureLookingFor";
  26.     private static int totalUpdates = 0;
  27.    
  28.     private static final Logger LOG = LoggerFactory.getLogger(TestTable.class);
  29.    
  30.     /**
  31.      * Construct a new TestTable
  32.      * @param c
  33.      * @param rowKey
  34.      * @param iterations
  35.      * @throws IOException
  36.      */
  37.     public TestTable(Configuration c, String rowKey, int iterations) throws IOException {
  38.         conf = c;
  39.         table = new HTable(conf, TEST_TABLE_NAME);
  40.         this.rowKey = rowKey;
  41.         this.iterations = iterations;
  42.     }
  43.    
  44.     public static void main(String[] args) throws Exception {
  45.         String row = LARGE_ROW;
  46.         int iterations = 1000;
  47.         int numThreads = 4;
  48.        
  49.         if (args.length == 1 || args.length > 2) {
  50.             System.out.println("Usage: TestTable [<IterationsPerThread> <NumThreads>]");
  51.             System.exit(-1);
  52.         }
  53.        
  54.         LOG.info("Row               : " + row);
  55.         LOG.info("Threads           : " + numThreads);
  56.         LOG.info("IteratorsPerThread: " + iterations);
  57.        
  58.         Configuration conf = HBaseConfiguration.create();
  59.         HBaseAdmin hbase = new HBaseAdmin(conf);
  60.        
  61.         // Create test table if it doesn't exist
  62.         if (!hbase.tableExists(TEST_TABLE_NAME.getBytes())) {
  63.             HTableDescriptor desc = new HTableDescriptor(TEST_TABLE_NAME);
  64.             HColumnDescriptor data = new HColumnDescriptor("data".getBytes());
  65.             HColumnDescriptor stats = new HColumnDescriptor("stats".getBytes());
  66.             desc.addFamily(data);
  67.             desc.addFamily(stats);
  68.             hbase.createTable(desc);
  69.            
  70.             // init the table with random rows
  71.             row = initDatabase(conf);
  72.            
  73.             // This might not be necessary, but just in case hbase is doing some work from all the updates
  74.             // we just sent, we'll give it some time.
  75.             LOG.info("Starting test in 10s.");
  76.             Thread.sleep(10000);
  77.         }
  78.  
  79.         List<Thread> threads = new ArrayList<Thread>();
  80.         for (int i = 0; i < numThreads; i++) {
  81.             Thread t = new Thread(new TestTable(conf, row, iterations));
  82.             t.start();
  83.             threads.add(t);
  84.         }
  85.        
  86.         for (Thread t : threads) {
  87.             t.join();
  88.         }
  89.     }
  90.  
  91.     private static String initDatabase(Configuration conf) throws IOException {
  92.         HTable table = new HTable(conf, TEST_TABLE_NAME);
  93.         LOG.info("Initializing the database. Adding random rows");
  94.  
  95.         // Add some random rows
  96.         List<Put> puts = new ArrayList<Put>();
  97.         for (int i = 1; i < 100000; i++) {
  98.             String key = RandomStringUtils.randomAlphanumeric(14);
  99.             Put put = new Put(Bytes.toBytes(key));
  100.            
  101.             put.add(Bytes.toBytes("data"), Bytes.toBytes(RandomStringUtils.randomAlphanumeric(18)),
  102.                 Bytes.toBytes(RandomStringUtils.randomAlphanumeric(50)));
  103.             put.add(Bytes.toBytes("stats"), Bytes.toBytes("hello"),
  104.                 Bytes.toBytes(RandomStringUtils.randomAlphanumeric(8)));
  105.            
  106.             puts.add(put);
  107.             if (puts.size() > 0 && puts.size() % 1000 == 0) {
  108.                 table.put(puts);
  109.                 LOG.info("Added {} rows.", i);
  110.             }
  111.         }
  112.        
  113.         if (puts.size() > 0) {
  114.             table.put(puts);
  115.         }
  116.        
  117.         // Now give this one row tons of columns
  118.         //String key = RandomStringUtils.randomAlphanumeric(14);
  119.         String key = LARGE_ROW;
  120.         Put put = new Put(Bytes.toBytes(key));
  121.         LOG.info("Row with tons of columns {}", key);
  122.        
  123.         put.add(Bytes.toBytes("stats"), Bytes.toBytes("hello"),
  124.             Bytes.toBytes(RandomStringUtils.randomAlphanumeric(8)));
  125.        
  126.         for (int i = 0; i < 60000; i++) {
  127.             put.add(Bytes.toBytes("data"), Bytes.toBytes(RandomStringUtils.randomAlphanumeric(18)),
  128.                 Bytes.toBytes(RandomStringUtils.randomAlphanumeric(50)));
  129.         }
  130.        
  131.         table.put(put);
  132.         return key;
  133.     }
  134.    
  135.     @Override
  136.     public void run() {
  137.         for (int i = 1; i <= iterations; i++) {
  138.             try {
  139.                 Get row = new Get(Bytes.toBytes(rowKey));
  140.                 Put put = new Put(Bytes.toBytes(rowKey));
  141.                 //Put put = new Put(Bytes.toBytes(RandomStringUtils.randomAlphanumeric(10)));
  142.                 Result r = table.get(row);
  143.        
  144.                 Map<byte[], byte[]> data = r.getFamilyMap(Bytes.toBytes("data"));
  145.                 for (Map.Entry<byte[], byte[]> entry : data.entrySet()) {
  146.                     put.add(Bytes.toBytes("data"), entry.getKey(), entry.getValue());
  147.                 }
  148.                
  149.                 Map<byte[], byte[]> stats = r.getFamilyMap(Bytes.toBytes("stats"));
  150.                 for (Map.Entry<byte[], byte[]> entry : stats.entrySet()) {
  151.                     put.add(Bytes.toBytes("stats"), entry.getKey(), entry.getValue());
  152.                 }
  153.                
  154.                 table.put(put);
  155.                 incrementCounter();
  156.             } catch (IOException e) {
  157.                 LOG.error("ERROR: ", e);
  158.             }
  159.         }
  160.     }
  161.    
  162.     synchronized private void incrementCounter() {
  163.         totalUpdates++;
  164.         LOG.info("Sent {} updates", totalUpdates);
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement