001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.mapred; 020 021import java.io.DataInput; 022import java.io.DataOutput; 023import java.io.IOException; 024import java.util.Arrays; 025 026import org.apache.yetus.audience.InterfaceAudience; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.apache.hadoop.mapred.InputSplit; 031 032/** 033 * A table split corresponds to a key range [low, high) 034 */ 035@InterfaceAudience.Public 036public class TableSplit implements InputSplit, Comparable<TableSplit> { 037 private TableName m_tableName; 038 private byte [] m_startRow; 039 private byte [] m_endRow; 040 private String m_regionLocation; 041 042 /** default constructor */ 043 public TableSplit() { 044 this((TableName)null, HConstants.EMPTY_BYTE_ARRAY, 045 HConstants.EMPTY_BYTE_ARRAY, ""); 046 } 047 048 /** 049 * Constructor 050 * @param tableName 051 * @param startRow 052 * @param endRow 053 * @param location 054 */ 055 public TableSplit(TableName tableName, byte [] startRow, byte [] endRow, 056 final String location) { 057 this.m_tableName = tableName; 058 this.m_startRow = startRow; 059 this.m_endRow = endRow; 060 this.m_regionLocation = location; 061 } 062 063 public TableSplit(byte [] tableName, byte [] startRow, byte [] endRow, 064 final String location) { 065 this(TableName.valueOf(tableName), startRow, endRow, 066 location); 067 } 068 069 /** @return table name */ 070 public TableName getTable() { 071 return this.m_tableName; 072 } 073 074 /** @return table name */ 075 public byte [] getTableName() { 076 return this.m_tableName.getName(); 077 } 078 079 /** @return starting row key */ 080 public byte [] getStartRow() { 081 return this.m_startRow; 082 } 083 084 /** @return end row key */ 085 public byte [] getEndRow() { 086 return this.m_endRow; 087 } 088 089 /** @return the region's hostname */ 090 public String getRegionLocation() { 091 return this.m_regionLocation; 092 } 093 094 public String[] getLocations() { 095 return new String[] {this.m_regionLocation}; 096 } 097 098 public long getLength() { 099 // Not clear how to obtain this... seems to be used only for sorting splits 100 return 0; 101 } 102 103 public void readFields(DataInput in) throws IOException { 104 this.m_tableName = TableName.valueOf(Bytes.readByteArray(in)); 105 this.m_startRow = Bytes.readByteArray(in); 106 this.m_endRow = Bytes.readByteArray(in); 107 this.m_regionLocation = Bytes.toString(Bytes.readByteArray(in)); 108 } 109 110 public void write(DataOutput out) throws IOException { 111 Bytes.writeByteArray(out, this.m_tableName.getName()); 112 Bytes.writeByteArray(out, this.m_startRow); 113 Bytes.writeByteArray(out, this.m_endRow); 114 Bytes.writeByteArray(out, Bytes.toBytes(this.m_regionLocation)); 115 } 116 117 @Override 118 public String toString() { 119 StringBuilder sb = new StringBuilder(); 120 sb.append("HBase table split("); 121 sb.append("table name: ").append(m_tableName); 122 sb.append(", start row: ").append(Bytes.toStringBinary(m_startRow)); 123 sb.append(", end row: ").append(Bytes.toStringBinary(m_endRow)); 124 sb.append(", region location: ").append(m_regionLocation); 125 sb.append(")"); 126 return sb.toString(); 127 } 128 129 @Override 130 public int compareTo(TableSplit o) { 131 return Bytes.compareTo(getStartRow(), o.getStartRow()); 132 } 133 134 @Override 135 public boolean equals(Object o) { 136 if (o == null || !(o instanceof TableSplit)) { 137 return false; 138 } 139 TableSplit other = (TableSplit)o; 140 return m_tableName.equals(other.m_tableName) && 141 Bytes.equals(m_startRow, other.m_startRow) && 142 Bytes.equals(m_endRow, other.m_endRow) && 143 m_regionLocation.equals(other.m_regionLocation); 144 } 145 146 @Override 147 public int hashCode() { 148 int result = m_tableName != null ? m_tableName.hashCode() : 0; 149 result = 31 * result + Arrays.hashCode(m_startRow); 150 result = 31 * result + Arrays.hashCode(m_endRow); 151 result = 31 * result + (m_regionLocation != null ? m_regionLocation.hashCode() : 0); 152 return result; 153 } 154}