首页 > 代码库 > Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException

Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException

hbase 执行批量删除时出现错误:
 Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at org.apache.hadoop.hbase.client.HTable.delete(HTable.java:852)

这种异常其实很常见,remove操作不支持,为什么会出现不支持的情况呢?检查代码时发现传参list 是通过Arrays.asList()创建的,

很明显,问题就是这个地方导致的,Arrays.asList()返回是实际上是Arrays类内部的一个实现ArrayList:

   /**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        private final E[] a;

        ArrayList(E[] array) {
            if (array==null)
                throw new NullPointerException();
            a = array;
        }

        public int size() {
            return a.length;
        }

        public Object[] toArray() {
            return a.clone();
        }

        public <T> T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class<? extends T[]>) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }

        public E get(int index) {
            return a[index];
        }

        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }

        public int indexOf(Object o) {
            if (o==null) {
                for (int i=0; i<a.length; i++)
                    if (a[i]==null)
                        return i;
            } else {
                for (int i=0; i<a.length; i++)
                    if (o.equals(a[i]))
                        return i;
            }
            return -1;
        }

        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }
    }

Arrays.ArrayList继承至AbstractList,AbstractList本身是不支持删除操作的,Arrays.ArrayList也没有重现remove方法,因此,如果通过Arrays.asList()创建的ArrayList,如果调用了remove方法,则会出现UnsupportedOperationException异常。

Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException