【发布时间】:2015-06-03 06:59:25
【问题描述】:
我有一个 Hbase 表,它有一个唯一的行键和一个列族和一列。我有一个 TSV 文件,它有大约 300 多列。此文件中的行键是两列的组合值。所以现在我需要比较表和文件中的行键,如果行键匹配,那么我需要插入表列值作为 TSV 文件中相应行键的最后一列。我编写了以下代码,但它总是执行 else 部分。
package mapReduce;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
public class Tsv_read{
private static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
}
@SuppressWarnings("resource")
public static void main(String[] arg) throws Exception {
BufferedReader TSVFile =
new BufferedReader(new FileReader("Path/to/file/.tsv"));
String dataRow = TSVFile.readLine();
List<String> list = new ArrayList<String>();
while (dataRow != null){
list.clear();
String[] dataArray = dataRow.split("\t");
for (String item:dataArray) {
HTable table = new HTable(conf, "Table name"); //Hbase table name
Scan s = new Scan();
ResultScanner ss = table.getScanner(s);
for(Result r:ss){
for(KeyValue kv : r.raw()){
//System.out.println("Rowkey :" +dataArray[12]+"-"+dataArray[13]);
//System.out.print(new String(kv.getRow()) + " ");
if((dataArray[12]+"-"+dataArray[13]).equals(new String(kv.getRow()))){ //Comparing the rowkeys from file and table (doesn't work)
System.out.println("File Rowkey :"+dataArray[12]+"-"+dataArray[13]);
System.out.println("Table Row key"+new String(kv.getRow()));
//dataArray[392]=new String(kv.getValue());
FileWriter fstream = new FileWriter("/path/to/the/file/*.tsv",true);
BufferedWriter fbw = new BufferedWriter(fstream);
fbw.write(new String(kv.getValue())); //inserting the value to the tsv file
fbw.newLine();
fbw.close();
System.out.println("Column value written succesfully");
}
else //always executes this part
{
System.out.println("RowKey not found :" +new String(kv.getRow()));
}
/*System.out.print(new String(kv.getFamily()) + ":");
System.out.print(new String(kv.getQualifier()) + " ");
System.out.print(kv.getTimestamp() + " ");*/
//System.out.println(new String(kv.getValue()));
list.add(item);
}
}
}
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String txt = it.next();
System.out.print(txt+" ");
}
System.out.println(); // Print the data line.
dataRow = TSVFile.readLine();
}
TSVFile.close();
System.out.println();
} //main()
}
样本记录:
dataArray[12]+"-"+dataArray[13] = 3049620139673452544-5172983457411783096
在Hbase表中,rowkey也有相同格式的值。
我无法分享整条记录,因为它有 300 多列。
TSV 文件大小e:约 10GB
Hbase 表:大约 10254950 行。
感谢任何帮助。提前致谢。
【问题讨论】:
-
您介意分享样本记录吗?
-
分享输出:
System.out.println("Rowkey1 :" + dataArray[12] + "-" + dataArray[13]); System.out.print("Rowkey2 :" + new String(kv.getRow()) + " "); -
你调试了吗?如果是,调试的结果是什么?在没有看到实际数据的情况下很难调试你的 if 条件。什么是 dataArray[12]+"-"+dataArray[13] ? new String(kv.getRow()) 返回什么?样品案例?
-
更新了我的问题。我无法分享整条记录,因为它有 300 多列。