मेरे पास एक कॉलम परिवार के साथ एक HBase (v0.94.7) तालिका है और समय के साथ कॉलम जोड़े गए हैं। इन स्तंभों को उनके द्वारा बनाए गए टाइमस्टैम्प के रूप में नामित किया गया है, इसलिए जब तक मैं पंक्ति से पूछता हूं, मुझे नहीं पता कि इसमें सभी कॉलम क्या हैं।पंक्ति के सभी कॉलम को प्रतिस्थापित करने के लिए HBase RowMutations
अब एक पंक्ति दी गई है, मैं परमाणु इस कॉलम परिवार के सभी मौजूदा कॉलम को हटा दूं और कॉलम और मानों का एक नया सेट जोड़ना चाहता हूं।
तो मैं का उपयोग कर के बारे में सोचा HBase के RowMutations की तरह:
RowMutations mutations = new RowMutations(row);
//delete the column family
Delete delete = new Delete(row);
delete.deleteFamily(cf);
//add new columns
Put put = new Put(row);
put.add(cf, col1, v1);
put.add(cf, col2, v2);
//delete column family and add new columns to same family
mutations.add(delete);
mutations.add(put);
table.mutateRow(mutations);
लेकिन क्या इस कोड को कर बस स्तंभ परिवार हटा रहा है समाप्त होता है, यह नहीं जोड़ता है नए कॉलम। क्या यह व्यवहार अपेक्षित है?
यदि ऐसा है, तो मैं पर अपने लक्ष्य को कैसे प्राप्त कर सकता हूं कॉलम परिवार के सभी कॉलम को कॉलम के नए सेट के साथ बदलकर?
यहाँ एक ही के लिए एक परीक्षण मामला है:
import junit.framework.Assert;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.NavigableMap;
public class TestHBaseRowMutations {
static String tableName = "nnn";
static byte[] cf1 = Bytes.toBytes("cf1");
static byte[] row = Bytes.toBytes("r1");
static HTablePool hTablePool;
@BeforeClass
public static void beforeClass() throws Exception {
Configuration config = HBaseConfiguration.create();
hTablePool = new HTablePool(config, Integer.MAX_VALUE);
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
tableDescriptor.addFamily(new HColumnDescriptor(cf1));
try {
admin.createTable(tableDescriptor);
} catch (TableExistsException ignored){}
}
@Before
public void before() throws Exception {
HTableInterface table = hTablePool.getTable(tableName);
try {
Delete delete = new Delete(row);
table.delete(delete);
System.out.println("deleted old row");
Put put = new Put(row);
put.add(cf1, Bytes.toBytes("c1"), Bytes.toBytes("v1"));
put.add(cf1, Bytes.toBytes("c11"), Bytes.toBytes("v11"));
table.put(put);
System.out.println("Created row with seed data");
} finally {
table.close();
}
}
@Test
public void testColumnFamilyDeleteRM() throws Exception {
HTableInterface table = hTablePool.getTable(tableName);
try {
RowMutations rm =new RowMutations(row);
//delete column family cf1
Delete delete = new Delete(row);
delete.deleteFamily(cf1);
rm.add(delete);
System.out.println("Added delete of cf1 column family to row mutation");
//add new columns to same column family cf1
Put put = new Put(row);
put.add(cf1, Bytes.toBytes("c1"), Bytes.toBytes("new_v1"));
put.add(cf1, Bytes.toBytes("c11"), Bytes.toBytes("new_v11"));
rm.add(put);
System.out.println("Added puts of cf1 column family to row mutation");
//atomic mutate the row
table.mutateRow(rm);
System.out.println("Mutated row");
//now read the column family cf1 back
Result result = table.get(new Get(row));
NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(cf1);
//column family cf1 should have 2 columns because of the Put above
//------Following assert fails as cf1 does not exist anymore, why does cf1 not exist anymore?-------
Assert.assertNotNull(familyMap);
Assert.assertEquals(2, familyMap.size());
} finally {
table.close();
}
}
}
क्या यह हो सकता है कि आप * कॉलम परिवार * को हटा रहे हों, व्यक्तिगत कॉलम स्वयं नहीं? –
जैसा ऊपर बताया गया है, मैं कॉलम नामों को पहले से नहीं जानता, इसलिए हाँ, मैं हटाए गए परिवार के साथ कॉलम परिवार को हटा रहा हूं। डिलीटफैमिली (सीएफ); और उसके बाद put.add (cf, col1, v1) के साथ नए कॉलम जोड़ना; ... – vinodv26