1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.util;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Random;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.Cell;
26 import org.apache.hadoop.hbase.CellScanner;
27 import org.apache.hadoop.hbase.KeyValue;
28 import org.apache.hadoop.hbase.KeyValue.Type;
29 import org.apache.hadoop.hbase.Tag;
30 import org.apache.hadoop.hbase.client.Mutation;
31 import org.apache.hadoop.hbase.client.Put;
32 import org.apache.hadoop.hbase.util.MultiThreadedAction.DefaultDataGenerator;
33
34 @InterfaceAudience.Private
35 public class LoadTestDataGeneratorWithTags extends DefaultDataGenerator {
36
37 private int minNumTags, maxNumTags;
38 private int minTagLength, maxTagLength;
39 private Random random = new Random();
40
41 public LoadTestDataGeneratorWithTags(int minValueSize, int maxValueSize, int minColumnsPerKey,
42 int maxColumnsPerKey, byte[]... columnFamilies) {
43 super(minValueSize, maxValueSize, minColumnsPerKey, maxColumnsPerKey, columnFamilies);
44 }
45
46 @Override
47 public void initialize(String[] args) {
48 super.initialize(args);
49 if (args.length != 4) {
50 throw new IllegalArgumentException("LoadTestDataGeneratorWithTags must have "
51 + "4 initialization arguments. ie. minNumTags:maxNumTags:minTagLength:maxTagLength");
52 }
53
54 this.minNumTags = Integer.parseInt(args[0]);
55
56 this.maxNumTags = Integer.parseInt(args[1]);
57
58 this.minTagLength = Integer.parseInt(args[2]);
59
60 this.maxTagLength = Integer.parseInt(args[3]);
61 }
62
63 @Override
64 public Mutation beforeMutate(long rowkeyBase, Mutation m) throws IOException {
65 if (m instanceof Put) {
66 List<Cell> updatedCells = new ArrayList<Cell>();
67 int numTags;
68 if (minNumTags == maxNumTags) {
69 numTags = minNumTags;
70 } else {
71 numTags = minNumTags + random.nextInt(maxNumTags - minNumTags);
72 }
73 List<Tag> tags;
74 for (CellScanner cellScanner = m.cellScanner(); cellScanner.advance();) {
75 Cell cell = cellScanner.current();
76 byte[] tag = LoadTestTool.generateData(random,
77 minTagLength + random.nextInt(maxTagLength - minTagLength));
78 tags = new ArrayList<Tag>();
79 for (int n = 0; n < numTags; n++) {
80 tags.add(new Tag((byte) 127, tag));
81 }
82 Cell updatedCell = new KeyValue(cell.getRowArray(), cell.getRowOffset(),
83 cell.getRowLength(), cell.getFamilyArray(), cell.getFamilyOffset(),
84 cell.getFamilyLength(), cell.getQualifierArray(), cell.getQualifierOffset(),
85 cell.getQualifierLength(), cell.getTimestamp(), Type.codeToType(cell.getTypeByte()),
86 cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), tags);
87 updatedCells.add(updatedCell);
88 }
89 m.getFamilyCellMap().clear();
90
91 for (Cell cell : updatedCells) {
92 ((Put) m).add(cell);
93 }
94 }
95 return m;
96 }
97 }