-
Notifications
You must be signed in to change notification settings - Fork 48
/
HBaseFullTap.java
187 lines (151 loc) · 5.1 KB
/
HBaseFullTap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2009 Concurrent, Inc.
*
* This work has been released into the public domain
* by the copyright holder. This applies worldwide.
*
* In case this is not legally possible:
* The copyright holder grants any entity the right
* to use this work for any purpose, without any
* conditions, unless such conditions are required by law.
*/
package cascading.hbase;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import cascading.tap.Tap;
import cascading.tap.TapException;
import cascading.tap.SinkMode;
import cascading.tap.hadoop.TapIterator;
import cascading.tap.hadoop.TapCollector;
import cascading.tuple.TupleEntryIterator;
import cascading.tuple.TupleEntryCollector;
import cascading.flow.Flow;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.hbase.mapred.TableOutputFormat;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The HBaseTap class is a {@link Tap} subclass. It is used in conjunction with the {@HBaseFullScheme} to allow for the
* reading and writing of data to and from a HBase cluster.
*
*/
public class HBaseFullTap extends Tap
{
/** Field LOG */
private static final Logger LOG = LoggerFactory.getLogger( HBaseFullTap.class );
/** Field SCHEME */
public static final String SCHEME = "hbase";
/** Field hBaseAdmin */
private transient HBaseAdmin hBaseAdmin;
private String tableName;
/**
* Constructor HBaseTap creates a new HBaseTap instance.
*
* @param tableName of type String
* @param HBaseFullScheme of type HBaseFullScheme
*/
public HBaseFullTap( String tableName, HBaseFullScheme HBaseFullScheme )
{
super( HBaseFullScheme, SinkMode.APPEND );
this.tableName = tableName;
}
/**
* Constructor HBaseTap creates a new HBaseTap instance.
*
* @param tableName of type String
* @param HBaseFullScheme of type HBaseFullScheme
* @param sinkMode of type SinkMode
*/
public HBaseFullTap( String tableName, HBaseFullScheme HBaseFullScheme, SinkMode sinkMode )
{
super( HBaseFullScheme, sinkMode );
this.tableName = tableName;
}
private URI getURI()
{
try
{
return new URI( SCHEME, tableName, null );
}
catch( URISyntaxException exception )
{
throw new TapException( "unable to create uri", exception );
}
}
public Path getPath()
{
return new Path( getURI().toString() );
}
public TupleEntryIterator openForRead( JobConf conf ) throws IOException
{
return new TupleEntryIterator( getSourceFields(), new TapIterator( this, conf ) );
}
public TupleEntryCollector openForWrite( JobConf conf ) throws IOException
{
return new TapCollector( this, conf );
}
private HBaseAdmin getHBaseAdmin() throws MasterNotRunningException
{
if( hBaseAdmin == null )
hBaseAdmin = new HBaseAdmin( new HBaseConfiguration() );
return hBaseAdmin;
}
public boolean makeDirs( JobConf conf ) throws IOException
{
HBaseAdmin hBaseAdmin = getHBaseAdmin();
if( hBaseAdmin.tableExists( tableName ) )
return true;
LOG.debug( "creating hbase table: {}", tableName );
HTableDescriptor tableDescriptor = new HTableDescriptor( tableName );
String[] familyNames = ( (HBaseFullScheme) getScheme() ).getFamilyNames();
for( String familyName : familyNames )
tableDescriptor.addFamily( new HColumnDescriptor( familyName + ":" ) );
hBaseAdmin.createTable( tableDescriptor );
return true;
}
public boolean deletePath( JobConf conf ) throws IOException
{
// eventually keep table meta-data to source table create
HBaseAdmin hBaseAdmin = getHBaseAdmin();
if( !hBaseAdmin.tableExists( tableName ) )
return true;
LOG.debug( "deleting hbase table: {}", tableName );
hBaseAdmin.disableTable( tableName );
hBaseAdmin.deleteTable( tableName );
return true;
}
public boolean pathExists( JobConf conf ) throws IOException
{
return getHBaseAdmin().tableExists( tableName );
}
public long getPathModified( JobConf conf ) throws IOException
{
return System.currentTimeMillis(); // currently unable to find last mod time on a table
}
@Override
public void sinkInit( JobConf conf ) throws IOException
{
LOG.debug( "sinking to table: {}", tableName );
// do not delete if initialized from within a task
if( isReplace() && conf.get( "mapred.task.partition" ) == null )
deletePath( conf );
makeDirs( conf );
conf.set( TableOutputFormat.OUTPUT_TABLE, tableName );
super.sinkInit( conf );
}
@Override
public void sourceInit( JobConf conf ) throws IOException
{
LOG.debug( "sourcing from table: {}", tableName );
FileInputFormat.addInputPaths( conf, tableName );
super.sourceInit( conf );
}
}